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.rbindex 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 ::DiscourseDataExplorerrequires_plugin PLUGIN_NAMEbefore_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 indexqueries = Query.where(hidden: false).order(:last_run_at, :name).includes(:groups).to_a@@ -83,6 +83,13 @@ module ::DiscourseDataExplorerrunend+ # 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 createquery =Query.create!(diff --git a/plugins/discourse-data-explorer/config/routes.rb b/plugins/discourse-data-explorer/config/routes.rbindex 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 doget "/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"enddiff --git a/plugins/discourse-data-explorer/plugin.rb b/plugins/discourse-data-explorer/plugin.rbindex 12aa8eef098..5273a6bd5af 100644--- a/plugins/discourse-data-explorer/plugin.rb+++ b/plugins/discourse-data-explorer/plugin.rb@@ -68,7 +68,12 @@ after_initialize doadd_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 dodiff --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.rbindex 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" doexpect(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)+ endenddiff --git a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rbindex 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 doexpect(response.status).to eq(404)endend++ 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+ endendend