FIX: Do not allow anonymous users to be anonymized (#20776)

This commit is contained in:
Bianca Nenciu 2023-03-22 20:51:42 +02:00 committed by GitHub
parent 7070f81596
commit f12e77d500
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 2 deletions

View File

@ -3,6 +3,8 @@
class UserAnonymizer class UserAnonymizer
attr_reader :user_history attr_reader :user_history
EMAIL_SUFFIX = "@anonymized.invalid"
# opts: # opts:
# anonymize_ip - an optional new IP to update their logs with # anonymize_ip - an optional new IP to update their logs with
def initialize(user, actor = nil, opts = nil) def initialize(user, actor = nil, opts = nil)
@ -38,7 +40,7 @@ class UserAnonymizer
end end
@user.save! @user.save!
@user.primary_email.update_attribute(:email, "#{@user.username}@anonymized.invalid") @user.primary_email.update_attribute(:email, "#{@user.username}#{EMAIL_SUFFIX}")
options = @user.user_option options = @user.user_option
options.mailing_list_mode = false options.mailing_list_mode = false

View File

@ -74,7 +74,7 @@ module UserGuardian
end end
def can_anonymize_user?(user) def can_anonymize_user?(user)
is_staff? && !user.nil? && !user.staff? is_staff? && !user.nil? && !user.staff? && !user.email.ends_with?(UserAnonymizer::EMAIL_SUFFIX)
end end
def can_merge_user?(user) def can_merge_user?(user)

View File

@ -2773,6 +2773,10 @@ RSpec.describe Guardian do
expect(Guardian.new(user).can_anonymize_user?(user)).to be_falsey expect(Guardian.new(user).can_anonymize_user?(user)).to be_falsey
end end
it "it false for an anonymized user" do
expect(Guardian.new(user).can_anonymize_user?(anonymous_user)).to be_falsey
end
it "is true for admin anonymizing a regular user" do it "is true for admin anonymizing a regular user" do
expect(Guardian.new(admin).can_anonymize_user?(user)).to eq(true) expect(Guardian.new(admin).can_anonymize_user?(user)).to eq(true)
end end