A platform for community discussion. Free, open, simple.
FIX: Ensure normalized_email gets anonymized (#29719)
Roman Rizzid97d48ead1f1
diff --git a/app/services/user_anonymizer.rb b/app/services/user_anonymizer.rbindex 8dd04bd5bd0..21c02cd1448 100644--- a/app/services/user_anonymizer.rb+++ b/app/services/user_anonymizer.rb@@ -40,7 +40,9 @@ class UserAnonymizerend@user.save!+@user.primary_email.update_attribute(:email, "#{@user.username}#{EMAIL_SUFFIX}")+ @user.primary_email.update_attribute(:normalized_email, "#{@user.username}#{EMAIL_SUFFIX}")options = @user.user_optionoptions.mailing_list_mode = falsediff --git a/db/migrate/20241112145744_anonymize_normalized_email_column.rb b/db/migrate/20241112145744_anonymize_normalized_email_column.rbnew file mode 100644index 00000000000..9a5e36dd94c--- /dev/null+++ b/db/migrate/20241112145744_anonymize_normalized_email_column.rb@@ -0,0 +1,14 @@+# frozen_string_literal: true+class AnonymizeNormalizedEmailColumn < ActiveRecord::Migration[7.1]+ def up+ execute <<~SQL+ UPDATE user_emails+ SET normalized_email = email+ WHERE SPLIT_PART(email, '@', 2) = 'anonymized.invalid'+ SQL+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/spec/services/user_anonymizer_spec.rb b/spec/services/user_anonymizer_spec.rbindex 5a753b4f37d..ad5ede53563 100644--- a/spec/services/user_anonymizer_spec.rb+++ b/spec/services/user_anonymizer_spec.rb@@ -39,6 +39,14 @@ RSpec.describe UserAnonymizer doexpect(user.reload.email).to eq("#{user.username}@anonymized.invalid")end+ it "changes the primary email normalized email address" do+ make_anonymous++ primary_email = user.reload.primary_email++ expect(primary_email.normalized_email).to eq("#{user.username}@anonymized.invalid")+ end+it "changes the primary email address when there is an email domain allowlist" doSiteSetting.allowed_email_domains = "example.net|wayne.com|discourse.org"