2013-04-11 16:04:20 -04:00
|
|
|
# Responsible for logging the actions of admins and moderators.
|
2013-07-23 17:58:26 -04:00
|
|
|
class StaffActionLogger
|
2014-11-24 13:48:54 -05:00
|
|
|
|
2013-04-11 16:04:20 -04:00
|
|
|
def initialize(admin)
|
|
|
|
@admin = admin
|
2014-10-01 11:40:13 -04:00
|
|
|
raise Discourse::InvalidParameters.new('admin is nil') unless @admin && @admin.is_a?(User)
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
2013-07-25 18:04:51 -04:00
|
|
|
def log_user_deletion(deleted_user, opts={})
|
2014-10-01 11:40:13 -04:00
|
|
|
raise Discourse::InvalidParameters.new('user is nil') unless deleted_user && deleted_user.is_a?(User)
|
2013-09-10 21:21:16 -04:00
|
|
|
UserHistory.create( params(opts).merge({
|
|
|
|
action: UserHistory.actions[:delete_user],
|
2013-07-25 18:04:51 -04:00
|
|
|
email: deleted_user.email,
|
2013-10-22 18:55:30 -04:00
|
|
|
ip_address: deleted_user.ip_address.to_s,
|
2014-10-01 11:40:13 -04:00
|
|
|
details: [:id, :username, :name, :created_at, :trust_level, :last_seen_at, :last_emailed_at].map { |x| "#{x}: #{deleted_user.send(x)}" }.join("\n")
|
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_post_deletion(deleted_post, opts={})
|
|
|
|
raise Discourse::InvalidParameters.new("post is nil") unless deleted_post && deleted_post.is_a?(Post)
|
|
|
|
|
|
|
|
topic = deleted_post.topic || Topic.with_deleted.find(deleted_post.topic_id)
|
|
|
|
|
|
|
|
details = [
|
|
|
|
"id: #{deleted_post.id}",
|
|
|
|
"created_at: #{deleted_post.created_at}",
|
|
|
|
"user: #{deleted_post.user.username} (#{deleted_post.user.name})",
|
|
|
|
"topic: #{topic.title}",
|
|
|
|
"post_number: #{deleted_post.post_number}",
|
|
|
|
"raw: #{deleted_post.raw}"
|
|
|
|
]
|
|
|
|
|
|
|
|
UserHistory.create(params(opts).merge({
|
|
|
|
action: UserHistory.actions[:delete_post],
|
|
|
|
post_id: deleted_post.id,
|
|
|
|
details: details.join("\n")
|
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_topic_deletion(deleted_topic, opts={})
|
|
|
|
raise Discourse::InvalidParameters.new("topic is nil") unless deleted_topic && deleted_topic.is_a?(Topic)
|
|
|
|
|
|
|
|
details = [
|
|
|
|
"id: #{deleted_topic.id}",
|
|
|
|
"created_at: #{deleted_topic.created_at}",
|
|
|
|
"user: #{deleted_topic.user.username} (#{deleted_topic.user.name})",
|
|
|
|
"title: #{deleted_topic.title}"
|
|
|
|
]
|
|
|
|
|
|
|
|
if first_post = deleted_topic.ordered_posts.first
|
|
|
|
details << "raw: #{first_post.raw}"
|
|
|
|
end
|
|
|
|
|
|
|
|
UserHistory.create(params(opts).merge({
|
|
|
|
action: UserHistory.actions[:delete_topic],
|
|
|
|
topic_id: deleted_topic.id,
|
|
|
|
details: details.join("\n")
|
2013-08-19 16:58:38 -04:00
|
|
|
}))
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
2013-07-08 05:53:22 -04:00
|
|
|
|
2013-08-13 12:04:28 -04:00
|
|
|
def log_trust_level_change(user, old_trust_level, new_trust_level, opts={})
|
2014-10-01 11:40:13 -04:00
|
|
|
raise Discourse::InvalidParameters.new('user is nil') unless user && user.is_a?(User)
|
2014-09-05 01:20:39 -04:00
|
|
|
raise Discourse::InvalidParameters.new('old trust level is invalid') unless TrustLevel.valid? old_trust_level
|
|
|
|
raise Discourse::InvalidParameters.new('new trust level is invalid') unless TrustLevel.valid? new_trust_level
|
2013-09-10 21:21:16 -04:00
|
|
|
UserHistory.create!( params(opts).merge({
|
|
|
|
action: UserHistory.actions[:change_trust_level],
|
2013-08-07 17:01:59 -04:00
|
|
|
target_user_id: user.id,
|
2014-10-01 11:40:13 -04:00
|
|
|
details: "old trust level: #{old_trust_level}\nnew trust level: #{new_trust_level}"
|
2013-08-19 16:58:38 -04:00
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_site_setting_change(setting_name, previous_value, new_value, opts={})
|
2014-10-01 11:40:13 -04:00
|
|
|
raise Discourse::InvalidParameters.new('setting_name is invalid') unless setting_name.present? && SiteSetting.respond_to?(setting_name)
|
2013-09-10 21:21:16 -04:00
|
|
|
UserHistory.create( params(opts).merge({
|
|
|
|
action: UserHistory.actions[:change_site_setting],
|
2013-08-19 16:58:38 -04:00
|
|
|
subject: setting_name,
|
|
|
|
previous_value: previous_value,
|
|
|
|
new_value: new_value
|
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
2014-12-22 00:18:32 -05:00
|
|
|
SITE_CUSTOMIZATION_LOGGED_ATTRS = ['stylesheet', 'header', 'position', 'enabled', 'key']
|
2013-08-21 12:03:21 -04:00
|
|
|
|
2013-08-21 10:49:35 -04: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-10 21:21:16 -04:00
|
|
|
UserHistory.create( params(opts).merge({
|
|
|
|
action: UserHistory.actions[:change_site_customization],
|
2013-08-21 10:49:35 -04:00
|
|
|
subject: site_customization_params[:name],
|
2013-08-21 12:03:21 -04: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-10 21:21:16 -04:00
|
|
|
UserHistory.create( params(opts).merge({
|
|
|
|
action: UserHistory.actions[:delete_site_customization],
|
2013-08-21 12:03:21 -04:00
|
|
|
subject: site_customization.name,
|
|
|
|
previous_value: site_customization.attributes.slice(*SITE_CUSTOMIZATION_LOGGED_ATTRS).to_json
|
2013-08-21 10:49:35 -04:00
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
2013-11-07 13:53:32 -05:00
|
|
|
def log_user_suspend(user, reason, opts={})
|
2013-11-01 10:47:03 -04:00
|
|
|
raise Discourse::InvalidParameters.new('user is nil') unless user
|
|
|
|
UserHistory.create( params(opts).merge({
|
2013-11-07 13:53:32 -05:00
|
|
|
action: UserHistory.actions[:suspend_user],
|
2013-11-01 10:47:03 -04:00
|
|
|
target_user_id: user.id,
|
|
|
|
details: reason
|
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
2013-11-07 13:53:32 -05:00
|
|
|
def log_user_unsuspend(user, opts={})
|
2013-11-01 10:47:03 -04:00
|
|
|
raise Discourse::InvalidParameters.new('user is nil') unless user
|
|
|
|
UserHistory.create( params(opts).merge({
|
2013-11-07 13:53:32 -05:00
|
|
|
action: UserHistory.actions[:unsuspend_user],
|
2013-11-01 10:47:03 -04:00
|
|
|
target_user_id: user.id
|
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
2014-03-19 15:30:12 -04: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
|
|
|
|
|
2014-09-29 16:31:05 -04:00
|
|
|
def log_check_email(user, opts={})
|
|
|
|
raise Discourse::InvalidParameters.new('user is nil') unless user
|
|
|
|
UserHistory.create(params(opts).merge({
|
|
|
|
action: UserHistory.actions[:check_email],
|
|
|
|
target_user_id: user.id
|
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
2014-11-03 06:46:08 -05:00
|
|
|
def log_show_emails(users)
|
2014-12-15 14:14:12 -05:00
|
|
|
return if users.blank?
|
|
|
|
|
2014-11-03 06:46:08 -05:00
|
|
|
values = []
|
|
|
|
|
|
|
|
users.each do |user|
|
|
|
|
values << "(#{@admin.id}, #{UserHistory.actions[:check_email]}, #{user.id}, current_timestamp, current_timestamp)"
|
|
|
|
end
|
|
|
|
|
|
|
|
# bulk insert
|
|
|
|
UserHistory.exec_sql <<-SQL
|
|
|
|
INSERT INTO user_histories (acting_user_id, action, target_user_id, created_at, updated_at)
|
|
|
|
VALUES #{values.join(",")}
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2014-11-06 04:58:47 -05:00
|
|
|
def log_impersonate(user, opts={})
|
|
|
|
raise Discourse::InvalidParameters.new("user is nil") unless user
|
|
|
|
UserHistory.create(params(opts).merge({
|
|
|
|
action: UserHistory.actions[:impersonate],
|
|
|
|
target_user_id: user.id
|
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
2014-11-26 19:12:23 -05:00
|
|
|
def log_roll_up(subnets, opts={})
|
2014-11-24 13:48:54 -05:00
|
|
|
UserHistory.create(params(opts).merge({
|
2014-11-26 19:12:23 -05:00
|
|
|
action: UserHistory.actions[:roll_up],
|
2014-11-24 13:48:54 -05:00
|
|
|
details: subnets.join(", ")
|
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
2013-08-19 16:58:38 -04:00
|
|
|
private
|
|
|
|
|
2014-11-24 13:48:54 -05:00
|
|
|
def params(opts)
|
|
|
|
{ acting_user_id: @admin.id, context: opts[:context] }
|
|
|
|
end
|
2013-08-21 12:03:21 -04:00
|
|
|
|
2013-07-08 05:53:22 -04:00
|
|
|
end
|