diff --git a/app/controllers/admin/site_texts_controller.rb b/app/controllers/admin/site_texts_controller.rb index 17c07262db3..d228229512f 100644 --- a/app/controllers/admin/site_texts_controller.rb +++ b/app/controllers/admin/site_texts_controller.rb @@ -44,6 +44,8 @@ class Admin::SiteTextsController < Admin::AdminController def update site_text = find_site_text site_text[:value] = params[:site_text][:value] + old_text = I18n.t(site_text[:id]) + StaffActionLogger.new(current_user).log_site_text_change(site_text[:id], site_text[:value], old_text) TranslationOverride.upsert!(I18n.locale, site_text[:id], site_text[:value]) render_serialized(site_text, SiteTextSerializer, root: 'site_text', rest_serializer: true) @@ -51,8 +53,10 @@ class Admin::SiteTextsController < Admin::AdminController def revert site_text = find_site_text + old_text = I18n.t(site_text[:id]) TranslationOverride.revert!(I18n.locale, site_text[:id]) site_text = find_site_text + StaffActionLogger.new(current_user).log_site_text_change(site_text[:id], site_text[:value], old_text) render_serialized(site_text, SiteTextSerializer, root: 'site_text', rest_serializer: true) end diff --git a/app/models/user_history.rb b/app/models/user_history.rb index 038df0e9c2e..978ca6ee380 100644 --- a/app/models/user_history.rb +++ b/app/models/user_history.rb @@ -21,6 +21,7 @@ class UserHistory < ActiveRecord::Base :change_site_setting, :change_site_customization, :delete_site_customization, + :change_site_text, :checked_for_custom_avatar, # not used anymore :notified_about_avatar, :notified_about_sequential_replies, @@ -53,6 +54,7 @@ class UserHistory < ActiveRecord::Base :change_site_setting, :change_site_customization, :delete_site_customization, + :change_site_text, :suspend_user, :unsuspend_user, :grant_badge, diff --git a/app/services/staff_action_logger.rb b/app/services/staff_action_logger.rb index f760aa5fdd1..c0766551024 100644 --- a/app/services/staff_action_logger.rb +++ b/app/services/staff_action_logger.rb @@ -134,6 +134,18 @@ class StaffActionLogger })) end + def log_site_text_change(subject, new_text, old_text, opts={}) + raise Discourse::InvalidParameters.new(:subject) unless subject.present? + raise Discourse::InvalidParameters.new(:new_text) unless new_text.present? + raise Discourse::InvalidParameters.new(:old_text) unless old_text.present? + UserHistory.create( params(opts).merge({ + action: UserHistory.actions[:change_site_text], + subject: subject, + previous_value: old_text, + new_value: new_text + })) + end + def log_username_change(user, old_username, new_username, opts={}) raise Discourse::InvalidParameters.new(:user) unless user UserHistory.create( params(opts).merge({ diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index b44e809b708..c0526c7383a 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2253,6 +2253,7 @@ en: change_site_setting: "change site setting" change_site_customization: "change site customization" delete_site_customization: "delete site customization" + change_site_text: "change site text" suspend_user: "suspend user" unsuspend_user: "unsuspend user" grant_badge: "grant badge" diff --git a/spec/controllers/admin/site_texts_controller_spec.rb b/spec/controllers/admin/site_texts_controller_spec.rb index 1ec7ad1aa7b..557646000fb 100644 --- a/spec/controllers/admin/site_texts_controller_spec.rb +++ b/spec/controllers/admin/site_texts_controller_spec.rb @@ -75,6 +75,11 @@ describe Admin::SiteTextsController do xhr :put, :update, id: 'made_up_no_key_exists', site_text: {value: 'hello'} expect(response).not_to be_success end + + it 'logs the change' do + StaffActionLogger.any_instance.expects(:log_site_text_change).once + xhr :put, :update, id: 'title', site_text: {value: 'hello'} + end end end diff --git a/spec/services/staff_action_logger_spec.rb b/spec/services/staff_action_logger_spec.rb index b6f93bc3359..d1c7422c7cf 100644 --- a/spec/services/staff_action_logger_spec.rb +++ b/spec/services/staff_action_logger_spec.rb @@ -173,6 +173,18 @@ describe StaffActionLogger do end end + describe "log_site_text_change" do + it "raises an error when params are invalid" do + expect { logger.log_site_text_change(nil, 'new text', 'old text') }.to raise_error(Discourse::InvalidParameters) + expect { logger.log_site_text_change('created', nil, 'old text') }.to raise_error(Discourse::InvalidParameters) + expect { logger.log_site_text_change('created', 'new text', nil) }.to raise_error(Discourse::InvalidParameters) + end + + it "creates a new UserHistory record" do + expect { logger.log_site_text_change('created', 'new text', 'old text') }.to change { UserHistory.count }.by(1) + end + end + describe "log_user_suspend" do let(:user) { Fabricate(:user, suspended_at: 10.minutes.ago, suspended_till: 1.day.from_now) }