From 4ec2c1e9a9e6e967b66c8aa878fd195b0bccfac0 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Tue, 3 Aug 2021 10:04:35 +0530 Subject: [PATCH] DEV: don't merge email address if target user is not human. (#13915) While merging two user accounts don't merge the source user's email address if the target user is not a human. Co-authored-by: Alan Guo Xiang Tan --- app/services/user_merger.rb | 4 +++- spec/services/user_merger_spec.rb | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/services/user_merger.rb b/app/services/user_merger.rb index c322d5f3638..1284dd655ef 100644 --- a/app/services/user_merger.rb +++ b/app/services/user_merger.rb @@ -363,7 +363,9 @@ class UserMerger update_user_id(:user_custom_fields, conditions: "x.name = y.name") - update_user_id(:user_emails, conditions: "x.email = y.email OR y.primary = false", updates: '"primary" = false') + if @target_user.human? + update_user_id(:user_emails, conditions: "x.email = y.email OR y.primary = false", updates: '"primary" = false') + end UserExport.where(user_id: @source_user.id).update_all(user_id: @target_user.id) diff --git a/spec/services/user_merger_spec.rb b/spec/services/user_merger_spec.rb index 55996ac52f0..06d3a51931c 100644 --- a/spec/services/user_merger_spec.rb +++ b/spec/services/user_merger_spec.rb @@ -848,6 +848,15 @@ describe UserMerger do expect(UserEmail.where(user_id: source_user.id).count).to eq(0) end + it "skips merging email addresses when target user is not human" do + target_user = Discourse.system_user + merge_users!(source_user, target_user) + + emails = UserEmail.where(user_id: target_user.id).pluck(:email, :primary) + expect(emails).to contain_exactly([target_user.email, true]) + expect(UserEmail.exists?(user_id: source_user.id)).to eq(false) + end + it "updates exports" do UserExport.create(file_name: "user-archive-alice1-190218-003249", user_id: source_user.id)