From 515f50e42e2dd3be83b7bf09c24c3dafb4a04b3c Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 11 Jan 2017 14:46:48 +0800 Subject: [PATCH] FEATURE: Log admin action when readonly mode is changed. --- app/controllers/admin/backups_controller.rb | 2 ++ app/models/user_history.rb | 6 ++++-- app/services/staff_action_logger.rb | 9 ++++++++- config/locales/client.en.yml | 1 + .../admin/backups_controller_spec.rb | 16 +++++++++++++-- spec/services/staff_action_logger_spec.rb | 20 +++++++++++++++++++ 6 files changed, 49 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin/backups_controller.rb b/app/controllers/admin/backups_controller.rb index 2c71943b488..feed2a709d1 100644 --- a/app/controllers/admin/backups_controller.rb +++ b/app/controllers/admin/backups_controller.rb @@ -103,6 +103,8 @@ class Admin::BackupsController < Admin::AdminController Discourse.disable_readonly_mode(readonly_mode_key) end + StaffActionLogger.new(current_user).log_change_readonly_mode(enable) + render nothing: true end diff --git a/app/models/user_history.rb b/app/models/user_history.rb index e7db96a5f64..ddc48286630 100644 --- a/app/models/user_history.rb +++ b/app/models/user_history.rb @@ -58,7 +58,8 @@ class UserHistory < ActiveRecord::Base wizard_step: 40, lock_trust_level: 41, unlock_trust_level: 42, - activate_user: 43 + activate_user: 43, + change_readonly_mode: 44 ) end @@ -97,7 +98,8 @@ class UserHistory < ActiveRecord::Base :deactivate_user, :lock_trust_level, :unlock_trust_level, - :activate_user] + :activate_user, + :change_readonly_mode] end def self.staff_action_ids diff --git a/app/services/staff_action_logger.rb b/app/services/staff_action_logger.rb index 9efe294e989..c872fdd0f6b 100644 --- a/app/services/staff_action_logger.rb +++ b/app/services/staff_action_logger.rb @@ -373,11 +373,18 @@ class StaffActionLogger raise Discourse::InvalidParameters.new(:step) unless step UserHistory.create(params(opts).merge({ action: UserHistory.actions[:wizard_step], - acting_user_id: @admin.id, context: step.id })) end + def log_change_readonly_mode(state) + UserHistory.create(params.merge({ + action: UserHistory.actions[:change_readonly_mode], + previous_value: !state, + new_value: state + })) + end + private def params(opts=nil) diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index bbe0aab2c83..0620de21f37 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2919,6 +2919,7 @@ en: unlock_trust_level: "unlock trust level" activate_user: "activate user" deactivate_user: "deactivate user" + change_readonly_mode: "change readonly mode" 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." diff --git a/spec/controllers/admin/backups_controller_spec.rb b/spec/controllers/admin/backups_controller_spec.rb index 080f4eb6c35..270dc9a2317 100644 --- a/spec/controllers/admin/backups_controller_spec.rb +++ b/spec/controllers/admin/backups_controller_spec.rb @@ -179,17 +179,29 @@ describe Admin::BackupsController do it "enables readonly mode" do Discourse.expects(:enable_readonly_mode) - xhr :put, :readonly, enable: true + expect { xhr :put, :readonly, enable: true } + .to change { UserHistory.count }.by(1) expect(response).to be_success + + user_history = UserHistory.last + + expect(UserHistory.last.action).to eq(UserHistory.actions[:change_readonly_mode]) + expect(UserHistory.last.new_value).to eq('t') end it "disables readonly mode" do Discourse.expects(:disable_readonly_mode) - xhr :put, :readonly, enable: false + expect { xhr :put, :readonly, enable: false } + .to change { UserHistory.count }.by(1) expect(response).to be_success + + user_history = UserHistory.last + + expect(UserHistory.last.action).to eq(UserHistory.actions[:change_readonly_mode]) + expect(UserHistory.last.new_value).to eq('f') end end diff --git a/spec/services/staff_action_logger_spec.rb b/spec/services/staff_action_logger_spec.rb index 773f990d104..df2e4c6a9ca 100644 --- a/spec/services/staff_action_logger_spec.rb +++ b/spec/services/staff_action_logger_spec.rb @@ -370,6 +370,7 @@ describe StaffActionLogger do end end +<<<<<<< 240c4870cfa43dea222ed52d4ce57b6ac186588d describe 'log_lock_trust_level' do let(:user) { Fabricate(:user) } @@ -405,6 +406,25 @@ describe StaffActionLogger do user_history = UserHistory.last expect(user_history.action).to eq(UserHistory.actions[:activate_user]) expect(user_history.details).to eq(reason) + end + + describe '#log_readonly_mode' do + it "creates a new record" do + expect { logger.log_change_readonly_mode(true) }.to change { UserHistory.count }.by(1) + + user_history = UserHistory.last + + expect(user_history.action).to eq(UserHistory.actions[:change_readonly_mode]) + expect(user_history.new_value).to eq('t') + expect(user_history.previous_value).to eq('f') + + expect { logger.log_change_readonly_mode(false) }.to change { UserHistory.count }.by(1) + + user_history = UserHistory.last + + expect(user_history.action).to eq(UserHistory.actions[:change_readonly_mode]) + expect(user_history.new_value).to eq('f') + expect(user_history.previous_value).to eq('t') end end end