2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
2015-02-05 14:34:57 -05:00
|
|
|
def self.base_attrs
|
|
|
|
%i[topic_id post_id context subject ip_address previous_value new_value]
|
|
|
|
end
|
|
|
|
|
2013-04-11 16:04:20 -04:00
|
|
|
def initialize(admin)
|
|
|
|
@admin = admin
|
2015-02-05 14:34:57 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:admin) unless @admin && @admin.is_a?(User)
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
2024-10-15 22:09:07 -04:00
|
|
|
USER_FIELDS = %i[id username name created_at trust_level last_seen_at last_emailed_at]
|
2018-05-17 12:09:27 -04:00
|
|
|
|
2013-07-25 18:04:51 -04:00
|
|
|
def log_user_deletion(deleted_user, opts = {})
|
2015-02-05 14:34:57 -05:00
|
|
|
unless deleted_user && deleted_user.is_a?(User)
|
|
|
|
raise Discourse::InvalidParameters.new(:deleted_user)
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2019-05-06 21:27:05 -04:00
|
|
|
|
|
|
|
details = USER_FIELDS.map { |x| "#{x}: #{deleted_user.public_send(x)}" }.join("\n")
|
|
|
|
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:delete_user],
|
|
|
|
ip_address: deleted_user.ip_address.to_s,
|
|
|
|
details: details,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2014-10-01 11:40:13 -04:00
|
|
|
end
|
|
|
|
|
2015-02-05 14:34:57 -05:00
|
|
|
def log_custom(custom_type, details = nil)
|
|
|
|
raise Discourse::InvalidParameters.new(:custom_type) unless custom_type
|
|
|
|
|
|
|
|
details ||= {}
|
|
|
|
|
|
|
|
attrs = {}
|
|
|
|
StaffActionLogger.base_attrs.each do |attr|
|
|
|
|
attrs[attr] = details.delete(attr) if details.has_key?(attr)
|
|
|
|
end
|
2024-02-22 14:47:15 -05:00
|
|
|
attrs[:details] = details.map { |r| "#{r[0]}: #{truncate(r[1].to_s)}" }.join("\n")
|
2015-02-05 14:34:57 -05:00
|
|
|
attrs[:acting_user_id] = @admin.id
|
|
|
|
attrs[:action] = UserHistory.actions[:custom_staff]
|
|
|
|
attrs[:custom_type] = custom_type
|
|
|
|
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(attrs)
|
2015-02-05 14:34:57 -05:00
|
|
|
end
|
|
|
|
|
2023-06-07 18:19:58 -04:00
|
|
|
def edit_directory_columns_details(column_data, directory_column)
|
|
|
|
directory_column = directory_column.attributes.transform_values(&:to_s)
|
|
|
|
previous_value = directory_column
|
|
|
|
new_value = directory_column.clone
|
|
|
|
|
|
|
|
directory_column.each do |key, value|
|
|
|
|
if column_data[key] != value && column_data[key].present?
|
|
|
|
new_value[key] = column_data[key]
|
|
|
|
elsif key != "name"
|
|
|
|
previous_value.delete key
|
|
|
|
new_value.delete key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[previous_value.to_json, new_value.to_json]
|
|
|
|
end
|
|
|
|
|
2014-10-01 11:40:13 -04:00
|
|
|
def log_post_deletion(deleted_post, opts = {})
|
2015-02-05 14:34:57 -05:00
|
|
|
unless deleted_post && deleted_post.is_a?(Post)
|
|
|
|
raise Discourse::InvalidParameters.new(:deleted_post)
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2014-10-01 11:40:13 -04:00
|
|
|
|
2016-04-10 23:50:08 -04:00
|
|
|
topic = deleted_post.topic || Topic.with_deleted.find_by(id: deleted_post.topic_id)
|
2014-10-01 11:40:13 -04:00
|
|
|
|
2018-06-01 23:55:41 -04:00
|
|
|
username = deleted_post.user.try(:username) || I18n.t("staff_action_logs.unknown")
|
|
|
|
name = deleted_post.user.try(:name) || I18n.t("staff_action_logs.unknown")
|
|
|
|
topic_title = topic.try(:title) || I18n.t("staff_action_logs.not_found")
|
2015-08-13 23:57:02 -04:00
|
|
|
|
2024-07-23 03:27:11 -04:00
|
|
|
if opts[:permanent]
|
|
|
|
details = []
|
|
|
|
else
|
|
|
|
details = [
|
|
|
|
"id: #{deleted_post.id}",
|
|
|
|
"created_at: #{deleted_post.created_at}",
|
|
|
|
"user: #{username} (#{name})",
|
|
|
|
"topic: #{topic_title}",
|
|
|
|
"post_number: #{deleted_post.post_number}",
|
|
|
|
"raw: #{truncate(deleted_post.raw)}",
|
|
|
|
]
|
|
|
|
end
|
2014-10-01 11:40:13 -04:00
|
|
|
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
2024-07-23 03:27:11 -04:00
|
|
|
action: UserHistory.actions[opts[:permanent] ? :delete_post_permanently : :delete_post],
|
2018-05-17 12:09:27 -04:00
|
|
|
post_id: deleted_post.id,
|
|
|
|
details: details.join("\n"),
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2014-10-01 11:40:13 -04:00
|
|
|
end
|
|
|
|
|
2018-03-21 00:15:16 -04:00
|
|
|
def log_topic_delete_recover(topic, action = "delete_topic", opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:topic) unless topic && topic.is_a?(Topic)
|
2014-10-01 11:40:13 -04:00
|
|
|
|
2018-03-21 00:15:16 -04:00
|
|
|
user = topic.user ? "#{topic.user.username} (#{topic.user.name})" : "(deleted user)"
|
2016-06-15 08:41:08 -04:00
|
|
|
|
2024-07-23 03:27:11 -04:00
|
|
|
if action == "delete_topic_permanently"
|
|
|
|
details = []
|
|
|
|
else
|
|
|
|
details = [
|
|
|
|
"id: #{topic.id}",
|
|
|
|
"created_at: #{topic.created_at}",
|
|
|
|
"user: #{user}",
|
|
|
|
"title: #{topic.title}",
|
|
|
|
]
|
|
|
|
|
|
|
|
if first_post = topic.ordered_posts.with_deleted.first
|
|
|
|
details << "raw: #{truncate(first_post.raw)}"
|
|
|
|
end
|
2014-10-01 11:40:13 -04:00
|
|
|
end
|
|
|
|
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[action.to_sym],
|
|
|
|
topic_id: topic.id,
|
|
|
|
details: details.join("\n"),
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -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 = {})
|
2015-02-05 14:34:57 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user && user.is_a?(User)
|
|
|
|
unless TrustLevel.valid? old_trust_level
|
|
|
|
raise Discourse::InvalidParameters.new(:old_trust_level)
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2015-02-05 14:34:57 -05:00
|
|
|
unless TrustLevel.valid? new_trust_level
|
|
|
|
raise Discourse::InvalidParameters.new(:new_trust_level)
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:change_trust_level],
|
|
|
|
target_user_id: user.id,
|
2022-08-29 06:00:48 -04:00
|
|
|
previous_value: old_trust_level,
|
|
|
|
new_value: new_trust_level,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2013-08-19 16:58:38 -04:00
|
|
|
end
|
|
|
|
|
2017-01-10 16:45:36 -05:00
|
|
|
def log_lock_trust_level(user, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user && user.is_a?(User)
|
2018-05-17 12:09:27 -04:00
|
|
|
action =
|
|
|
|
UserHistory.actions[
|
|
|
|
user.manual_locked_trust_level.nil? ? :unlock_trust_level : :lock_trust_level
|
|
|
|
]
|
|
|
|
UserHistory.create!(params(opts).merge(action: action, target_user_id: user.id))
|
2017-01-10 16:45:36 -05:00
|
|
|
end
|
|
|
|
|
2018-03-13 15:59:12 -04:00
|
|
|
def log_topic_published(topic, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:topic) unless topic && topic.is_a?(Topic)
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:topic_published], topic_id: topic.id),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-02-22 04:03:52 -05:00
|
|
|
def log_topic_timestamps_changed(topic, new_timestamp, previous_timestamp, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:topic) unless topic && topic.is_a?(Topic)
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:topic_timestamps_changed],
|
|
|
|
topic_id: topic.id,
|
|
|
|
new_value: new_timestamp,
|
|
|
|
previous_value: previous_timestamp,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2019-02-22 04:03:52 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-01-25 15:38:40 -05:00
|
|
|
def log_post_lock(post, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:post) unless post && post.is_a?(Post)
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[opts[:locked] ? :post_locked : :post_unlocked],
|
|
|
|
post_id: post.id,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-01-25 15:38:40 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-03-12 13:49:52 -04:00
|
|
|
def log_post_edit(post, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:post) unless post && post.is_a?(Post)
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:post_edit],
|
|
|
|
post_id: post.id,
|
2024-02-22 14:47:15 -05:00
|
|
|
details: "#{truncate(opts[:old_raw])}\n\n---\n\n#{truncate(post.raw)}",
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-03-12 13:49:52 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-07-21 14:29:02 -04:00
|
|
|
def log_topic_closed(topic, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:topic) unless topic && topic.is_a?(Topic)
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[opts[:closed] ? :topic_closed : :topic_opened],
|
|
|
|
topic_id: topic.id,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2020-07-21 14:29:02 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_topic_archived(topic, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:topic) unless topic && topic.is_a?(Topic)
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[opts[:archived] ? :topic_archived : :topic_unarchived],
|
|
|
|
topic_id: topic.id,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2020-07-21 14:29:02 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-07-16 05:08:09 -04:00
|
|
|
def log_topic_slow_mode(topic, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:topic) unless topic && topic.is_a?(Topic)
|
|
|
|
|
|
|
|
details = opts[:enabled] ? ["interval: #{opts[:seconds]}", "until: #{opts[:until]}"] : []
|
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action:
|
|
|
|
UserHistory.actions[opts[:enabled] ? :topic_slow_mode_set : :topic_slow_mode_removed],
|
|
|
|
topic_id: topic.id,
|
|
|
|
details: details.join("\n"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-07-21 14:29:02 -04:00
|
|
|
def log_post_staff_note(post, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:post) unless post && post.is_a?(Post)
|
|
|
|
|
|
|
|
args =
|
|
|
|
params(opts).merge(
|
2020-11-11 07:49:53 -05:00
|
|
|
action:
|
|
|
|
UserHistory.actions[
|
|
|
|
opts[:new_value].present? ? :post_staff_note_create : :post_staff_note_destroy
|
|
|
|
],
|
2020-07-21 14:29:02 -04:00
|
|
|
post_id: post.id,
|
|
|
|
)
|
2020-11-11 07:49:53 -05:00
|
|
|
args[:new_value] = opts[:new_value] if opts[:new_value].present?
|
2020-07-21 14:29:02 -04:00
|
|
|
args[:previous_value] = opts[:old_value] if opts[:old_value].present?
|
|
|
|
|
|
|
|
UserHistory.create!(params(opts).merge(args))
|
|
|
|
end
|
|
|
|
|
2013-08-19 16:58:38 -04:00
|
|
|
def log_site_setting_change(setting_name, previous_value, new_value, opts = {})
|
2015-02-05 14:34:57 -05:00
|
|
|
unless setting_name.present? && SiteSetting.respond_to?(setting_name)
|
|
|
|
raise Discourse::InvalidParameters.new(:setting_name)
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:change_site_setting],
|
|
|
|
subject: setting_name,
|
2022-05-22 23:23:10 -04:00
|
|
|
previous_value: previous_value&.to_s,
|
|
|
|
new_value: new_value&.to_s,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2013-08-19 16:58:38 -04:00
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def theme_json(theme)
|
2023-04-24 10:30:51 -04:00
|
|
|
ThemeSerializer.new(theme, root: false, include_theme_field_values: true).to_json
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def strip_duplicates(old, cur)
|
|
|
|
return old, cur unless old && cur
|
|
|
|
|
|
|
|
old = JSON.parse(old)
|
|
|
|
cur = JSON.parse(cur)
|
|
|
|
|
|
|
|
old.each do |k, v|
|
|
|
|
next if k == "name"
|
|
|
|
next if k == "id"
|
|
|
|
if (v == cur[k])
|
|
|
|
cur.delete(k)
|
|
|
|
old.delete(k)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[old.to_json, cur.to_json]
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_theme_change(old_json, new_theme, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:new_theme) unless new_theme
|
|
|
|
|
|
|
|
new_json = theme_json(new_theme)
|
|
|
|
old_json, new_json = strip_duplicates(old_json, new_json)
|
|
|
|
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
2024-02-22 14:47:15 -05:00
|
|
|
params(opts).merge(json_params(old_json, new_json)).merge(
|
2018-05-17 12:09:27 -04:00
|
|
|
action: UserHistory.actions[:change_theme],
|
|
|
|
subject: new_theme.name,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2013-08-21 12:03:21 -04:00
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def log_theme_destroy(theme, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:theme) unless theme
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
2024-02-22 14:47:15 -05:00
|
|
|
params(opts).merge(json_params(theme_json(theme), nil)).merge(
|
2018-05-17 12:09:27 -04:00
|
|
|
action: UserHistory.actions[:delete_theme],
|
|
|
|
subject: theme.name,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2019-07-03 04:18:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def log_theme_component_disabled(component)
|
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(
|
|
|
|
action: UserHistory.actions[:disable_theme_component],
|
|
|
|
subject: component.name,
|
|
|
|
context: component.id,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2019-07-03 04:18:11 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_theme_component_enabled(component)
|
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(
|
|
|
|
action: UserHistory.actions[:enable_theme_component],
|
|
|
|
subject: component.name,
|
|
|
|
context: component.id,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2019-07-03 04:18:11 -04:00
|
|
|
)
|
2013-08-21 10:49:35 -04:00
|
|
|
end
|
|
|
|
|
2019-06-21 13:49:14 -04:00
|
|
|
def log_theme_setting_change(setting_name, previous_value, new_value, theme, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:theme) unless theme
|
2020-05-01 12:51:11 -04:00
|
|
|
unless theme.cached_settings.has_key?(setting_name)
|
|
|
|
raise Discourse::InvalidParameters.new(:setting_name)
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2019-06-21 13:49:14 -04:00
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:change_theme_setting],
|
2023-12-06 17:25:00 -05:00
|
|
|
subject: "#{theme.name}: #{setting_name}",
|
2019-06-21 13:49:14 -04:00
|
|
|
previous_value: previous_value,
|
|
|
|
new_value: new_value,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2019-06-21 13:49:14 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-01-18 10:26:14 -05:00
|
|
|
def log_site_text_change(subject, new_text = nil, old_text = nil, opts = {})
|
2024-05-27 06:27:13 -04:00
|
|
|
raise Discourse::InvalidParameters.new(:subject) if subject.blank?
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:change_site_text],
|
|
|
|
subject: subject,
|
|
|
|
previous_value: old_text,
|
|
|
|
new_value: new_text,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2015-12-18 08:31:04 -05:00
|
|
|
end
|
|
|
|
|
2015-01-16 17:30:46 -05:00
|
|
|
def log_username_change(user, old_username, new_username, opts = {})
|
2015-02-05 14:34:57 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:change_username],
|
|
|
|
target_user_id: user.id,
|
|
|
|
previous_value: old_username,
|
|
|
|
new_value: new_username,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2015-01-16 17:30:46 -05:00
|
|
|
end
|
|
|
|
|
2017-02-23 00:48:57 -05:00
|
|
|
def log_name_change(user_id, old_name, new_name, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user_id
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:change_name],
|
|
|
|
target_user_id: user_id,
|
|
|
|
previous_value: old_name,
|
|
|
|
new_value: new_name,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2017-02-23 00:48:57 -05:00
|
|
|
end
|
|
|
|
|
2013-11-07 13:53:32 -05:00
|
|
|
def log_user_suspend(user, reason, opts = {})
|
2015-02-05 14:34:57 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
2017-11-20 12:33:02 -05:00
|
|
|
|
2018-01-19 11:34:54 -05:00
|
|
|
details = StaffMessageFormat.new(:suspend, reason, opts[:message]).format
|
2017-11-20 12:33:02 -05:00
|
|
|
|
2017-09-14 14:10:39 -04:00
|
|
|
args =
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:suspend_user],
|
|
|
|
target_user_id: user.id,
|
2017-11-20 12:33:02 -05:00
|
|
|
details: details,
|
2017-09-14 14:10:39 -04:00
|
|
|
)
|
|
|
|
args[:post_id] = opts[:post_id] if opts[:post_id]
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(args)
|
2013-11-01 10:47:03 -04:00
|
|
|
end
|
|
|
|
|
2013-11-07 13:53:32 -05:00
|
|
|
def log_user_unsuspend(user, opts = {})
|
2015-02-05 14:34:57 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:unsuspend_user], target_user_id: user.id),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-06-08 10:48:26 -04:00
|
|
|
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,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-06-08 10:48:26 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-10-15 22:09:07 -04:00
|
|
|
BADGE_FIELDS = %i[
|
2021-03-17 01:55:23 -04:00
|
|
|
id
|
|
|
|
name
|
|
|
|
description
|
|
|
|
long_description
|
|
|
|
icon
|
|
|
|
image_upload_id
|
|
|
|
badge_type_id
|
2018-05-17 12:09:27 -04:00
|
|
|
badge_grouping_id
|
|
|
|
query
|
|
|
|
allow_title
|
|
|
|
multiple_grant
|
|
|
|
listable
|
|
|
|
target_posts
|
|
|
|
enabled
|
|
|
|
auto_revoke
|
|
|
|
show_posts
|
|
|
|
system
|
2023-01-09 07:20:10 -05:00
|
|
|
]
|
2018-05-17 12:09:27 -04:00
|
|
|
|
|
|
|
def log_badge_creation(badge)
|
|
|
|
raise Discourse::InvalidParameters.new(:badge) unless badge
|
2019-05-06 21:27:05 -04:00
|
|
|
|
|
|
|
details =
|
|
|
|
BADGE_FIELDS
|
|
|
|
.map { |f| [f, badge.public_send(f)] }
|
|
|
|
.select { |f, v| v.present? }
|
|
|
|
.map { |f, v| "#{f}: #{v}" }
|
|
|
|
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(action: UserHistory.actions[:create_badge], details: details.join("\n")),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_badge_change(badge)
|
|
|
|
raise Discourse::InvalidParameters.new(:badge) unless badge
|
|
|
|
details = ["id: #{badge.id}"]
|
|
|
|
badge.previous_changes.each do |f, values|
|
|
|
|
details << "#{f}: #{values[1]}" if BADGE_FIELDS.include?(f.to_sym)
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(action: UserHistory.actions[:change_badge], details: details.join("\n")),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_badge_deletion(badge)
|
|
|
|
raise Discourse::InvalidParameters.new(:badge) unless badge
|
2019-05-06 21:27:05 -04:00
|
|
|
|
|
|
|
details =
|
|
|
|
BADGE_FIELDS
|
|
|
|
.map { |f| [f, badge.public_send(f)] }
|
|
|
|
.select { |f, v| v.present? }
|
|
|
|
.map { |f, v| "#{f}: #{v}" }
|
|
|
|
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(action: UserHistory.actions[:delete_badge], details: details.join("\n")),
|
|
|
|
)
|
2013-11-01 10:47:03 -04:00
|
|
|
end
|
|
|
|
|
2014-03-19 15:30:12 -04:00
|
|
|
def log_badge_grant(user_badge, opts = {})
|
2015-02-05 14:34:57 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:user_badge) unless user_badge
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:grant_badge],
|
|
|
|
target_user_id: user_badge.user_id,
|
|
|
|
details: user_badge.badge.name,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2014-03-19 15:30:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def log_badge_revoke(user_badge, opts = {})
|
2015-02-05 14:34:57 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:user_badge) unless user_badge
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:revoke_badge],
|
|
|
|
target_user_id: user_badge.user_id,
|
|
|
|
details: user_badge.badge.name,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2014-03-19 15:30:12 -04:00
|
|
|
end
|
|
|
|
|
2019-11-08 00:34:24 -05:00
|
|
|
def log_title_revoke(user, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:revoke_title],
|
|
|
|
target_user_id: user.id,
|
|
|
|
details: opts[:revoke_reason],
|
|
|
|
previous_value: opts[:previous_value],
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2019-11-08 00:34:24 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_title_change(user, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:change_title],
|
|
|
|
target_user_id: user.id,
|
|
|
|
details: opts[:details],
|
|
|
|
new_value: opts[:new_value],
|
|
|
|
previous_value: opts[:previous_value],
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2019-11-08 00:34:24 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-11-27 16:32:17 -05:00
|
|
|
def log_change_upload_secure_status(opts = {})
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:override_upload_secure_status],
|
|
|
|
details: [
|
|
|
|
"upload_id: #{opts[:upload_id]}",
|
|
|
|
"reason: #{I18n.t("uploads.marked_insecure_from_theme_component_reason")}",
|
|
|
|
].join("\n"),
|
|
|
|
new_value: opts[:new_value],
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2019-11-27 16:32:17 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-09-29 16:31:05 -04:00
|
|
|
def log_check_email(user, opts = {})
|
2015-02-05 14:34:57 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:check_email], target_user_id: user.id),
|
|
|
|
)
|
2014-09-29 16:31:05 -04:00
|
|
|
end
|
|
|
|
|
2014-12-29 05:50:36 -05:00
|
|
|
def log_show_emails(users, opts = {})
|
2014-12-15 14:14:12 -05:00
|
|
|
return if users.blank?
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:check_email],
|
|
|
|
details: users.map { |u| "[#{u.id}] #{u.username}" }.join("\n"),
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2014-11-03 06:46:08 -05:00
|
|
|
end
|
|
|
|
|
2014-11-06 04:58:47 -05:00
|
|
|
def log_impersonate(user, opts = {})
|
2015-02-05 14:34:57 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:impersonate], target_user_id: user.id),
|
|
|
|
)
|
2014-11-06 04:58:47 -05:00
|
|
|
end
|
|
|
|
|
2022-04-12 14:07:37 -04:00
|
|
|
def log_roll_up(subnet, ips, opts = {})
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:roll_up],
|
2022-04-12 14:07:37 -04:00
|
|
|
details: "#{subnet} from #{ips.join(", ")}",
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2014-11-24 13:48:54 -05:00
|
|
|
end
|
|
|
|
|
2021-09-17 06:37:56 -04:00
|
|
|
def log_category_settings_change(
|
|
|
|
category,
|
|
|
|
category_params,
|
|
|
|
old_permissions: nil,
|
|
|
|
old_custom_fields: nil
|
|
|
|
)
|
2015-09-17 03:51:32 -04:00
|
|
|
validate_category(category)
|
|
|
|
|
|
|
|
changed_attributes = category.previous_changes.slice(*category_params.keys)
|
|
|
|
|
2024-06-20 04:33:01 -04:00
|
|
|
if !old_permissions.empty? && (old_permissions != category_params[:permissions].to_h)
|
2015-09-17 03:51:32 -04:00
|
|
|
changed_attributes.merge!(
|
|
|
|
permissions: [old_permissions.to_json, category_params[:permissions].to_json],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-17 06:37:56 -04:00
|
|
|
if old_custom_fields && category_params[:custom_fields]
|
|
|
|
category_params[:custom_fields].each do |key, value|
|
2022-05-11 04:21:51 -04:00
|
|
|
next if old_custom_fields[key] == value
|
2021-09-17 06:37:56 -04:00
|
|
|
changed_attributes["custom_fields[#{key}]"] = [old_custom_fields[key], value]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-17 03:51:32 -04:00
|
|
|
changed_attributes.each do |key, value|
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(
|
|
|
|
action: UserHistory.actions[:change_category_settings],
|
|
|
|
category_id: category.id,
|
|
|
|
context: category.url,
|
|
|
|
subject: key,
|
|
|
|
previous_value: value[0],
|
|
|
|
new_value: value[1],
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2015-09-17 03:51:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_category_deletion(category)
|
|
|
|
validate_category(category)
|
|
|
|
|
|
|
|
details = [
|
|
|
|
"created_at: #{category.created_at}",
|
|
|
|
"name: #{category.name}",
|
|
|
|
"permissions: #{category.permissions_params}",
|
|
|
|
]
|
|
|
|
|
|
|
|
if parent_category = category.parent_category
|
|
|
|
details << "parent_category: #{parent_category.name}"
|
|
|
|
end
|
|
|
|
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(
|
|
|
|
action: UserHistory.actions[:delete_category],
|
|
|
|
category_id: category.id,
|
|
|
|
details: details.join("\n"),
|
|
|
|
context: category.url,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2015-09-17 03:51:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def log_category_creation(category)
|
|
|
|
validate_category(category)
|
|
|
|
|
|
|
|
details = ["created_at: #{category.created_at}", "name: #{category.name}"]
|
|
|
|
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(
|
|
|
|
action: UserHistory.actions[:create_category],
|
|
|
|
details: details.join("\n"),
|
|
|
|
category_id: category.id,
|
|
|
|
context: category.url,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2015-09-17 03:51:32 -04:00
|
|
|
end
|
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
def log_silence_user(user, opts = {})
|
2016-01-14 15:05:11 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
2017-11-20 12:33:02 -05:00
|
|
|
|
2018-02-13 15:33:15 -05:00
|
|
|
create_args =
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:silence_user],
|
|
|
|
target_user_id: user.id,
|
|
|
|
details: opts[:details],
|
2017-11-13 13:41:36 -05:00
|
|
|
)
|
2018-02-13 15:33:15 -05:00
|
|
|
create_args[:post_id] = opts[:post_id] if opts[:post_id]
|
|
|
|
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(create_args)
|
2016-01-14 15:05:11 -05:00
|
|
|
end
|
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
def log_unsilence_user(user, opts = {})
|
2016-01-14 15:05:11 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:unsilence_user], target_user_id: user.id),
|
|
|
|
)
|
2017-12-21 20:18:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def log_disable_second_factor_auth(user, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:disabled_second_factor],
|
|
|
|
target_user_id: user.id,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2016-01-14 15:05:11 -05:00
|
|
|
end
|
|
|
|
|
2016-01-27 04:38:16 -05:00
|
|
|
def log_grant_admin(user, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:grant_admin], target_user_id: user.id),
|
|
|
|
)
|
2016-01-27 04:38:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def log_revoke_admin(user, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:revoke_admin], target_user_id: user.id),
|
|
|
|
)
|
2016-01-27 04:38:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def log_grant_moderation(user, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:grant_moderation], target_user_id: user.id),
|
|
|
|
)
|
2016-01-27 04:38:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def log_revoke_moderation(user, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:revoke_moderation], target_user_id: user.id),
|
|
|
|
)
|
2016-01-27 04:38:16 -05:00
|
|
|
end
|
|
|
|
|
2017-01-16 13:53:31 -05:00
|
|
|
def log_backup_create(opts = {})
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:backup_create],
|
|
|
|
ip_address: @admin.ip_address.to_s,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2016-02-27 12:38:24 -05:00
|
|
|
end
|
|
|
|
|
2018-09-18 17:46:45 -04:00
|
|
|
def log_entity_export(entity, opts = {})
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:entity_export],
|
|
|
|
ip_address: @admin.ip_address.to_s,
|
|
|
|
subject: entity,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-09-18 17:46:45 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-01-16 13:53:31 -05:00
|
|
|
def log_backup_download(backup, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:backup) unless backup
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:backup_download],
|
|
|
|
ip_address: @admin.ip_address.to_s,
|
|
|
|
details: backup.filename,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2017-01-16 13:53:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def log_backup_destroy(backup, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:backup) unless backup
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:backup_destroy],
|
|
|
|
ip_address: @admin.ip_address.to_s,
|
|
|
|
details: backup.filename,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2017-01-16 13:53:31 -05:00
|
|
|
end
|
|
|
|
|
2016-07-25 12:57:06 -04:00
|
|
|
def log_revoke_email(user, reason, opts = {})
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:revoke_email],
|
|
|
|
target_user_id: user.id,
|
|
|
|
details: reason,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2016-07-25 12:57:06 -04:00
|
|
|
end
|
|
|
|
|
2019-03-12 04:16:56 -04:00
|
|
|
def log_user_approve(user, opts = {})
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:approve_user], target_user_id: user.id),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-07-25 12:57:06 -04:00
|
|
|
def log_user_deactivate(user, reason, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:deactivate_user],
|
|
|
|
target_user_id: user.id,
|
|
|
|
details: reason,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2016-05-02 17:15:32 -04:00
|
|
|
end
|
|
|
|
|
2017-01-10 16:45:36 -05:00
|
|
|
def log_user_activate(user, reason, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:activate_user],
|
|
|
|
target_user_id: user.id,
|
|
|
|
details: reason,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2017-01-10 16:45:36 -05:00
|
|
|
end
|
|
|
|
|
2016-09-14 16:36:08 -04:00
|
|
|
def log_wizard_step(step, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:step) unless step
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:wizard_step], context: step.id),
|
|
|
|
)
|
2016-09-14 16:36:08 -04:00
|
|
|
end
|
|
|
|
|
2017-01-11 01:46:48 -05:00
|
|
|
def log_change_readonly_mode(state)
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(
|
|
|
|
action: UserHistory.actions[:change_readonly_mode],
|
|
|
|
previous_value: !state,
|
|
|
|
new_value: state,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2017-01-11 01:46:48 -05:00
|
|
|
end
|
|
|
|
|
2018-01-27 07:51:22 -05:00
|
|
|
def log_check_personal_message(topic, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:topic) unless topic && topic.is_a?(Topic)
|
2018-05-17 12:09:27 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: UserHistory.actions[:check_personal_message],
|
|
|
|
topic_id: topic.id,
|
|
|
|
context: topic.relative_url,
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2018-01-27 07:51:22 -05:00
|
|
|
end
|
|
|
|
|
2018-04-23 01:48:53 -04:00
|
|
|
def log_post_approved(post, opts = {})
|
2019-01-03 12:03:01 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:post) unless post.is_a?(Post)
|
2018-04-23 01:48:53 -04:00
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:post_approved], post_id: post.id),
|
2018-05-17 12:09:27 -04:00
|
|
|
)
|
2018-04-23 01:48:53 -04:00
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
def log_post_rejected(reviewable, rejected_at, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:rejected_post) unless reviewable.is_a?(Reviewable)
|
2018-06-01 10:12:31 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
topic = reviewable.topic || Topic.with_deleted.find_by(id: reviewable.topic_id)
|
2018-06-01 23:55:41 -04:00
|
|
|
topic_title = topic&.title || I18n.t("staff_action_logs.not_found")
|
2023-07-18 07:50:31 -04:00
|
|
|
username = reviewable.target_created_by&.username || I18n.t("staff_action_logs.unknown")
|
|
|
|
name = reviewable.target_created_by&.name || I18n.t("staff_action_logs.unknown")
|
2018-06-01 10:12:31 -04:00
|
|
|
|
|
|
|
details = [
|
2019-01-03 12:03:01 -05:00
|
|
|
"created_at: #{reviewable.created_at}",
|
|
|
|
"rejected_at: #{rejected_at}",
|
2018-06-01 10:12:31 -04:00
|
|
|
"user: #{username} (#{name})",
|
|
|
|
"topic: #{topic_title}",
|
2024-02-22 14:47:15 -05:00
|
|
|
"raw: #{truncate(reviewable.payload["raw"])}",
|
2018-06-01 10:12:31 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:post_rejected], details: details.join("\n")),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-03-21 11:13:09 -04:00
|
|
|
def log_web_hook(web_hook, action, opts = {})
|
|
|
|
details = ["webhook_id: #{web_hook.id}", "payload_url: #{web_hook.payload_url}"]
|
|
|
|
|
2019-03-29 12:05:51 -04:00
|
|
|
old_values, new_values = get_changes(opts[:changes])
|
2019-03-21 11:13:09 -04:00
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: action,
|
|
|
|
context: details.join(", "),
|
|
|
|
previous_value: old_values&.join(", "),
|
|
|
|
new_value: new_values&.join(", "),
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2019-03-21 11:13:09 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-04-18 07:36:37 -04:00
|
|
|
def log_web_hook_deactivate(web_hook, response_http_status, opts = {})
|
|
|
|
context = ["webhook_id: #{web_hook.id}", "webhook_response_status: #{response_http_status}"]
|
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(
|
|
|
|
action: UserHistory.actions[:web_hook_deactivate],
|
|
|
|
context: context,
|
|
|
|
details:
|
|
|
|
I18n.t("staff_action_logs.webhook_deactivation_reason", status: response_http_status),
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2019-04-18 07:36:37 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-03-29 12:05:51 -04:00
|
|
|
def log_embeddable_host(embeddable_host, action, opts = {})
|
|
|
|
old_values, new_values = get_changes(opts[:changes])
|
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(
|
|
|
|
action: action,
|
|
|
|
context: "host: #{embeddable_host.host}",
|
|
|
|
previous_value: old_values&.join(", "),
|
|
|
|
new_value: new_values&.join(", "),
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2019-03-29 12:05:51 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-11-05 09:10:23 -05:00
|
|
|
def log_api_key(api_key, action, opts = {})
|
|
|
|
opts[:changes]&.delete("key") # Do not log the full key
|
|
|
|
|
|
|
|
history_params = params(opts).merge(action: action, subject: api_key.truncated_key)
|
|
|
|
|
|
|
|
if opts[:changes]
|
|
|
|
old_values, new_values = get_changes(opts[:changes])
|
2024-05-27 06:27:13 -04:00
|
|
|
history_params[:previous_value] = old_values&.join(", ") if opts[:changes].keys.exclude?("id")
|
2019-11-05 09:10:23 -05:00
|
|
|
history_params[:new_value] = new_values&.join(", ")
|
|
|
|
end
|
|
|
|
|
|
|
|
UserHistory.create!(history_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_api_key_revoke(api_key)
|
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(
|
|
|
|
subject: api_key.truncated_key,
|
|
|
|
action: UserHistory.actions[:api_key_update],
|
|
|
|
details: I18n.t("staff_action_logs.api_key.revoked"),
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2019-11-05 09:10:23 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_api_key_restore(api_key)
|
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(
|
|
|
|
subject: api_key.truncated_key,
|
|
|
|
action: UserHistory.actions[:api_key_update],
|
|
|
|
details: I18n.t("staff_action_logs.api_key.restored"),
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2019-11-05 09:10:23 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-04-08 12:52:36 -04:00
|
|
|
def log_published_page(topic_id, slug)
|
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(subject: slug, topic_id: topic_id, action: UserHistory.actions[:page_published]),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_unpublished_page(topic_id, slug)
|
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(
|
|
|
|
subject: slug,
|
|
|
|
topic_id: topic_id,
|
|
|
|
action: UserHistory.actions[:page_unpublished],
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2020-04-08 12:52:36 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-06-10 12:11:49 -04:00
|
|
|
def log_add_email(user)
|
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
action: UserHistory.actions[:add_email],
|
|
|
|
acting_user_id: @admin.id,
|
|
|
|
target_user_id: user.id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_update_email(user)
|
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
action: UserHistory.actions[:update_email],
|
|
|
|
acting_user_id: @admin.id,
|
|
|
|
target_user_id: user.id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_destroy_email(user)
|
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
action: UserHistory.actions[:destroy_email],
|
|
|
|
acting_user_id: @admin.id,
|
|
|
|
target_user_id: user.id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-06-30 01:52:46 -04:00
|
|
|
def log_watched_words_creation(watched_word)
|
|
|
|
raise Discourse::InvalidParameters.new(:watched_word) unless watched_word
|
|
|
|
|
2024-05-10 11:11:56 -04:00
|
|
|
action_key = :watched_word_create
|
|
|
|
action_key = :create_watched_word_group if watched_word.is_a?(WatchedWordGroup)
|
|
|
|
|
2021-06-30 01:52:46 -04:00
|
|
|
UserHistory.create!(
|
2024-05-10 11:11:56 -04:00
|
|
|
action: UserHistory.actions[action_key],
|
2021-06-30 01:52:46 -04:00
|
|
|
acting_user_id: @admin.id,
|
|
|
|
details: watched_word.action_log_details,
|
|
|
|
context: WatchedWord.actions[watched_word.action],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_watched_words_deletion(watched_word)
|
|
|
|
raise Discourse::InvalidParameters.new(:watched_word) unless watched_word
|
|
|
|
|
2024-05-10 11:11:56 -04:00
|
|
|
action_key = :watched_word_destroy
|
|
|
|
action_key = :delete_watched_word_group if watched_word.is_a?(WatchedWordGroup)
|
|
|
|
|
2021-06-30 01:52:46 -04:00
|
|
|
UserHistory.create!(
|
2024-05-10 11:11:56 -04:00
|
|
|
action: UserHistory.actions[action_key],
|
2021-06-30 01:52:46 -04:00
|
|
|
acting_user_id: @admin.id,
|
|
|
|
details: watched_word.action_log_details,
|
|
|
|
context: WatchedWord.actions[watched_word.action],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-06-29 06:23:28 -04:00
|
|
|
def log_group_deletion(group)
|
2022-05-09 11:12:52 -04:00
|
|
|
raise Discourse::InvalidParameters.new(:group) if group.nil?
|
|
|
|
|
|
|
|
details = ["name: #{group.name}", "id: #{group.id}"]
|
|
|
|
|
|
|
|
details << "grant_trust_level: #{group.grant_trust_level}" if group.grant_trust_level
|
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
acting_user_id: @admin.id,
|
|
|
|
action: UserHistory.actions[:delete_group],
|
|
|
|
details: details.join(", "),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-01-19 16:09:01 -05:00
|
|
|
def log_permanently_delete_post_revisions(post)
|
|
|
|
raise Discourse::InvalidParameters.new(:post) if post.nil?
|
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
action: UserHistory.actions[:permanently_delete_post_revisions],
|
|
|
|
acting_user_id: @admin.id,
|
|
|
|
post_id: post.id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-02-21 16:55:44 -05:00
|
|
|
def log_create_public_sidebar_section(section)
|
|
|
|
UserHistory.create!(
|
|
|
|
action: UserHistory.actions[:create_public_sidebar_section],
|
|
|
|
acting_user_id: @admin.id,
|
|
|
|
subject: section.title,
|
|
|
|
details: custom_section_details(section),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_update_public_sidebar_section(section)
|
|
|
|
UserHistory.create!(
|
|
|
|
action: UserHistory.actions[:update_public_sidebar_section],
|
|
|
|
acting_user_id: @admin.id,
|
|
|
|
subject: section.title,
|
|
|
|
details: custom_section_details(section),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_destroy_public_sidebar_section(section)
|
|
|
|
UserHistory.create!(
|
|
|
|
action: UserHistory.actions[:destroy_public_sidebar_section],
|
|
|
|
acting_user_id: @admin.id,
|
|
|
|
subject: section.title,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-03-21 10:26:26 -04:00
|
|
|
def log_reset_bounce_score(user, opts = {})
|
|
|
|
raise Discourse::InvalidParameters.new(:user) unless user
|
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:reset_bounce_score], target_user_id: user.id),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-07-22 02:44:49 -04:00
|
|
|
def log_custom_emoji_create(name, opts = {})
|
|
|
|
opts[:details] = "Group: #{opts[:group]}" if opts[:group].present?
|
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:custom_emoji_create], new_value: name),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_custom_emoji_destroy(name, opts = {})
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(action: UserHistory.actions[:custom_emoji_destroy], previous_value: name),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-09-08 22:50:48 -04:00
|
|
|
def log_tag_group_create(name, new_value, opts = {})
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(json_params(nil, new_value)).merge(
|
|
|
|
action: UserHistory.actions[:tag_group_create],
|
|
|
|
subject: name,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_tag_group_destroy(name, old_value, opts = {})
|
|
|
|
UserHistory.create!(
|
|
|
|
params(opts).merge(json_params(old_value, nil)).merge(
|
|
|
|
action: UserHistory.actions[:tag_group_destroy],
|
|
|
|
subject: name,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_tag_group_change(name, old_data, new_data)
|
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(json_params(old_data, new_data)).merge(
|
|
|
|
action: UserHistory.actions[:tag_group_change],
|
|
|
|
subject: name,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-09-27 08:08:05 -04:00
|
|
|
def log_delete_associated_accounts(user, previous_value:, context:)
|
|
|
|
UserHistory.create!(
|
|
|
|
params.merge(
|
|
|
|
action: UserHistory.actions[:delete_associated_accounts],
|
|
|
|
target_user_id: user.id,
|
|
|
|
previous_value:,
|
|
|
|
context:,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-08-19 16:58:38 -04:00
|
|
|
private
|
|
|
|
|
2024-02-22 14:47:15 -05:00
|
|
|
def json_params(previous_value, new_value)
|
|
|
|
if (previous_value && previous_value.length > UserHistory::MAX_JSON_LENGTH) ||
|
|
|
|
(new_value && new_value.length > UserHistory::MAX_JSON_LENGTH)
|
|
|
|
{ context: I18n.t("staff_action_logs.json_too_long") }
|
|
|
|
else
|
|
|
|
{ previous_value: previous_value, new_value: new_value }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-29 12:05:51 -04:00
|
|
|
def get_changes(changes)
|
|
|
|
return unless changes
|
|
|
|
|
|
|
|
changes.delete("updated_at")
|
|
|
|
old_values = []
|
|
|
|
new_values = []
|
2021-04-21 05:36:32 -04:00
|
|
|
changes
|
|
|
|
.sort_by { |k, _| k.to_s }
|
|
|
|
.each do |k, v|
|
|
|
|
old_values << "#{k}: #{v[0]}"
|
|
|
|
new_values << "#{k}: #{v[1]}"
|
|
|
|
end
|
2019-03-29 12:05:51 -04:00
|
|
|
|
|
|
|
[old_values, new_values]
|
|
|
|
end
|
|
|
|
|
2015-02-05 14:34:57 -05:00
|
|
|
def params(opts = nil)
|
|
|
|
opts ||= {}
|
2020-12-10 12:06:41 -05:00
|
|
|
{ acting_user_id: @admin.id, context: opts[:context], details: opts[:details] }
|
2014-11-24 13:48:54 -05:00
|
|
|
end
|
2013-08-21 12:03:21 -04:00
|
|
|
|
2015-09-17 03:51:32 -04:00
|
|
|
def validate_category(category)
|
|
|
|
raise Discourse::InvalidParameters.new(:category) unless category && category.is_a?(Category)
|
|
|
|
end
|
2023-02-21 16:55:44 -05:00
|
|
|
|
|
|
|
def custom_section_details(section)
|
|
|
|
urls = section.sidebar_urls.map { |url| "#{url.name} - #{url.value}" }
|
|
|
|
"links: #{urls.join(", ")}"
|
|
|
|
end
|
2024-02-22 14:47:15 -05:00
|
|
|
|
|
|
|
def truncate(s)
|
|
|
|
if s.size > UserHistory::MAX_CONTEXT_LENGTH
|
|
|
|
"#{s.slice(..UserHistory::MAX_CONTEXT_LENGTH)}..."
|
|
|
|
else
|
|
|
|
s
|
|
|
|
end
|
|
|
|
end
|
2013-07-08 05:53:22 -04:00
|
|
|
end
|