mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 03:06:53 +08:00
Rename StaffActionLog to UserHistory
This commit is contained in:
@ -18,11 +18,11 @@ Discourse.AdminLogsStaffActionLogsController = Ember.ArrayController.extend(Disc
|
|||||||
self.set('content', result);
|
self.set('content', result);
|
||||||
self.set('loading', false);
|
self.set('loading', false);
|
||||||
});
|
});
|
||||||
}.observes('filters.action_name', 'filters.staff_user', 'filters.target_user', 'filters.subject'),
|
}.observes('filters.action_name', 'filters.acting_user', 'filters.target_user', 'filters.subject'),
|
||||||
|
|
||||||
filtersExists: function() {
|
filtersExists: function() {
|
||||||
return (_.size(this.get('filters')) > 0);
|
return (_.size(this.get('filters')) > 0);
|
||||||
}.property('filters.action_name', 'filters.staff_user', 'filters.target_user', 'filters.subject'),
|
}.property('filters.action_name', 'filters.acting_user', 'filters.target_user', 'filters.subject'),
|
||||||
|
|
||||||
clearFilter: function(key) {
|
clearFilter: function(key) {
|
||||||
delete this.get('filters')[key];
|
delete this.get('filters')[key];
|
||||||
@ -45,8 +45,8 @@ Discourse.AdminLogsStaffActionLogsController = Ember.ArrayController.extend(Disc
|
|||||||
}
|
}
|
||||||
}.property('filters.action_name'),
|
}.property('filters.action_name'),
|
||||||
|
|
||||||
filterByStaffUser: function(staff_user) {
|
filterByStaffUser: function(acting_user) {
|
||||||
this.set('filters.staff_user', staff_user.username);
|
this.set('filters.acting_user', acting_user.username);
|
||||||
},
|
},
|
||||||
|
|
||||||
filterByTargetUser: function(target_user) {
|
filterByTargetUser: function(target_user) {
|
||||||
|
@ -43,8 +43,8 @@ Discourse.StaffActionLog = Discourse.Model.extend({
|
|||||||
|
|
||||||
Discourse.StaffActionLog.reopenClass({
|
Discourse.StaffActionLog.reopenClass({
|
||||||
create: function(attrs) {
|
create: function(attrs) {
|
||||||
if (attrs.staff_user) {
|
if (attrs.acting_user) {
|
||||||
attrs.staff_user = Discourse.AdminUser.create(attrs.staff_user);
|
attrs.acting_user = Discourse.AdminUser.create(attrs.acting_user);
|
||||||
}
|
}
|
||||||
if (attrs.target_user) {
|
if (attrs.target_user) {
|
||||||
attrs.target_user = Discourse.AdminUser.create(attrs.target_user);
|
attrs.target_user = Discourse.AdminUser.create(attrs.target_user);
|
||||||
|
@ -8,9 +8,9 @@
|
|||||||
<i class="icon icon-remove-sign"></i>
|
<i class="icon icon-remove-sign"></i>
|
||||||
</a>
|
</a>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
{{#if filters.staff_user}}
|
{{#if filters.acting_user}}
|
||||||
<a {{action clearFilter "staff_user"}} class="filter">
|
<a {{action clearFilter "acting_user"}} class="filter">
|
||||||
<span class="label">{{i18n admin.logs.staff_actions.staff_user}}</span>: {{filters.staff_user}}
|
<span class="label">{{i18n admin.logs.staff_actions.staff_user}}</span>: {{filters.acting_user}}
|
||||||
<i class="icon icon-remove-sign"></i>
|
<i class="icon icon-remove-sign"></i>
|
||||||
</a>
|
</a>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<div class="col value first staff_user">
|
<div class="col value first staff_user">
|
||||||
{{#linkTo 'adminUser' staff_user}}{{avatar staff_user imageSize="tiny"}}{{/linkTo}}
|
{{#linkTo 'adminUser' acting_user}}{{avatar acting_user imageSize="tiny"}}{{/linkTo}}
|
||||||
<a {{action filterByStaffUser staff_user}}>{{staff_user.username}}</a>
|
<a {{action filterByStaffUser acting_user}}>{{acting_user.username}}</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="col value action">
|
<div class="col value action">
|
||||||
<a {{action filterByAction action_name}}>{{actionName}}</a>
|
<a {{action filterByAction action_name}}>{{actionName}}</a>
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
class Admin::StaffActionLogsController < Admin::AdminController
|
class Admin::StaffActionLogsController < Admin::AdminController
|
||||||
|
|
||||||
def index
|
def index
|
||||||
staff_action_logs = StaffActionLog.with_filters(params.slice(:action_name, :staff_user, :target_user, :subject)).limit(200).order('id DESC').includes(:staff_user, :target_user).to_a
|
staff_action_logs = UserHistory.with_filters(params.slice(:action_name, :acting_user, :target_user, :subject)).only_staff_actions.limit(200).order('id DESC').includes(:acting_user, :target_user).to_a
|
||||||
render_serialized(staff_action_logs, StaffActionLogSerializer)
|
render_serialized(staff_action_logs, UserHistorySerializer)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
# StaffActionLog stores information about actions that staff members have taken,
|
# UserHistory stores information about actions that users have taken,
|
||||||
# like deleting users, changing site settings, etc.
|
# like deleting users, changing site settings, dimissing notifications, etc.
|
||||||
# Use the StaffActionLogger class to log records to this table.
|
# Use other classes, like StaffActionLogger, to log records to this table.
|
||||||
class StaffActionLog < ActiveRecord::Base
|
class UserHistory < ActiveRecord::Base
|
||||||
belongs_to :staff_user, class_name: 'User'
|
belongs_to :acting_user, class_name: 'User'
|
||||||
belongs_to :target_user, class_name: 'User'
|
belongs_to :target_user, class_name: 'User'
|
||||||
|
|
||||||
validates_presence_of :staff_user_id
|
validates_presence_of :acting_user_id
|
||||||
validates_presence_of :action
|
validates_presence_of :action
|
||||||
|
|
||||||
|
scope :only_staff_actions, ->{ where("action IN (?)", UserHistory.staff_action_ids) }
|
||||||
|
|
||||||
def self.actions
|
def self.actions
|
||||||
@actions ||= Enum.new( :delete_user,
|
@actions ||= Enum.new( :delete_user,
|
||||||
:change_trust_level,
|
:change_trust_level,
|
||||||
@ -16,12 +18,25 @@ class StaffActionLog < ActiveRecord::Base
|
|||||||
:delete_site_customization)
|
:delete_site_customization)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
def self.with_filters(filters)
|
def self.with_filters(filters)
|
||||||
query = self
|
query = self
|
||||||
if filters[:action_name] and action_id = StaffActionLog.actions[filters[:action_name].to_sym]
|
if filters[:action_name] and action_id = UserHistory.actions[filters[:action_name].to_sym]
|
||||||
query = query.where('action = ?', action_id)
|
query = query.where('action = ?', action_id)
|
||||||
end
|
end
|
||||||
[:staff_user, :target_user].each do |key|
|
[:acting_user, :target_user].each do |key|
|
||||||
if filters[key] and obj_id = User.where(username_lower: filters[key].downcase).pluck(:id)
|
if filters[key] and obj_id = User.where(username_lower: filters[key].downcase).pluck(:id)
|
||||||
query = query.where("#{key.to_s}_id = ?", obj_id)
|
query = query.where("#{key.to_s}_id = ?", obj_id)
|
||||||
end
|
end
|
||||||
@ -31,7 +46,7 @@ class StaffActionLog < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def new_value_is_json?
|
def new_value_is_json?
|
||||||
[StaffActionLog.actions[:change_site_customization], StaffActionLog.actions[:delete_site_customization]].include?(action)
|
[UserHistory.actions[:change_site_customization], UserHistory.actions[:delete_site_customization]].include?(action)
|
||||||
end
|
end
|
||||||
|
|
||||||
def previous_value_is_json?
|
def previous_value_is_json?
|
@ -1,4 +1,4 @@
|
|||||||
class StaffActionLogSerializer < ApplicationSerializer
|
class UserHistorySerializer < ApplicationSerializer
|
||||||
attributes :action_name,
|
attributes :action_name,
|
||||||
:details,
|
:details,
|
||||||
:context,
|
:context,
|
||||||
@ -9,11 +9,11 @@ class StaffActionLogSerializer < ApplicationSerializer
|
|||||||
:previous_value,
|
:previous_value,
|
||||||
:new_value
|
:new_value
|
||||||
|
|
||||||
has_one :staff_user, serializer: BasicUserSerializer, embed: :objects
|
has_one :acting_user, serializer: BasicUserSerializer, embed: :objects
|
||||||
has_one :target_user, serializer: BasicUserSerializer, embed: :objects
|
has_one :target_user, serializer: BasicUserSerializer, embed: :objects
|
||||||
|
|
||||||
def action_name
|
def action_name
|
||||||
StaffActionLog.actions.key(object.action).to_s
|
UserHistory.actions.key(object.action).to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
def new_value
|
def new_value
|
@ -7,8 +7,8 @@ class StaffActionLogger
|
|||||||
|
|
||||||
def log_user_deletion(deleted_user, opts={})
|
def log_user_deletion(deleted_user, opts={})
|
||||||
raise Discourse::InvalidParameters.new('user is nil') unless deleted_user and deleted_user.is_a?(User)
|
raise Discourse::InvalidParameters.new('user is nil') unless deleted_user and deleted_user.is_a?(User)
|
||||||
StaffActionLog.create( params(opts).merge({
|
UserHistory.create( params(opts).merge({
|
||||||
action: StaffActionLog.actions[:delete_user],
|
action: UserHistory.actions[:delete_user],
|
||||||
target_user_id: deleted_user.id,
|
target_user_id: deleted_user.id,
|
||||||
email: deleted_user.email,
|
email: deleted_user.email,
|
||||||
ip_address: deleted_user.ip_address,
|
ip_address: deleted_user.ip_address,
|
||||||
@ -20,8 +20,8 @@ class StaffActionLogger
|
|||||||
raise Discourse::InvalidParameters.new('user is nil') unless user and user.is_a?(User)
|
raise Discourse::InvalidParameters.new('user is nil') unless user and user.is_a?(User)
|
||||||
raise Discourse::InvalidParameters.new('old trust level is invalid') unless TrustLevel.levels.values.include? old_trust_level
|
raise Discourse::InvalidParameters.new('old trust level is invalid') unless TrustLevel.levels.values.include? old_trust_level
|
||||||
raise Discourse::InvalidParameters.new('new trust level is invalid') unless TrustLevel.levels.values.include? new_trust_level
|
raise Discourse::InvalidParameters.new('new trust level is invalid') unless TrustLevel.levels.values.include? new_trust_level
|
||||||
StaffActionLog.create!( params(opts).merge({
|
UserHistory.create!( params(opts).merge({
|
||||||
action: StaffActionLog.actions[:change_trust_level],
|
action: UserHistory.actions[:change_trust_level],
|
||||||
target_user_id: user.id,
|
target_user_id: user.id,
|
||||||
details: "old trust level: #{old_trust_level}, new trust level: #{new_trust_level}"
|
details: "old trust level: #{old_trust_level}, new trust level: #{new_trust_level}"
|
||||||
}))
|
}))
|
||||||
@ -29,8 +29,8 @@ class StaffActionLogger
|
|||||||
|
|
||||||
def log_site_setting_change(setting_name, previous_value, new_value, opts={})
|
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)
|
raise Discourse::InvalidParameters.new('setting_name is invalid') unless setting_name.present? and SiteSetting.respond_to?(setting_name)
|
||||||
StaffActionLog.create( params(opts).merge({
|
UserHistory.create( params(opts).merge({
|
||||||
action: StaffActionLog.actions[:change_site_setting],
|
action: UserHistory.actions[:change_site_setting],
|
||||||
subject: setting_name,
|
subject: setting_name,
|
||||||
previous_value: previous_value,
|
previous_value: previous_value,
|
||||||
new_value: new_value
|
new_value: new_value
|
||||||
@ -41,8 +41,8 @@ class StaffActionLogger
|
|||||||
|
|
||||||
def log_site_customization_change(old_record, site_customization_params, opts={})
|
def log_site_customization_change(old_record, site_customization_params, opts={})
|
||||||
raise Discourse::InvalidParameters.new('site_customization_params is nil') unless site_customization_params
|
raise Discourse::InvalidParameters.new('site_customization_params is nil') unless site_customization_params
|
||||||
StaffActionLog.create( params(opts).merge({
|
UserHistory.create( params(opts).merge({
|
||||||
action: StaffActionLog.actions[:change_site_customization],
|
action: UserHistory.actions[:change_site_customization],
|
||||||
subject: site_customization_params[:name],
|
subject: site_customization_params[:name],
|
||||||
previous_value: old_record ? old_record.attributes.slice(*SITE_CUSTOMIZATION_LOGGED_ATTRS).to_json : nil,
|
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
|
new_value: site_customization_params.slice(*(SITE_CUSTOMIZATION_LOGGED_ATTRS.map(&:to_sym))).to_json
|
||||||
@ -51,8 +51,8 @@ class StaffActionLogger
|
|||||||
|
|
||||||
def log_site_customization_destroy(site_customization, opts={})
|
def log_site_customization_destroy(site_customization, opts={})
|
||||||
raise Discourse::InvalidParameters.new('site_customization is nil') unless site_customization
|
raise Discourse::InvalidParameters.new('site_customization is nil') unless site_customization
|
||||||
StaffActionLog.create( params(opts).merge({
|
UserHistory.create( params(opts).merge({
|
||||||
action: StaffActionLog.actions[:delete_site_customization],
|
action: UserHistory.actions[:delete_site_customization],
|
||||||
subject: site_customization.name,
|
subject: site_customization.name,
|
||||||
previous_value: site_customization.attributes.slice(*SITE_CUSTOMIZATION_LOGGED_ATTRS).to_json
|
previous_value: site_customization.attributes.slice(*SITE_CUSTOMIZATION_LOGGED_ATTRS).to_json
|
||||||
}))
|
}))
|
||||||
@ -61,7 +61,7 @@ class StaffActionLogger
|
|||||||
private
|
private
|
||||||
|
|
||||||
def params(opts)
|
def params(opts)
|
||||||
{staff_user_id: @admin.id, context: opts[:context]}
|
{acting_user_id: @admin.id, context: opts[:context]}
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -0,0 +1,17 @@
|
|||||||
|
class RenameStaffActionLogsToUserHistory < ActiveRecord::Migration
|
||||||
|
def up
|
||||||
|
remove_index :staff_action_logs, [:staff_user_id, :id]
|
||||||
|
rename_table :staff_action_logs, :user_histories
|
||||||
|
rename_column :user_histories, :staff_user_id, :acting_user_id
|
||||||
|
execute "ALTER INDEX staff_action_logs_pkey RENAME TO user_histories_pkey"
|
||||||
|
add_index :user_histories, [:acting_user_id, :action, :id]
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
remove_index :user_histories, [:acting_user_id, :action, :id]
|
||||||
|
rename_table :user_histories, :staff_action_logs
|
||||||
|
rename_column :staff_action_logs, :acting_user_id, :staff_user_id
|
||||||
|
execute "ALTER INDEX user_histories_pkey RENAME TO staff_action_logs_pkey"
|
||||||
|
add_index :staff_action_logs, [:staff_user_id, :id]
|
||||||
|
end
|
||||||
|
end
|
@ -1,5 +1,5 @@
|
|||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe StaffActionLog do
|
describe UserHistory do
|
||||||
# Nothing fancy going on in this model. See StaffActionLogger.
|
# Nothing fancy going on in this model. See StaffActionLogger.
|
||||||
end
|
end
|
@ -29,8 +29,8 @@ describe StaffActionLogger do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it 'creates a new StaffActionLog record' do
|
it 'creates a new StaffActionLog record' do
|
||||||
expect { log_user_deletion }.to change { StaffActionLog.count }.by(1)
|
expect { log_user_deletion }.to change { UserHistory.count }.by(1)
|
||||||
StaffActionLog.last.target_user_id.should == deleted_user.id
|
UserHistory.last.target_user_id.should == deleted_user.id
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -57,8 +57,8 @@ describe StaffActionLogger do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it 'creates a new StaffActionLog record' do
|
it 'creates a new StaffActionLog record' do
|
||||||
expect { log_trust_level_change }.to change { StaffActionLog.count }.by(1)
|
expect { log_trust_level_change }.to change { UserHistory.count }.by(1)
|
||||||
StaffActionLog.last.details.should include "new trust level: #{new_trust_level}"
|
UserHistory.last.details.should include "new trust level: #{new_trust_level}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -70,7 +70,7 @@ describe StaffActionLogger do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "creates a new StaffActionLog record" do
|
it "creates a new StaffActionLog record" do
|
||||||
expect { logger.log_site_setting_change('title', 'Discourse', 'My Site') }.to change { StaffActionLog.count }.by(1)
|
expect { logger.log_site_setting_change('title', 'Discourse', 'My Site') }.to change { UserHistory.count }.by(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user