Skip to content

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

Show the unauthorized page if the user tries to access notebook without data perms (#62300)

Alexander Polyankin509225c8d217
diff --git a/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts b/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts
index 80333966a4b..e7b69b90b75 100644
--- a/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts
+++ b/e2e/test/scenarios/question-reproductions/reproductions.cy.spec.ts
@@ -1124,3 +1124,23 @@ describe("issue 55487", () => {
cy.findByTestId("object-detail").should("be.visible");
});
});
+
+describe("issue 58628", () => {
+ beforeEach(() => {
+ H.restore();
+ cy.signIn("nodata");
+ });
+
+ it("should show the unauthorized page when accessing the notebook editor without data perms (metabase#58628)", () => {
+ cy.log("should not be able to access the notebook editor");
+ cy.visit("/question/notebook");
+ cy.url().should("include", "/unauthorized");
+ H.main()
+ .findByText("Sorry, you don’t have permission to see that.")
+ .should("be.visible");
+
+ cy.log("should be able to access the query builder in view mode");
+ H.visitQuestion(ORDERS_QUESTION_ID);
+ H.queryBuilderHeader().should("be.visible");
+ });
+});
diff --git a/e2e/test/scenarios/question/notebook-link-to-data-source.cy.spec.ts b/e2e/test/scenarios/question/notebook-link-to-data-source.cy.spec.ts
index 2d0a69b28c1..6019c995ae1 100644
--- a/e2e/test/scenarios/question/notebook-link-to-data-source.cy.spec.ts
+++ b/e2e/test/scenarios/question/notebook-link-to-data-source.cy.spec.ts
@@ -7,7 +7,10 @@ import {
ORDERS_COUNT_QUESTION_ID,
ORDERS_MODEL_ID,
} from "e2e/support/cypress_sample_instance_data";
-import type { NativeQuestionDetails } from "e2e/support/helpers";
+import type {
+ NativeQuestionDetails,
+ QuestionDetails,
+} from "e2e/support/helpers";
import { DataPermissionValue } from "metabase/admin/permissions/types";
import { METAKEY } from "metabase/lib/browser";
@@ -441,12 +444,10 @@ describe("scenarios > notebook > link to data source", () => {
.should("not.exist");
cy.visit(`/question/${nestedQuestion.id}/notebook`);
- cy.findByTestId("entity-picker-modal").within(() => {
- cy.findByText("Pick your starting data").should("be.visible");
- cy.findByLabelText("Close").click();
- });
-
- H.getNotebookStep("data").should("contain", "Pick your starting data");
+ cy.url().should("include", "/unauthorized");
+ H.main()
+ .findByText("Sorry, you don’t have permission to see that.")
+ .should("be.visible");
});
});
@@ -539,7 +540,7 @@ describe("scenarios > notebook > link to data source", () => {
});
context("joins", () => {
- const getQuery = (id: number) => {
+ const getQuery = (id: number): QuestionDetails => {
return {
dataset_query: {
database: SAMPLE_DB_ID,
diff --git a/frontend/src/metabase/query_builder/actions/core/initializeQB.ts b/frontend/src/metabase/query_builder/actions/core/initializeQB.ts
index 1a3d1b9f82f..30b3d5c1d19 100644
--- a/frontend/src/metabase/query_builder/actions/core/initializeQB.ts
+++ b/frontend/src/metabase/query_builder/actions/core/initializeQB.ts
@@ -1,6 +1,8 @@
import type { LocationDescriptorObject } from "history";
import querystring from "querystring";
+import { replace } from "react-router-redux";
+import { databaseApi } from "metabase/api";
import Questions from "metabase/entities/questions";
import Snippets from "metabase/entities/snippets";
import { deserializeCardFromUrl } from "metabase/lib/card";
@@ -13,6 +15,7 @@ import {
} from "metabase/query_builder/selectors";
import { loadMetadataForCard } from "metabase/questions/actions";
import { setErrorPage } from "metabase/redux/app";
+import { getHasDataAccess } from "metabase/selectors/data";
import { getMetadata } from "metabase/selectors/metadata";
import { getUser } from "metabase/selectors/user";
import * as Lib from "metabase-lib";
@@ -186,6 +189,17 @@ export async function resolveCards({
);
}
+async function loadDatabases(dispatch: any) {
+ const action = databaseApi.endpoints.listDatabases.initiate();
+ try {
+ const { data } = await dispatch(action).unwrap();
+ return data;
+ } catch (error) {
+ console.error("error loading databases", error);
+ return [];
+ }
+}
+
export function parseHash(hash?: string) {
let options: BlankQueryOptions = {};
let serializedCard;
@@ -257,6 +271,14 @@ async function handleQBInit(
const hasCard = cardId || serializedCard;
const currentUser = getUser(getState());
+ if (uiControls.queryBuilderMode === "notebook") {
+ const databases = await loadDatabases(dispatch);
+ if (!getHasDataAccess(databases)) {
+ dispatch(replace("/unauthorized"));
+ return;
+ }
+ }
+
const deserializedCard = serializedCard
? deserializeCard(serializedCard)
: null;
diff --git a/frontend/src/metabase/query_builder/actions/core/initializeQB.unit.spec.ts b/frontend/src/metabase/query_builder/actions/core/initializeQB.unit.spec.ts
index b631c8ab185..17eea29d3df 100644
--- a/frontend/src/metabase/query_builder/actions/core/initializeQB.unit.spec.ts
+++ b/frontend/src/metabase/query_builder/actions/core/initializeQB.unit.spec.ts
@@ -62,11 +62,14 @@ async function baseSetup({
}: BaseSetupOpts) {
jest.useFakeTimers();
- const dispatch = jest.fn().mockReturnValue({ mock: "mock" });
+ const databases = hasDataPermissions ? [createSampleDatabase()] : [];
+ const dispatch = jest
+ .fn()
+ .mockReturnValue({ unwrap: () => ({ data: databases }) });
const state = createMockState({
entities: createMockEntitiesState({
- databases: hasDataPermissions ? [createSampleDatabase()] : [],
+ databases,
segments: [SEGMENT],
}),
currentUser: user === undefined ? createMockUser() : user,