Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix can't drill SQL questions in embedding SDK (#64083)

Mahatthana (Kelvin) Nomsawadid848a88e645b
diff --git a/e2e/test-component/scenarios/embedding-sdk/interactive-question.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/interactive-question.cy.spec.tsx
new file mode 100644
index 00000000000..8f049f985b5
--- /dev/null
+++ b/e2e/test-component/scenarios/embedding-sdk/interactive-question.cy.spec.tsx
@@ -0,0 +1,50 @@
+const { H } = cy;
+import { InteractiveQuestion } from "@metabase/embedding-sdk-react";
+
+import { getSdkRoot } from "e2e/support/helpers/e2e-embedding-sdk-helpers";
+import { mountSdkContent } from "e2e/support/helpers/embedding-sdk-component-testing/component-embedding-sdk-helpers";
+import { signInAsAdminAndEnableEmbeddingSdk } from "e2e/support/helpers/embedding-sdk-testing";
+import { mockAuthProviderAndJwtSignIn } from "e2e/support/helpers/embedding-sdk-testing/embedding-sdk-helpers";
+
+describe("scenarios > embedding-sdk > interactive-question", () => {
+ beforeEach(() => {
+ signInAsAdminAndEnableEmbeddingSdk();
+
+ H.createNativeQuestion(
+ {
+ name: "SQL Orders",
+ native: { query: "select * from ORDERS limit 5" },
+ },
+ {
+ wrapId: true,
+ idAlias: "sqlQuestionId",
+ },
+ );
+ cy.signOut();
+
+ mockAuthProviderAndJwtSignIn();
+ });
+
+ it("should be able to drill SQL question (EMB-273)", () => {
+ cy.get("@sqlQuestionId").then((sqlQuestionId) => {
+ mountSdkContent(
+ <InteractiveQuestion questionId={sqlQuestionId} withParameters />,
+ );
+ });
+
+ getSdkRoot().within(() => {
+ cy.findByText("SQL Orders").should("be.visible");
+ H.assertTableRowsCount(5);
+
+ // Drill down to "See these Orders"
+ cy.get("[data-dataset-index=0] > [data-column-id='PRODUCT_ID']")
+ .should("have.text", "14")
+ .click();
+ H.popover().within(() => {
+ cy.findByText("Filter by this value").should("be.visible");
+ cy.button(">").click();
+ });
+ H.assertTableRowsCount(4);
+ });
+ });
+});
diff --git a/src/metabase/queries/metadata.clj b/src/metabase/queries/metadata.clj
index 1c4f7379ff5..998d3399e6f 100644
--- a/src/metabase/queries/metadata.clj
+++ b/src/metabase/queries/metadata.clj
@@ -1,20 +1,46 @@
(ns metabase.queries.metadata
(:require
[clojure.set :as set]
+ [metabase.api.common :as api]
[metabase.legacy-mbql.normalize :as mbql.normalize]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.lib.util :as lib.util]
[metabase.models.interface :as mi]
+ [metabase.permissions.core :as perms]
[metabase.util :as u]
[metabase.util.malli :as mu]
[metabase.warehouse-schema.field :as schema.field]
[metabase.warehouse-schema.table :as schema.table]
[toucan2.core :as t2]))
+;; This is similar to the function in src/metabase/warehouses/api.clj, but we want to
+;; allow filtering the DBs in case of audit database, we don't want to allow users to modify
+;; its queries.
+(mu/defn- get-native-perms-info :- [:enum :write :none]
+ "Calculate `:native_permissions` field for the passed database describing the current user's permissions for running
+ native (e.g. SQL) queries. Will be either `:write` or `:none`. `:write` means you can run ad-hoc native queries,
+ and save new Cards with native queries; `:none` means you can do neither.
+
+ For the curious: the use of `:write` and `:none` is mainly for legacy purposes, when we had data-access-based
+ permissions; there was a specific option where you could give a Perms Group permissions to run existing Cards with
+ native queries, but not to create new ones. With the advent of what is currently being called 'Space-Age
+ Permissions', all Cards' permissions are based on their parent Collection, removing the need for native read perms."
+ [db :- [:map]]
+ (if (and (not (:is_audit db))
+ (= :query-builder-and-native
+ (perms/full-db-permission-for-user
+ api/*current-user-id*
+ :perms/create-queries
+ (u/the-id db))))
+ :write
+ :none))
+
(defn- get-databases
[ids]
(when (seq ids)
- (into [] (filter mi/can-read?)
+ (perms/prime-db-cache ids)
+ (into [] (comp (filter mi/can-read?)
+ (map #(assoc % :native_permissions (get-native-perms-info %))))
(t2/select :model/Database :id [:in ids]))))
(defn- field-ids->table-ids