Skip to content

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

DEV: new API endpoint for data explorer query running (#34561)

Sam0584a135174b
diff --git a/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb b/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb
index a395dcf0fc5..0092822e3cc 100644
--- a/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb
+++ b/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb
@@ -5,12 +5,12 @@ module ::DiscourseDataExplorer
requires_plugin PLUGIN_NAME
before_action :set_group, only: %i[group_reports_index group_reports_show group_reports_run]
- before_action :set_query, only: %i[group_reports_show group_reports_run show update]
+ before_action :set_query, only: %i[group_reports_show group_reports_run show update public_run]
before_action :ensure_admin
- skip_before_action :check_xhr, only: %i[show group_reports_run run]
+ skip_before_action :check_xhr, only: %i[show group_reports_run run public_run]
skip_before_action :ensure_admin,
- only: %i[group_reports_index group_reports_show group_reports_run]
+ only: %i[group_reports_index group_reports_show group_reports_run public_run]
def index
queries = Query.where(hidden: false).order(:last_run_at, :name).includes(:groups).to_a
@@ -83,6 +83,13 @@ module ::DiscourseDataExplorer
run
end
+ # Public GET endpoint to run a query by ID for users with access
+ def public_run
+ return raise Discourse::NotFound if !guardian.user_can_access_query?(@query) || @query.hidden
+
+ run
+ end
+
def create
query =
Query.create!(
diff --git a/plugins/discourse-data-explorer/config/routes.rb b/plugins/discourse-data-explorer/config/routes.rb
index d3434be4184..f0e11a76376 100644
--- a/plugins/discourse-data-explorer/config/routes.rb
+++ b/plugins/discourse-data-explorer/config/routes.rb
@@ -20,5 +20,11 @@ Discourse::Application.routes.draw do
get "/g/:group_name/reports/:id" => "discourse_data_explorer/query#group_reports_show"
post "/g/:group_name/reports/:id/run" => "discourse_data_explorer/query#group_reports_run"
+ # Public API to fetch query results via GET with permission checks
+ get "/data-explorer/queries/:id/run" => "discourse_data_explorer/query#public_run",
+ :constraints => {
+ format: /(json|csv)/,
+ }
+
mount DiscourseDataExplorer::Engine, at: "/admin/plugins/explorer"
end
diff --git a/plugins/discourse-data-explorer/plugin.rb b/plugins/discourse-data-explorer/plugin.rb
index 12aa8eef098..5273a6bd5af 100644
--- a/plugins/discourse-data-explorer/plugin.rb
+++ b/plugins/discourse-data-explorer/plugin.rb
@@ -68,7 +68,12 @@ after_initialize do
add_api_key_scope(
:discourse_data_explorer,
- { run_queries: { actions: %w[discourse_data_explorer/query#run], params: %i[id] } },
+ {
+ run_queries: {
+ actions: %w[discourse_data_explorer/query#run discourse_data_explorer/query#public_run],
+ params: %i[id],
+ },
+ },
)
reloadable_patch do
diff --git a/plugins/discourse-data-explorer/spec/integration/custom_api_key_scopes_spec.rb b/plugins/discourse-data-explorer/spec/integration/custom_api_key_scopes_spec.rb
index dcf546f6d27..9f311103cfe 100644
--- a/plugins/discourse-data-explorer/spec/integration/custom_api_key_scopes_spec.rb
+++ b/plugins/discourse-data-explorer/spec/integration/custom_api_key_scopes_spec.rb
@@ -109,4 +109,26 @@ describe "API keys scoped to query#run" do
expect(response.parsed_body["success"]).to eq(true)
expect(response.parsed_body["columns"]).to eq(["query2_res"])
end
+
+ it "can run queries via GET public route when allowed" do
+ expect {
+ get "/data-explorer/queries/#{query1.id}/run.json",
+ headers: {
+ "Api-Key" => single_query_api_key.key,
+ "Api-Username" => admin.username,
+ }
+ }.to change { query1.reload.last_run_at }
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["success"]).to eq(true)
+ expect(response.parsed_body["columns"]).to eq(["query1_res"])
+
+ expect {
+ get "/data-explorer/queries/#{query2.id}/run.json",
+ headers: {
+ "Api-Key" => single_query_api_key.key,
+ "Api-Username" => admin.username,
+ }
+ }.not_to change { query2.reload.last_run_at }
+ expect(response.status).to eq(403)
+ end
end
diff --git a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb
index 830ea55b9bf..fa043522d62 100644
--- a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb
+++ b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb
@@ -571,5 +571,35 @@ describe DiscourseDataExplorer::QueryController do
expect(response.status).to eq(404)
end
end
+
+ describe "GET /data-explorer/queries/:id/run.json (public_run)" do
+ it "runs the query for a user with access" do
+ group.add(user)
+ query = make_query("SELECT 1828 as value", { name: "B" }, [group.id.to_s])
+
+ get "/data-explorer/queries/#{query.id}/run.json"
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["success"]).to eq(true)
+ expect(response.parsed_body["columns"]).to eq(["value"])
+ expect(response.parsed_body["rows"]).to eq([[1828]])
+ end
+
+ it "returns 404 when the user does not have access" do
+ # query restricted to another group, user is not a member
+ other_group = Fabricate(:group)
+ query = make_query("SELECT 1 as value", {}, [other_group.id.to_s])
+
+ get "/data-explorer/queries/#{query.id}/run.json"
+ expect(response.status).to eq(404)
+ end
+
+ it "returns 404 when the query is hidden" do
+ group.add(user)
+ query = make_query("SELECT 1 as value", { hidden: true }, [group.id.to_s])
+
+ get "/data-explorer/queries/#{query.id}/run.json"
+ expect(response.status).to eq(404)
+ end
+ end
end
end