FIX: Ensure normalized_email gets anonymized (#29720)

This commit is contained in:
Roman Rizzi 2024-11-12 15:41:11 -03:00 committed by GitHub
parent 0394df524a
commit d51a9373b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 0 deletions

View File

@ -40,7 +40,9 @@ class UserAnonymizer
end
@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_option
options.mailing_list_mode = false

View File

@ -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
end

View File

@ -39,6 +39,14 @@ RSpec.describe UserAnonymizer do
expect(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" do
SiteSetting.allowed_email_domains = "example.net|wayne.com|discourse.org"