From 9281b72308c7844396d41d67d61305b93d148236 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Wed, 19 Sep 2018 03:16:45 +0530 Subject: [PATCH] FEATURE: Log entity export in staff logs --- app/controllers/export_csv_controller.rb | 1 + app/models/user_history.rb | 6 ++++-- app/services/staff_action_logger.rb | 8 ++++++++ config/locales/client.en.yml | 1 + spec/requests/export_csv_controller_spec.rb | 18 ++++++++++++++++++ 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/controllers/export_csv_controller.rb b/app/controllers/export_csv_controller.rb index 851d1643681..d55053630af 100644 --- a/app/controllers/export_csv_controller.rb +++ b/app/controllers/export_csv_controller.rb @@ -5,6 +5,7 @@ class ExportCsvController < ApplicationController def export_entity guardian.ensure_can_export_entity!(export_params[:entity]) Jobs.enqueue(:export_csv_file, entity: export_params[:entity], user_id: current_user.id, args: export_params[:args]) + StaffActionLogger.new(current_user).log_entity_export(export_params[:entity]) render json: success_json end diff --git a/app/models/user_history.rb b/app/models/user_history.rb index 0ce8a8da6d8..1aaf9c8dfc4 100644 --- a/app/models/user_history.rb +++ b/app/models/user_history.rb @@ -81,7 +81,8 @@ class UserHistory < ActiveRecord::Base removed_unsilence_user: 62, removed_unsuspend_user: 63, post_rejected: 64, - merge_user: 65 + merge_user: 65, + entity_export: 66 ) end @@ -141,7 +142,8 @@ class UserHistory < ActiveRecord::Base :change_badge, :delete_badge, :post_rejected, - :merge_user + :merge_user, + :entity_export ] end diff --git a/app/services/staff_action_logger.rb b/app/services/staff_action_logger.rb index aa2338b305f..6ebb5af059d 100644 --- a/app/services/staff_action_logger.rb +++ b/app/services/staff_action_logger.rb @@ -459,6 +459,14 @@ class StaffActionLogger )) end + def log_entity_export(entity, opts = {}) + UserHistory.create!(params(opts).merge( + action: UserHistory.actions[:entity_export], + ip_address: @admin.ip_address.to_s, + subject: entity + )) + end + def log_backup_download(backup, opts = {}) raise Discourse::InvalidParameters.new(:backup) unless backup UserHistory.create!(params(opts).merge( diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 808cc1cc9a4..15e00f8d888 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -3522,6 +3522,7 @@ en: change_badge: "change badge" delete_badge: "delete badge" merge_user: "merge user" + entity_export: "export entity" 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/requests/export_csv_controller_spec.rb b/spec/requests/export_csv_controller_spec.rb index 1fe2a15e7c5..52c19a0ccbd 100644 --- a/spec/requests/export_csv_controller_spec.rb +++ b/spec/requests/export_csv_controller_spec.rb @@ -30,6 +30,15 @@ describe ExportCsvController do expect(response).to be_forbidden expect(Jobs::ExportCsvFile.jobs.size).to eq(0) end + + it "correctly logs the entity export" do + post "/export_csv/export_entity.json", params: { entity: "user_archive" } + + log_entry = UserHistory.last + expect(log_entry.action).to eq(UserHistory.actions[:entity_export]) + expect(log_entry.acting_user_id).to eq(user.id) + expect(log_entry.subject).to eq("user_archive") + end end end @@ -58,6 +67,15 @@ describe ExportCsvController do expect(job_data["entity"]).to eq("staff_action") expect(job_data["user_id"]).to eq(admin.id) end + + it "correctly logs the entity export" do + post "/export_csv/export_entity.json", params: { entity: "user_list" } + + log_entry = UserHistory.last + expect(log_entry.action).to eq(UserHistory.actions[:entity_export]) + expect(log_entry.acting_user_id).to eq(admin.id) + expect(log_entry.subject).to eq("user_list") + end end end end