Update username only after successful user anonymization
This commit is contained in:
parent
2564a8285d
commit
4ba910ac54
|
@ -20,8 +20,9 @@ class UserAnonymizer
|
||||||
@prev_email = @user.email
|
@prev_email = @user.email
|
||||||
@prev_username = @user.username
|
@prev_username = @user.username
|
||||||
|
|
||||||
@user.update_attribute(:uploaded_avatar_id, nil)
|
unless UsernameChanger.new(@user, make_anon_username).change(run_update_job: false)
|
||||||
raise "Failed to change username" unless UsernameChanger.change(@user, make_anon_username)
|
raise "Failed to change username"
|
||||||
|
end
|
||||||
|
|
||||||
@user.reload
|
@user.reload
|
||||||
@user.password = SecureRandom.hex
|
@user.password = SecureRandom.hex
|
||||||
|
@ -29,6 +30,7 @@ class UserAnonymizer
|
||||||
@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
|
||||||
|
@user.uploaded_avatar_id = nil
|
||||||
|
|
||||||
if @opts.has_key?(:anonymize_ip)
|
if @opts.has_key?(:anonymize_ip)
|
||||||
@user.ip_address = @opts[:anonymize_ip]
|
@user.ip_address = @opts[:anonymize_ip]
|
||||||
|
@ -63,6 +65,11 @@ class UserAnonymizer
|
||||||
@user_history = log_action
|
@user_history = log_action
|
||||||
end
|
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,
|
Jobs.enqueue(:anonymize_user,
|
||||||
user_id: @user.id,
|
user_id: @user.id,
|
||||||
prev_email: @prev_email,
|
prev_email: @prev_email,
|
||||||
|
|
|
@ -24,10 +24,11 @@ class UserMerger
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def update_username
|
def update_username
|
||||||
Jobs::UpdateUsername.new.execute(user_id: @source_user.id,
|
UsernameChanger.update_username(user_id: @source_user.id,
|
||||||
old_username: @source_user.username,
|
old_username: @source_user.username,
|
||||||
new_username: @target_user.username,
|
new_username: @target_user.username,
|
||||||
avatar_template: @target_user.avatar_template)
|
avatar_template: @target_user.avatar_template,
|
||||||
|
asynchronous: false)
|
||||||
end
|
end
|
||||||
|
|
||||||
def move_posts
|
def move_posts
|
||||||
|
|
|
@ -13,19 +13,31 @@ class UsernameChanger
|
||||||
self.new(user, new_username, actor).change
|
self.new(user, new_username, actor).change
|
||||||
end
|
end
|
||||||
|
|
||||||
def change(asynchronous: true)
|
def change(asynchronous: true, run_update_job: true)
|
||||||
if @actor && @old_username != @new_username
|
if @actor && @old_username != @new_username
|
||||||
StaffActionLogger.new(@actor).log_username_change(@user, @old_username, @new_username)
|
StaffActionLogger.new(@actor).log_username_change(@user, @old_username, @new_username)
|
||||||
end
|
end
|
||||||
|
|
||||||
@user.username = @new_username
|
@user.username = @new_username
|
||||||
if @user.save
|
|
||||||
|
|
||||||
args = {
|
if @user.save
|
||||||
user_id: @user.id,
|
UsernameChanger.update_username(user_id: @user.id,
|
||||||
old_username: @old_username,
|
old_username: @old_username,
|
||||||
new_username: @new_username,
|
new_username: @new_username,
|
||||||
avatar_template: @user.avatar_template
|
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
|
if asynchronous
|
||||||
|
@ -33,11 +45,5 @@ class UsernameChanger
|
||||||
else
|
else
|
||||||
Jobs::UpdateUsername.new.execute(args)
|
Jobs::UpdateUsername.new.execute(args)
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
false
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -73,11 +73,7 @@ task "users:update_posts", [:old_username, :current_username] => [:environment]
|
||||||
end
|
end
|
||||||
|
|
||||||
user = find_user(current_username)
|
user = find_user(current_username)
|
||||||
Jobs::UpdateUsername.new.execute(
|
UsernameChanger.update_username(user.id, old_username, user.username, user.avatar_template)
|
||||||
user_id: user.id,
|
|
||||||
old_username: old_username,
|
|
||||||
new_username: user.username,
|
|
||||||
avatar_template: user.avatar_template)
|
|
||||||
|
|
||||||
puts "", "Username updated!", ""
|
puts "", "Username updated!", ""
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue