From 74f22f95da49709d6ec09eca8f23645c9ac7bc16 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Wed, 27 Jan 2016 15:08:16 +0530 Subject: [PATCH] FEATURE: log admin/moderator grant/revoke action --- app/controllers/admin/users_controller.rb | 4 +++ app/models/user_history.rb | 12 +++++++-- app/services/staff_action_logger.rb | 32 +++++++++++++++++++++++ config/locales/client.en.yml | 4 +++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index dacf04175..36aa18f19 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -87,6 +87,7 @@ class Admin::UsersController < Admin::AdminController def revoke_admin guardian.ensure_can_revoke_admin!(@user) @user.revoke_admin! + StaffActionLogger.new(current_user).log_revoke_admin(@user) render nothing: true end @@ -103,18 +104,21 @@ class Admin::UsersController < Admin::AdminController def grant_admin guardian.ensure_can_grant_admin!(@user) @user.grant_admin! + StaffActionLogger.new(current_user).log_grant_admin(@user) render_serialized(@user, AdminUserSerializer) end def revoke_moderation guardian.ensure_can_revoke_moderation!(@user) @user.revoke_moderation! + StaffActionLogger.new(current_user).log_revoke_moderation(@user) render nothing: true end def grant_moderation guardian.ensure_can_grant_moderation!(@user) @user.grant_moderation! + StaffActionLogger.new(current_user).log_grant_moderation(@user) render_serialized(@user, AdminUserSerializer) end diff --git a/app/models/user_history.rb b/app/models/user_history.rb index 252b5cc5b..e742631f3 100644 --- a/app/models/user_history.rb +++ b/app/models/user_history.rb @@ -46,7 +46,11 @@ class UserHistory < ActiveRecord::Base create_category: 28, change_site_text: 29, block_user: 30, - unblock_user: 31) + unblock_user: 31, + grant_admin: 32, + revoke_admin: 33, + grant_moderation: 34, + revoke_moderation: 35) end # Staff actions is a subset of all actions, used to audit actions taken by staff users. @@ -74,7 +78,11 @@ class UserHistory < ActiveRecord::Base :delete_category, :create_category, :block_user, - :unblock_user] + :unblock_user, + :grant_admin, + :revoke_admin, + :grant_moderation, + :revoke_moderation] end def self.staff_action_ids diff --git a/app/services/staff_action_logger.rb b/app/services/staff_action_logger.rb index a6354ecb2..24ce83718 100644 --- a/app/services/staff_action_logger.rb +++ b/app/services/staff_action_logger.rb @@ -294,6 +294,38 @@ class StaffActionLogger })) end + def log_grant_admin(user, opts={}) + raise Discourse::InvalidParameters.new(:user) unless user + UserHistory.create( params(opts).merge({ + action: UserHistory.actions[:grant_admin], + target_user_id: user.id + })) + end + + def log_revoke_admin(user, opts={}) + raise Discourse::InvalidParameters.new(:user) unless user + UserHistory.create( params(opts).merge({ + action: UserHistory.actions[:revoke_admin], + target_user_id: user.id + })) + end + + def log_grant_moderation(user, opts={}) + raise Discourse::InvalidParameters.new(:user) unless user + UserHistory.create( params(opts).merge({ + action: UserHistory.actions[:grant_moderation], + target_user_id: user.id + })) + end + + def log_revoke_moderation(user, opts={}) + raise Discourse::InvalidParameters.new(:user) unless user + UserHistory.create( params(opts).merge({ + action: UserHistory.actions[:revoke_moderation], + target_user_id: user.id + })) + end + private def params(opts=nil) diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 2de25cbf4..e9660428b 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2329,6 +2329,10 @@ en: create_category: "create category" block_user: "block user" unblock_user: "unblock user" + grant_admin: "grant admin" + revoke_admin: "revoke admin" + grant_moderation: "grant moderation" + revoke_moderation: "revoke moderation" screened_emails: title: "Screened Emails" description: "When someone tries to create a new account, the following email addresses will be checked and the registration will be blocked, or some other action performed."