Update username only after successful user anonymization

This commit is contained in:
Gerhard Schlager 2018-06-08 14:31:15 +02:00
parent 2564a8285d
commit 4ba910ac54
4 changed files with 36 additions and 26 deletions

View File

@ -20,8 +20,9 @@ class UserAnonymizer
@prev_email = @user.email
@prev_username = @user.username
@user.update_attribute(:uploaded_avatar_id, nil)
raise "Failed to change username" unless UsernameChanger.change(@user, make_anon_username)
unless UsernameChanger.new(@user, make_anon_username).change(run_update_job: false)
raise "Failed to change username"
end
@user.reload
@user.password = SecureRandom.hex
@ -29,6 +30,7 @@ class UserAnonymizer
@user.name = SiteSetting.full_name_required ? @user.username : nil
@user.date_of_birth = nil
@user.title = nil
@user.uploaded_avatar_id = nil
if @opts.has_key?(:anonymize_ip)
@user.ip_address = @opts[:anonymize_ip]
@ -63,6 +65,11 @@ class UserAnonymizer
@user_history = log_action
end
UsernameChanger.update_username(user_id: @user.id,
old_username: @prev_username,
new_username: @user.username,
avatar_template: @user.avatar_template)
Jobs.enqueue(:anonymize_user,
user_id: @user.id,
prev_email: @prev_email,

View File

@ -24,10 +24,11 @@ class UserMerger
protected
def update_username
Jobs::UpdateUsername.new.execute(user_id: @source_user.id,
old_username: @source_user.username,
new_username: @target_user.username,
avatar_template: @target_user.avatar_template)
UsernameChanger.update_username(user_id: @source_user.id,
old_username: @source_user.username,
new_username: @target_user.username,
avatar_template: @target_user.avatar_template,
asynchronous: false)
end
def move_posts

View File

@ -13,31 +13,37 @@ class UsernameChanger
self.new(user, new_username, actor).change
end
def change(asynchronous: true)
def change(asynchronous: true, run_update_job: true)
if @actor && @old_username != @new_username
StaffActionLogger.new(@actor).log_username_change(@user, @old_username, @new_username)
end
@user.username = @new_username
if @user.save
args = {
user_id: @user.id,
old_username: @old_username,
new_username: @new_username,
avatar_template: @user.avatar_template
}
if asynchronous
Jobs.enqueue(:update_username, args)
else
Jobs::UpdateUsername.new.execute(args)
end
UsernameChanger.update_username(user_id: @user.id,
old_username: @old_username,
new_username: @new_username,
avatar_template: @user.avatar_template,
asynchronous: asynchronous) if run_update_job
return true
end
false
end
def self.update_username(user_id:, old_username:, new_username:, avatar_template:, asynchronous: true)
args = {
user_id: user_id,
old_username: old_username,
new_username: new_username,
avatar_template: avatar_template
}
if asynchronous
Jobs.enqueue(:update_username, args)
else
Jobs::UpdateUsername.new.execute(args)
end
end
end

View File

@ -73,11 +73,7 @@ task "users:update_posts", [:old_username, :current_username] => [:environment]
end
user = find_user(current_username)
Jobs::UpdateUsername.new.execute(
user_id: user.id,
old_username: old_username,
new_username: user.username,
avatar_template: user.avatar_template)
UsernameChanger.update_username(user.id, old_username, user.username, user.avatar_template)
puts "", "Username updated!", ""
end