diff --git a/app/models/user_history.rb b/app/models/user_history.rb index c02e73f9aff..0ce8a8da6d8 100644 --- a/app/models/user_history.rb +++ b/app/models/user_history.rb @@ -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 diff --git a/app/services/staff_action_logger.rb b/app/services/staff_action_logger.rb index d8360735806..aa2338b305f 100644 --- a/app/services/staff_action_logger.rb +++ b/app/services/staff_action_logger.rb @@ -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} diff --git a/app/services/user_merger.rb b/app/services/user_merger.rb index 5f28717dede..e1d590926bb 100644 --- a/app/services/user_merger.rb +++ b/app/services/user_merger.rb @@ -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) diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index a1713ce3160..2fe14ee7a52 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -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." diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 62138552cdb..d2aa4507ae5 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -3887,3 +3887,4 @@ en: staff_action_logs: not_found: "not found" unknown: "unknown" + user_merged: "%{username} was merged into this account" diff --git a/spec/services/user_merger_spec.rb b/spec/services/user_merger_spec.rb index 6306291138b..fac984e6fc0 100644 --- a/spec/services/user_merger_spec.rb +++ b/spec/services/user_merger_spec.rb @@ -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