2013-09-11 09:21:16 +08:00
|
|
|
# UserHistory stores information about actions that users have taken,
|
|
|
|
# like deleting users, changing site settings, dimissing notifications, etc.
|
|
|
|
# Use other classes, like StaffActionLogger, to log records to this table.
|
|
|
|
class UserHistory < ActiveRecord::Base
|
|
|
|
belongs_to :acting_user, class_name: 'User'
|
|
|
|
belongs_to :target_user, class_name: 'User'
|
2013-04-12 04:04:20 +08:00
|
|
|
|
|
|
|
validates_presence_of :action
|
|
|
|
|
2013-09-11 09:21:16 +08:00
|
|
|
scope :only_staff_actions, ->{ where("action IN (?)", UserHistory.staff_action_ids) }
|
|
|
|
|
2013-04-12 04:04:20 +08:00
|
|
|
def self.actions
|
2013-08-22 00:03:21 +08:00
|
|
|
@actions ||= Enum.new( :delete_user,
|
|
|
|
:change_trust_level,
|
|
|
|
:change_site_setting,
|
|
|
|
:change_site_customization,
|
2013-09-13 02:58:20 +08:00
|
|
|
:delete_site_customization,
|
|
|
|
:checked_for_custom_avatar,
|
|
|
|
:notified_about_avatar)
|
2013-04-12 04:04:20 +08:00
|
|
|
end
|
2013-08-09 22:06:02 +08:00
|
|
|
|
2013-09-11 09:21:16 +08:00
|
|
|
# Staff actions is a subset of all actions, used to audit actions taken by staff users.
|
|
|
|
def self.staff_actions
|
|
|
|
@staff_actions ||= [:delete_user,
|
|
|
|
:change_trust_level,
|
|
|
|
:change_site_setting,
|
|
|
|
:change_site_customization,
|
|
|
|
:delete_site_customization]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.staff_action_ids
|
|
|
|
@staff_action_ids ||= staff_actions.map { |a| actions[a] }
|
|
|
|
end
|
|
|
|
|
2013-08-09 22:06:02 +08:00
|
|
|
def self.with_filters(filters)
|
|
|
|
query = self
|
2013-09-11 09:21:16 +08:00
|
|
|
if filters[:action_name] and action_id = UserHistory.actions[filters[:action_name].to_sym]
|
2013-08-09 22:06:02 +08:00
|
|
|
query = query.where('action = ?', action_id)
|
|
|
|
end
|
2013-09-11 09:21:16 +08:00
|
|
|
[:acting_user, :target_user].each do |key|
|
2013-08-10 04:58:57 +08:00
|
|
|
if filters[key] and obj_id = User.where(username_lower: filters[key].downcase).pluck(:id)
|
|
|
|
query = query.where("#{key.to_s}_id = ?", obj_id)
|
|
|
|
end
|
|
|
|
end
|
2013-08-21 01:50:51 +08:00
|
|
|
query = query.where("subject = ?", filters[:subject]) if filters[:subject]
|
2013-08-09 22:06:02 +08:00
|
|
|
query
|
|
|
|
end
|
2013-08-21 22:49:35 +08:00
|
|
|
|
|
|
|
def new_value_is_json?
|
2013-09-11 09:21:16 +08:00
|
|
|
[UserHistory.actions[:change_site_customization], UserHistory.actions[:delete_site_customization]].include?(action)
|
2013-08-21 22:49:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def previous_value_is_json?
|
|
|
|
new_value_is_json?
|
|
|
|
end
|
2013-04-12 04:04:20 +08:00
|
|
|
end
|
2013-05-24 10:48:32 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
2013-07-24 05:58:26 +08:00
|
|
|
# Table name: staff_action_logs
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# action :integer not null
|
2013-07-24 05:58:26 +08:00
|
|
|
# staff_user_id :integer not null
|
2013-05-24 10:48:32 +08:00
|
|
|
# target_user_id :integer
|
|
|
|
# details :text
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2013-08-14 04:09:27 +08:00
|
|
|
# context :string(255)
|
|
|
|
# ip_address :string(255)
|
|
|
|
# email :string(255)
|
2013-08-28 08:42:58 +08:00
|
|
|
# subject :text
|
|
|
|
# previous_value :text
|
|
|
|
# new_value :text
|
2013-08-14 04:09:27 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_staff_action_logs_on_action_and_id (action,id)
|
|
|
|
# index_staff_action_logs_on_staff_user_id_and_id (staff_user_id,id)
|
2013-08-28 08:42:58 +08:00
|
|
|
# index_staff_action_logs_on_subject_and_id (subject,id)
|
2013-08-14 04:09:27 +08:00
|
|
|
# index_staff_action_logs_on_target_user_id_and_id (target_user_id,id)
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|
|
|
|
|