Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Add admin-only reports and centralize report visibility logic

zogstrip5e99b520079e
diff --git a/app/controllers/admin/reports_controller.rb b/app/controllers/admin/reports_controller.rb
index 5266a552194..77a64c2761e 100644
--- a/app/controllers/admin/reports_controller.rb
+++ b/app/controllers/admin/reports_controller.rb
@@ -4,7 +4,7 @@ class Admin::ReportsController < Admin::StaffController
REPORTS_LIMIT = 50
def index
- render_json_dump(reports: Reports::ListQuery.call)
+ render_json_dump(reports: Reports::ListQuery.call(admin: current_user.admin?))
end
def bulk
@@ -17,16 +17,9 @@ class Admin::ReportsController < Admin::StaffController
report = nil
report = Report.find_cached(report_type, args) if (report_params[:cache])
- if SiteSetting.use_legacy_pageviews
- if Report::HIDDEN_PAGEVIEW_REPORTS.include?(report_type)
- report = Report._get(report_type, args)
- report.error = :not_found
- end
- else
- if Report::HIDDEN_LEGACY_PAGEVIEW_REPORTS.include?(report_type)
- report = Report._get(report_type, args)
- report.error = :not_found
- end
+ if Report.hidden?(report_type, admin: current_user.admin?)
+ report = Report._get(report_type, args)
+ report.error = :not_found
end
if report
@@ -53,12 +46,7 @@ class Admin::ReportsController < Admin::StaffController
report_type = params[:type]
raise Discourse::NotFound unless report_type =~ /\A[a-z0-9\_]+\z/
-
- if SiteSetting.use_legacy_pageviews
- raise Discourse::NotFound if Report::HIDDEN_PAGEVIEW_REPORTS.include?(report_type)
- else
- raise Discourse::NotFound if Report::HIDDEN_LEGACY_PAGEVIEW_REPORTS.include?(report_type)
- end
+ raise Discourse::NotFound if Report.hidden?(report_type, admin: current_user.admin?)
args = parse_params(params)
diff --git a/app/controllers/admin/search_controller.rb b/app/controllers/admin/search_controller.rb
index c0d0a9f5509..4157b86b12e 100644
--- a/app/controllers/admin/search_controller.rb
+++ b/app/controllers/admin/search_controller.rb
@@ -19,7 +19,7 @@ class Admin::SearchController < Admin::AdminController
),
themes_and_components:
serialize_data(Theme.include_relations.order(:name), BasicThemeSerializer),
- reports: Reports::ListQuery.call,
+ reports: Reports::ListQuery.call(admin: current_user.admin?),
)
end
diff --git a/app/models/report.rb b/app/models/report.rb
index f134d4e24f4..57c51399064 100644
--- a/app/models/report.rb
+++ b/app/models/report.rb
@@ -35,6 +35,15 @@ class Report
page_view_logged_in_reqs
]
+ ADMIN_ONLY_REPORTS = %w[top_uploads]
+
+ def self.hidden?(type, admin:)
+ return true if !admin && ADMIN_ONLY_REPORTS.include?(type)
+ hidden_reports =
+ SiteSetting.use_legacy_pageviews ? HIDDEN_PAGEVIEW_REPORTS : HIDDEN_LEGACY_PAGEVIEW_REPORTS
+ hidden_reports.include?(type)
+ end
+
COLORS = {
turquoise: "#1EB8D1",
lime: "#9BC53D",
diff --git a/app/queries/reports/list_query.rb b/app/queries/reports/list_query.rb
index 981983fc490..71aeeeb9479 100644
--- a/app/queries/reports/list_query.rb
+++ b/app/queries/reports/list_query.rb
@@ -10,8 +10,9 @@ module Reports
@type = name.to_s.gsub("report_", "")
end
- def to_h
- return if skip_report?
+ def to_h(admin:)
+ return if Report.hidden?(type, admin:)
+
{
type:,
title:,
@@ -22,11 +23,6 @@ module Reports
private
- def skip_report?
- (SiteSetting.use_legacy_pageviews && type.in?(Report::HIDDEN_PAGEVIEW_REPORTS)) ||
- (!SiteSetting.use_legacy_pageviews && type.in?(Report::HIDDEN_LEGACY_PAGEVIEW_REPORTS))
- end
-
# HACK: We need to show a different label and description for some
# old reports while people are still relying on them, that lets us
# point toward the new 'Site traffic' report as well. Not ideal,
@@ -47,7 +43,7 @@ module Reports
end
end
- def self.call
+ def self.call(admin:)
page_view_req_report_methods =
["page_view_total_reqs"] +
ApplicationRequest
@@ -65,7 +61,9 @@ module Reports
Report.singleton_methods.grep(/\Areport_(?!about|storage_stats)/)
reports_methods
- .filter_map { |report_name| Reports::ListQuery::FormattedReport.new(report_name).to_h }
+ .filter_map do |report_name|
+ Reports::ListQuery::FormattedReport.new(report_name).to_h(admin:)
+ end
.sort_by { |report| report[:title] }
end
end
diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb
index aa8e24cbe2e..27e0fd0bf2f 100644
--- a/spec/models/report_spec.rb
+++ b/spec/models/report_spec.rb
@@ -1950,4 +1950,62 @@ RSpec.describe Report do
end
end
end
+
+ describe ".hidden?" do
+ context "when admin is true" do
+ it "returns false for regular reports" do
+ expect(Report.hidden?("topics", admin: true)).to eq(false)
+ end
+
+ it "returns false for admin-only reports" do
+ Report::ADMIN_ONLY_REPORTS.each do |report_type|
+ expect(Report.hidden?(report_type, admin: true)).to eq(false)
+ end
+ end
+ end
+
+ context "when admin is false" do
+ it "returns false for regular reports" do
+ expect(Report.hidden?("topics", admin: false)).to eq(false)
+ end
+
+ it "returns true for admin-only reports" do
+ Report::ADMIN_ONLY_REPORTS.each do |report_type|
+ expect(Report.hidden?(report_type, admin: false)).to eq(true)
+ end
+ end
+ end
+
+ context "when use_legacy_pageviews is true" do
+ before { SiteSetting.use_legacy_pageviews = true }
+
+ it "hides pageview reports" do
+ Report::HIDDEN_PAGEVIEW_REPORTS.each do |report_type|
+ expect(Report.hidden?(report_type, admin: true)).to eq(true)
+ end
+ end
+
+ it "does not hide legacy pageview reports" do
+ Report::HIDDEN_LEGACY_PAGEVIEW_REPORTS.each do |report_type|
+ expect(Report.hidden?(report_type, admin: true)).to eq(false)
+ end
+ end
+ end
+
+ context "when use_legacy_pageviews is false" do
+ before { SiteSetting.use_legacy_pageviews = false }
+
+ it "does not hide pageview reports" do
+ Report::HIDDEN_PAGEVIEW_REPORTS.each do |report_type|
+ expect(Report.hidden?(report_type, admin: true)).to eq(false)
+ end
+ end
+
+ it "hides legacy pageview reports" do
+ Report::HIDDEN_LEGACY_PAGEVIEW_REPORTS.each do |report_type|
+ expect(Report.hidden?(report_type, admin: true)).to eq(true)
+ end
+ end
+ end
+ end
end
diff --git a/spec/queries/reports/list_query_spec.rb b/spec/queries/reports/list_query_spec.rb
index 9a79eb9e473..f53f84db683 100644
--- a/spec/queries/reports/list_query_spec.rb
+++ b/spec/queries/reports/list_query_spec.rb
@@ -2,9 +2,7 @@
RSpec.describe Reports::ListQuery do
describe ".call" do
- subject(:result) { described_class.call }
-
- fab!(:current_user, :admin)
+ subject(:result) { described_class.call(admin: true) }
let(:result_page_view_report_types) do
result.filter { |r| r[:type].starts_with?("page_view") }.map { |r| r[:type] }
@@ -84,5 +82,21 @@ RSpec.describe Reports::ListQuery do
)
end
end
+
+ context "when admin is true" do
+ subject(:result) { described_class.call(admin: true) }
+
+ it "includes admin-only reports" do
+ expect(result.map { |r| r[:type] }).to include(*Report::ADMIN_ONLY_REPORTS)
+ end
+ end
+
+ context "when admin is false" do
+ subject(:result) { described_class.call(admin: false) }
+
+ it "excludes admin-only reports" do
+ expect(result.map { |r| r[:type] }).not_to include(*Report::ADMIN_ONLY_REPORTS)
+ end
+ end
end
end
diff --git a/spec/requests/admin/reports_controller_spec.rb b/spec/requests/admin/reports_controller_spec.rb
index 7627d274901..f24de064bf0 100644
--- a/spec/requests/admin/reports_controller_spec.rb
+++ b/spec/requests/admin/reports_controller_spec.rb
@@ -6,11 +6,33 @@ RSpec.describe Admin::ReportsController do
fab!(:user)
describe "#index" do
+ context "when logged in as an admin" do
+ before { sign_in(admin) }
+
+ it "includes admin-only reports" do
+ get "/admin/reports.json"
+ expect(response.parsed_body["reports"].map { |r| r["type"] }).to include(
+ *Report::ADMIN_ONLY_REPORTS,
+ )
+ end
+ end
+
+ context "when logged in as a moderator" do
+ before { sign_in(moderator) }
+
+ it "excludes admin-only reports" do
+ get "/admin/reports.json"
+ expect(response.parsed_body["reports"].map { |r| r["type"] }).not_to include(
+ *Report::ADMIN_ONLY_REPORTS,
+ )
+ end
+ end
+
before { sign_in(admin) }
it "excludes page view mobile reports" do
get "/admin/reports.json"
- expect(response.parsed_body["reports"].map { |r| r[:type] }).not_to include(
+ expect(response.parsed_body["reports"].map { |r| r["type"] }).not_to include(
"page_view_anon_browser_mobile_reqs",
"page_view_logged_in_browser_mobile_reqs",
"page_view_anon_mobile_reqs",
@@ -20,7 +42,7 @@ RSpec.describe Admin::ReportsController do
it "excludes about and storage stats reports" do
get "/admin/reports.json"
- expect(response.parsed_body["reports"].map { |r| r[:type] }).not_to include(
+ expect(response.parsed_body["reports"].map { |r| r["type"] }).not_to include(
"report_about",
"report_storage_stats",
)
@@ -177,6 +199,25 @@ RSpec.describe Admin::ReportsController do
expect(response.status).to eq(200)
expect(response.parsed_body["reports"].count).to eq(2)
end
+
+ it "marks admin-only reports as not_found" do
+ get "/admin/reports/bulk.json",
+ params: {
+ reports: {
+ topics: {
+ limit: 10,
+ },
+ top_uploads: {
+ limit: 10,
+ },
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["reports"].count).to eq(2)
+ expect(response.parsed_body["reports"][0]["type"]).to eq("topics")
+ expect(response.parsed_body["reports"][1]).to include("error" => "not_found", "data" => nil)
+ end
end
context "when logged in as a non-staff user" do
@@ -365,6 +406,13 @@ RSpec.describe Admin::ReportsController do
expect(response.status).to eq(200)
expect(response.parsed_body["report"]["total"]).to eq(1)
end
+
+ it "does not allow accessing admin-only reports" do
+ get "/admin/reports/top_uploads.json"
+
+ expect(response.status).to eq(404)
+ expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
+ end
end
context "when logged in as a non-staff user" do