FEATURE: Log user merge in staff logs

This commit is contained in:
Gerhard Schlager 2018-06-08 16:48:26 +02:00
parent bfa0f71e2b
commit 150ae21489
6 changed files with 38 additions and 4 deletions

View File

@ -80,7 +80,8 @@ class UserHistory < ActiveRecord::Base
removed_suspend_user: 61,
removed_unsilence_user: 62,
removed_unsuspend_user: 63,
post_rejected: 64
post_rejected: 64,
merge_user: 65
)
end
@ -139,7 +140,8 @@ class UserHistory < ActiveRecord::Base
:create_badge,
:change_badge,
:delete_badge,
:post_rejected
:post_rejected,
:merge_user
]
end

View File

@ -242,6 +242,16 @@ class StaffActionLogger
))
end
def log_user_merge(user, source_username, source_email, opts = {})
raise Discourse::InvalidParameters.new(:user) unless user
UserHistory.create!(params(opts).merge(
action: UserHistory.actions[:merge_user],
target_user_id: user.id,
context: I18n.t("staff_action_logs.user_merged", username: source_username),
email: source_email
))
end
BADGE_FIELDS ||= %i{id name description long_description icon image badge_type_id
badge_grouping_id query allow_title multiple_grant listable target_posts
enabled auto_revoke show_posts system}

View File

@ -1,7 +1,9 @@
class UserMerger
def initialize(source_user, target_user)
def initialize(source_user, target_user, acting_user = nil)
@source_user = source_user
@target_user = target_user
@acting_user = acting_user
@source_primary_email = source_user.email
end
def merge!
@ -19,6 +21,7 @@ class UserMerger
delete_source_user
delete_source_user_references
log_merge
end
protected
@ -353,7 +356,7 @@ class UserMerger
email: "#{@source_user.username}_#{SecureRandom.hex}@no-email.invalid"
)
UserDestroyer.new(Discourse.system_user).destroy(@source_user)
UserDestroyer.new(Discourse.system_user).destroy(@source_user, quiet: true)
end
def delete_source_user_references
@ -366,6 +369,11 @@ class UserMerger
UserAction.where(acting_user_id: @source_user.id).delete_all
end
def log_merge
logger = StaffActionLogger.new(@acting_user || Discourse.system_user)
logger.log_user_merge(@target_user, @source_user.username, @source_primary_email)
end
def update_user_id(table_name, opts = {})
builder = update_user_id_sql_builder(table_name, opts)
builder.exec(source_user_id: @source_user.id, target_user_id: @target_user.id)

View File

@ -3410,6 +3410,7 @@ en:
create_badge: "create badge"
change_badge: "change badge"
delete_badge: "delete badge"
merge_user: "merge user"
screened_emails:
title: "Screened Emails"
description: "When someone tries to create a new account, the following email addresses will be checked and the registration will be blocked, or some other action performed."

View File

@ -3887,3 +3887,4 @@ en:
staff_action_logs:
not_found: "not found"
unknown: "unknown"
user_merged: "%{username} was merged into this account"

View File

@ -850,6 +850,7 @@ describe UserMerger do
UserHistory.create(action: UserHistory.actions[:anonymize_user], target_user_id: walter.id, acting_user_id: source_user.id)
merge_users!
UserHistory.where(action: UserHistory.actions[:merge_user], target_user_id: target_user.id).delete_all
expect(UserHistory.where(target_user_id: target_user.id).count).to eq(1)
expect(UserHistory.where(target_user_id: source_user.id).count).to eq(0)
@ -1028,4 +1029,15 @@ describe UserMerger do
merge_users!
end
it "correctly logs the merge" do
expect { merge_users! }.to change { UserHistory.count }.by(1)
log_entry = UserHistory.last
expect(log_entry.action).to eq(UserHistory.actions[:merge_user])
expect(log_entry.acting_user_id).to eq(Discourse::SYSTEM_USER_ID)
expect(log_entry.target_user_id).to eq(target_user.id)
expect(log_entry.context).to eq(I18n.t("staff_action_logs.user_merged", username: source_user.username))
expect(log_entry.email).to eq("alice@work.com")
end
end