From ae6c4cd237346629b73efa8e318be71cc05209c8 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 9 Jun 2020 18:51:59 +0100 Subject: [PATCH] FIX: Allow merging users when email domains are restricted --- app/models/user.rb | 2 +- app/services/user_merger.rb | 2 ++ spec/services/user_merger_spec.rb | 9 +++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index ab30ea1b5ab..3caf839a2de 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1200,7 +1200,7 @@ class User < ActiveRecord::Base if primary_email new_record? ? primary_email.email = new_email : primary_email.update(email: new_email) else - self.primary_email = UserEmail.new(email: new_email, user: self, primary: true) + self.primary_email = UserEmail.new(email: new_email, user: self, primary: true, skip_validate_email: !should_validate_email_address?) end end diff --git a/app/services/user_merger.rb b/app/services/user_merger.rb index d6774b5080d..8da5afbfbcd 100644 --- a/app/services/user_merger.rb +++ b/app/services/user_merger.rb @@ -358,6 +358,8 @@ class UserMerger def delete_source_user @source_user.reload + + @source_user.skip_email_validation = true @source_user.update( admin: false, email: "#{@source_user.username}_#{SecureRandom.hex}@no-email.invalid" diff --git a/spec/services/user_merger_spec.rb b/spec/services/user_merger_spec.rb index 939afd2ae9c..f8f66c99b8b 100644 --- a/spec/services/user_merger_spec.rb +++ b/spec/services/user_merger_spec.rb @@ -990,6 +990,15 @@ describe UserMerger do expect(User.find_by_username(source_user.username)).to be_nil end + it "works even when email domains are restricted" do + SiteSetting.email_domains_whitelist = "example.com|work.com" + source_user.update_attribute(:admin, true) + + expect(User.find_by_username(source_user.username)).to be_present + merge_users! + expect(User.find_by_username(source_user.username)).to be_nil + end + it "deletes external auth infos of source user" do UserAssociatedAccount.create(user_id: source_user.id, provider_name: "facebook", provider_uid: "1234") GithubUserInfo.create(user_id: source_user.id, screen_name: "example", github_user_id: "examplel123123")