A platform for community discussion. Free, open, simple.
FIX: Email domain whitelist prevented user anonymizing
Gerhard Schlager1e9892950162
diff --git a/app/services/user_anonymizer.rb b/app/services/user_anonymizer.rbindex d87c7e6da7b..ae480252b55 100644--- a/app/services/user_anonymizer.rb+++ b/app/services/user_anonymizer.rb@@ -26,7 +26,6 @@ class UserAnonymizer@user.reload@user.password = SecureRandom.hex- @user.email = "#{@user.username}@anonymized.invalid"@user.name = SiteSetting.full_name_required ? @user.username : nil@user.date_of_birth = nil@user.title = nil@@ -37,7 +36,8 @@ class UserAnonymizer@user.registration_ip_address = @opts[:anonymize_ip]end- @user.save+ @user.save!+ @user.primary_email.update_attribute(:email, "#{@user.username}@anonymized.invalid")options = @user.user_optionoptions.email_always = false@@ -45,7 +45,7 @@ class UserAnonymizeroptions.email_digests = falseoptions.email_private_messages = falseoptions.email_direct = false- options.save+ options.save!if profile = @user.user_profileprofile.update(location: nil, website: nil, bio_raw: nil, bio_cooked: nil,@@ -61,6 +61,7 @@ class UserAnonymizer@user.instagram_user_info.try(:destroy)@user.user_open_ids.find_each { |x| x.destroy }@user.api_key.try(:destroy)+ @user.user_emails.secondary.destroy_all@user_history = log_actionend@@ -101,6 +102,6 @@ class UserAnonymizerhistory_details[:details] = "username: #{@prev_username}"end- UserHistory.create(history_details)+ UserHistory.create!(history_details)endenddiff --git a/spec/services/user_anonymizer_spec.rb b/spec/services/user_anonymizer_spec.rbindex 9d43e6c18ef..6f1e3483be2 100644--- a/spec/services/user_anonymizer_spec.rb+++ b/spec/services/user_anonymizer_spec.rb@@ -24,7 +24,7 @@ describe UserAnonymizer dodescribe "make_anonymous" dolet(:original_email) { "[email protected]" }- let(:user) { Fabricate(:user, username: "edward", email: original_email) }+ let(:user) { Fabricate(:user_single_email, username: "edward", email: original_email) }let(:another_user) { Fabricate(:evil_trout) }subject(:make_anonymous) { described_class.make_anonymous(user, admin) }@@ -33,11 +33,25 @@ describe UserAnonymizer doexpect(user.reload.username).to match(/^anon\d{3,}$/)end- it "changes email address" do+ it "changes the primary email address" domake_anonymousexpect(user.reload.email).to eq("#{user.username}@anonymized.invalid")end+ it "changes the primary email address when there is an email domain whitelist" do+ SiteSetting.email_domains_whitelist = 'example.net|wayne.com|discourse.org'++ make_anonymous+ expect(user.reload.email).to eq("#{user.username}@anonymized.invalid")+ end++ it "deletes secondary email addresses" do+ Fabricate(:secondary_email, user: user, email: "[email protected]")++ make_anonymous+ expect(user.reload.secondary_emails).to be_blank+ end+it "turns off all notifications" douser.user_option.update_columns(email_always: true