A platform for community discussion. Free, open, simple.
FIX: align staff action CSV export with log visibility rules (#40177)
Roman Rizzic396822ed0c2
diff --git a/app/jobs/regular/export_csv_file.rb b/app/jobs/regular/export_csv_file.rbindex 739e21ed0d3..ecc304c83b9 100644--- a/app/jobs/regular/export_csv_file.rb+++ b/app/jobs/regular/export_csv_file.rb@@ -353,6 +353,7 @@ module Jobsdef get_staff_action_fields(staff_action)staff_action_array = []+ can_see_content = staff_action_log_guardian.can_see_staff_action_log_content?(staff_action)HEADER_ATTRS_FOR["staff_action"].each do |attr|data =@@ -368,6 +369,8 @@ module Jobselse"#{user.username} #{staff_action.attributes[attr]}"end+ elsif %w[details context].include?(attr) && !can_see_content+ attr == "details" ? I18n.t("staff_action_logs.redacted") : nilelsestaff_action.attributes[attr]end@@ -429,6 +432,10 @@ module Jobsscreened_url_arrayend+ def staff_action_log_guardian+ @staff_action_log_guardian ||= Guardian.new(@current_user)+ end+def notify_user(upload, export_title)post = nildiff --git a/spec/jobs/export_csv_file_spec.rb b/spec/jobs/export_csv_file_spec.rbindex a113464902c..9c3c483efe2 100644--- a/spec/jobs/export_csv_file_spec.rb+++ b/spec/jobs/export_csv_file_spec.rb@@ -139,6 +139,44 @@ RSpec.describe Jobs::ExportCsvFile doendendend++ it "redacts details and context for moderators who cannot see the log content" do+ category = Fabricate(:private_category, group: Fabricate(:group))+ topic = Fabricate(:topic, category: category)+ post = Fabricate(:post, topic: topic)+ moderator = Fabricate(:moderator)+ action_log =+ StaffActionLogger.new(admin).log_post_edit(+ post,+ old_raw: "#{post.raw} old",+ context: "secret context",+ )++ Jobs::ExportCsvFile.new.execute(user_id: moderator.id, entity: "staff_action")++ row = parse_staff_action_rows(Upload.last).find { |r| r["action"] == "post_edit" }+ expect(row).to be_present+ expect(row["details"]).to eq(I18n.t("staff_action_logs.redacted"))+ expect(row["context"]).to be_blank++ action_log.destroy!+ end++ it "does not redact details and context for admins" do+ post = Fabricate(:post)+ StaffActionLogger.new(admin).log_post_edit(+ post,+ old_raw: "#{post.raw} old",+ context: "visible context",+ )++ Jobs::ExportCsvFile.new.execute(user_id: admin.id, entity: "staff_action")++ row = parse_staff_action_rows(Upload.last).find { |r| r["action"] == "post_edit" }+ expect(row).to be_present+ expect(row["details"]).to include("---")+ expect(row["context"]).to include("visible context")+ endendend@@ -366,6 +404,17 @@ RSpec.describe Jobs::ExportCsvFile doHash[*user_list_header.zip(row).flatten]end+ def parse_staff_action_rows(upload)+ rows = []+ Zip::File.open(Discourse.store.path_for(upload)) do |zip_file|+ zip_file.each do |entry|+ csv_rows = CSV.parse(zip_file.read(entry), headers: true)+ rows.concat(csv_rows.map(&:to_h))+ end+ end+ rows+ end+it "exports secondary emails" douser = Fabricate(:user)Fabricate(:secondary_email, user: user, primary: false)