FIX: Email domain whitelist prevented user anonymizing

FIX: Secondary email addresses were not deleted during anonymizing
This commit is contained in:
Gerhard Schlager 2019-01-30 15:14:36 +01:00
parent 096974d178
commit 1e98929501
2 changed files with 21 additions and 6 deletions

View File

@ -26,7 +26,6 @@ class UserAnonymizer
@user.reload @user.reload
@user.password = SecureRandom.hex @user.password = SecureRandom.hex
@user.email = "#{@user.username}@anonymized.invalid"
@user.name = SiteSetting.full_name_required ? @user.username : nil @user.name = SiteSetting.full_name_required ? @user.username : nil
@user.date_of_birth = nil @user.date_of_birth = nil
@user.title = nil @user.title = nil
@ -37,7 +36,8 @@ class UserAnonymizer
@user.registration_ip_address = @opts[:anonymize_ip] @user.registration_ip_address = @opts[:anonymize_ip]
end end
@user.save @user.save!
@user.primary_email.update_attribute(:email, "#{@user.username}@anonymized.invalid")
options = @user.user_option options = @user.user_option
options.email_always = false options.email_always = false
@ -45,7 +45,7 @@ class UserAnonymizer
options.email_digests = false options.email_digests = false
options.email_private_messages = false options.email_private_messages = false
options.email_direct = false options.email_direct = false
options.save options.save!
if profile = @user.user_profile if profile = @user.user_profile
profile.update(location: nil, website: nil, bio_raw: nil, bio_cooked: nil, profile.update(location: nil, website: nil, bio_raw: nil, bio_cooked: nil,
@ -61,6 +61,7 @@ class UserAnonymizer
@user.instagram_user_info.try(:destroy) @user.instagram_user_info.try(:destroy)
@user.user_open_ids.find_each { |x| x.destroy } @user.user_open_ids.find_each { |x| x.destroy }
@user.api_key.try(:destroy) @user.api_key.try(:destroy)
@user.user_emails.secondary.destroy_all
@user_history = log_action @user_history = log_action
end end
@ -101,6 +102,6 @@ class UserAnonymizer
history_details[:details] = "username: #{@prev_username}" history_details[:details] = "username: #{@prev_username}"
end end
UserHistory.create(history_details) UserHistory.create!(history_details)
end end
end end

View File

@ -24,7 +24,7 @@ describe UserAnonymizer do
describe "make_anonymous" do describe "make_anonymous" do
let(:original_email) { "edward@example.net" } let(:original_email) { "edward@example.net" }
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) } let(:another_user) { Fabricate(:evil_trout) }
subject(:make_anonymous) { described_class.make_anonymous(user, admin) } subject(:make_anonymous) { described_class.make_anonymous(user, admin) }
@ -33,11 +33,25 @@ describe UserAnonymizer do
expect(user.reload.username).to match(/^anon\d{3,}$/) expect(user.reload.username).to match(/^anon\d{3,}$/)
end end
it "changes email address" do it "changes the primary email address" do
make_anonymous make_anonymous
expect(user.reload.email).to eq("#{user.username}@anonymized.invalid") expect(user.reload.email).to eq("#{user.username}@anonymized.invalid")
end 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: "secondary_email@example.com")
make_anonymous
expect(user.reload.secondary_emails).to be_blank
end
it "turns off all notifications" do it "turns off all notifications" do
user.user_option.update_columns( user.user_option.update_columns(
email_always: true email_always: true