2013-04-12 04:04:20 +08:00
|
|
|
# Responsible for logging the actions of admins and moderators.
|
2013-07-24 05:58:26 +08:00
|
|
|
class StaffActionLogger
|
2013-04-12 04:04:20 +08:00
|
|
|
def initialize(admin)
|
|
|
|
@admin = admin
|
|
|
|
raise Discourse::InvalidParameters.new('admin is nil') unless @admin and @admin.is_a?(User)
|
|
|
|
end
|
|
|
|
|
2013-07-26 06:04:51 +08:00
|
|
|
def log_user_deletion(deleted_user, opts={})
|
2013-04-12 04:04:20 +08:00
|
|
|
raise Discourse::InvalidParameters.new('user is nil') unless deleted_user and deleted_user.is_a?(User)
|
2013-09-11 09:21:16 +08:00
|
|
|
UserHistory.create( params(opts).merge({
|
|
|
|
action: UserHistory.actions[:delete_user],
|
2013-07-26 06:04:51 +08:00
|
|
|
email: deleted_user.email,
|
2013-10-23 06:55:30 +08:00
|
|
|
ip_address: deleted_user.ip_address.to_s,
|
2013-04-12 04:04:20 +08:00
|
|
|
details: [:id, :username, :name, :created_at, :trust_level, :last_seen_at, :last_emailed_at].map { |x| "#{x}: #{deleted_user.send(x)}" }.join(', ')
|
2013-08-20 04:58:38 +08:00
|
|
|
}))
|
2013-04-12 04:04:20 +08:00
|
|
|
end
|
2013-07-08 17:53:22 +08:00
|
|
|
|
2013-08-14 00:04:28 +08:00
|
|
|
def log_trust_level_change(user, old_trust_level, new_trust_level, opts={})
|
2013-07-08 17:53:22 +08:00
|
|
|
raise Discourse::InvalidParameters.new('user is nil') unless user and user.is_a?(User)
|
2013-08-14 00:04:28 +08:00
|
|
|
raise Discourse::InvalidParameters.new('old trust level is invalid') unless TrustLevel.levels.values.include? old_trust_level
|
2013-07-08 17:53:22 +08:00
|
|
|
raise Discourse::InvalidParameters.new('new trust level is invalid') unless TrustLevel.levels.values.include? new_trust_level
|
2013-09-11 09:21:16 +08:00
|
|
|
UserHistory.create!( params(opts).merge({
|
|
|
|
action: UserHistory.actions[:change_trust_level],
|
2013-08-08 05:01:59 +08:00
|
|
|
target_user_id: user.id,
|
2013-08-14 00:04:28 +08:00
|
|
|
details: "old trust level: #{old_trust_level}, new trust level: #{new_trust_level}"
|
2013-08-20 04:58:38 +08:00
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_site_setting_change(setting_name, previous_value, new_value, opts={})
|
|
|
|
raise Discourse::InvalidParameters.new('setting_name is invalid') unless setting_name.present? and SiteSetting.respond_to?(setting_name)
|
2013-09-11 09:21:16 +08:00
|
|
|
UserHistory.create( params(opts).merge({
|
|
|
|
action: UserHistory.actions[:change_site_setting],
|
2013-08-20 04:58:38 +08:00
|
|
|
subject: setting_name,
|
|
|
|
previous_value: previous_value,
|
|
|
|
new_value: new_value
|
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
2013-08-22 00:03:21 +08:00
|
|
|
SITE_CUSTOMIZATION_LOGGED_ATTRS = ['stylesheet', 'header', 'position', 'enabled', 'key', 'override_default_style']
|
|
|
|
|
2013-08-21 22:49:35 +08:00
|
|
|
def log_site_customization_change(old_record, site_customization_params, opts={})
|
|
|
|
raise Discourse::InvalidParameters.new('site_customization_params is nil') unless site_customization_params
|
2013-09-11 09:21:16 +08:00
|
|
|
UserHistory.create( params(opts).merge({
|
|
|
|
action: UserHistory.actions[:change_site_customization],
|
2013-08-21 22:49:35 +08:00
|
|
|
subject: site_customization_params[:name],
|
2013-08-22 00:03:21 +08:00
|
|
|
previous_value: old_record ? old_record.attributes.slice(*SITE_CUSTOMIZATION_LOGGED_ATTRS).to_json : nil,
|
|
|
|
new_value: site_customization_params.slice(*(SITE_CUSTOMIZATION_LOGGED_ATTRS.map(&:to_sym))).to_json
|
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_site_customization_destroy(site_customization, opts={})
|
|
|
|
raise Discourse::InvalidParameters.new('site_customization is nil') unless site_customization
|
2013-09-11 09:21:16 +08:00
|
|
|
UserHistory.create( params(opts).merge({
|
|
|
|
action: UserHistory.actions[:delete_site_customization],
|
2013-08-22 00:03:21 +08:00
|
|
|
subject: site_customization.name,
|
|
|
|
previous_value: site_customization.attributes.slice(*SITE_CUSTOMIZATION_LOGGED_ATTRS).to_json
|
2013-08-21 22:49:35 +08:00
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
2013-11-08 02:53:32 +08:00
|
|
|
def log_user_suspend(user, reason, opts={})
|
2013-11-01 22:47:03 +08:00
|
|
|
raise Discourse::InvalidParameters.new('user is nil') unless user
|
|
|
|
UserHistory.create( params(opts).merge({
|
2013-11-08 02:53:32 +08:00
|
|
|
action: UserHistory.actions[:suspend_user],
|
2013-11-01 22:47:03 +08:00
|
|
|
target_user_id: user.id,
|
|
|
|
details: reason
|
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
2013-11-08 02:53:32 +08:00
|
|
|
def log_user_unsuspend(user, opts={})
|
2013-11-01 22:47:03 +08:00
|
|
|
raise Discourse::InvalidParameters.new('user is nil') unless user
|
|
|
|
UserHistory.create( params(opts).merge({
|
2013-11-08 02:53:32 +08:00
|
|
|
action: UserHistory.actions[:unsuspend_user],
|
2013-11-01 22:47:03 +08:00
|
|
|
target_user_id: user.id
|
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
2014-03-20 03:30:12 +08:00
|
|
|
def log_badge_grant(user_badge, opts={})
|
|
|
|
raise Discourse::InvalidParameters.new('user_badge is nil') unless user_badge
|
|
|
|
UserHistory.create( params(opts).merge({
|
|
|
|
action: UserHistory.actions[:grant_badge],
|
|
|
|
target_user_id: user_badge.user_id,
|
|
|
|
details: user_badge.badge.name
|
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_badge_revoke(user_badge, opts={})
|
|
|
|
raise Discourse::InvalidParameters.new('user_badge is nil') unless user_badge
|
|
|
|
UserHistory.create( params(opts).merge({
|
|
|
|
action: UserHistory.actions[:revoke_badge],
|
|
|
|
target_user_id: user_badge.user_id,
|
|
|
|
details: user_badge.badge.name
|
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
2013-08-20 04:58:38 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def params(opts)
|
2013-09-11 09:21:16 +08:00
|
|
|
{acting_user_id: @admin.id, context: opts[:context]}
|
2013-07-08 17:53:22 +08:00
|
|
|
end
|
2013-08-22 00:03:21 +08:00
|
|
|
|
2013-07-08 17:53:22 +08:00
|
|
|
end
|