Skip to content

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

Fix: UXW-2235 Prevents 403s from SmartLinks from hiding the whole document (#65609)

Brad Andersonaf548d9daa47
diff --git a/e2e/test/scenarios/documents/document-links.cy.spec.ts b/e2e/test/scenarios/documents/document-links.cy.spec.ts
new file mode 100644
index 00000000000..57c2f7e1596
--- /dev/null
+++ b/e2e/test/scenarios/documents/document-links.cy.spec.ts
@@ -0,0 +1,78 @@
+import { PRODUCTS_AVERAGE_BY_CATEGORY } from "e2e/support/test-visualizer-data";
+
+const { H } = cy;
+
+describe("Links in documents", () => {
+ beforeEach(() => {
+ H.restore();
+ cy.signInAsAdmin();
+ H.activateToken("bleeding-edge");
+ });
+
+ describe("smart links", () => {
+ beforeEach(() => {
+ H.createQuestion(PRODUCTS_AVERAGE_BY_CATEGORY, { wrapId: true });
+ cy.get("@questionId").then((questionId) => {
+ H.createDocument({
+ name: "Document with SmartLinks",
+ document: {
+ type: "doc",
+ content: [
+ {
+ type: "paragraph",
+ content: [
+ { type: "text", text: "See " },
+ {
+ type: "smartLink",
+ attrs: {
+ entityId: questionId,
+ model: "card",
+ label: "cached name",
+ },
+ },
+ { type: "text", text: "." },
+ ],
+ },
+ ],
+ },
+ idAlias: "documentId",
+ });
+ });
+ });
+
+ it("should display the most up-to-date title for the entity it references", () => {
+ H.visitDocument("@documentId");
+
+ H.documentContent().should(
+ "contain.text",
+ `See ${PRODUCTS_AVERAGE_BY_CATEGORY.name}`,
+ );
+
+ H.documentContent()
+ .findByRole("link", {
+ name: new RegExp(PRODUCTS_AVERAGE_BY_CATEGORY.name),
+ })
+ .should("exist");
+
+ H.documentContent()
+ .findByRole("link", { name: /cached name/ })
+ .should("not.exist");
+ });
+
+ it("should display 'No access' if the user doesn't have permission to see the link", () => {
+ cy.get("@questionId").then((questionId) => {
+ cy.intercept("GET", `/api/card/${questionId}`, {
+ statusCode: 403,
+ });
+ });
+ H.visitDocument("@documentId");
+
+ H.documentContent().should("contain.text", "See No access");
+ H.documentContent()
+ .findByText("No access")
+ .should("exist")
+ .icon("eye_crossed_out")
+ .should("exist");
+ });
+ });
+});
diff --git a/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/SmartLink/SmartLinkNode.tsx b/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/SmartLink/SmartLinkNode.tsx
index 0c88a4099e3..09a4bd2355a 100644
--- a/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/SmartLink/SmartLinkNode.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/SmartLink/SmartLinkNode.tsx
@@ -6,6 +6,7 @@ import {
} from "@tiptap/react";
import { memo, useEffect } from "react";
import { t } from "ttag";
+import { isObject } from "underscore";
import {
useGetActionQuery,
@@ -119,6 +120,7 @@ export function parseEntityUrl(
}[] = [
{ pattern: /^\/question\/(\d+)/, model: "card" },
{ pattern: /^\/model\/(\d+)/, model: "dataset" },
+ { pattern: /^\/metric\/(\d+)/, model: "metric" },
{ pattern: /^\/dashboard\/(\d+)/, model: "dashboard" },
{ pattern: /^\/collection\/(\d+)/, model: "collection" },
{
@@ -271,20 +273,19 @@ export const useEntityData = (
entityId: number | null,
model: SuggestionModel | null,
) => {
+ const isCard = model && ["card", "dataset", "metric"].includes(model);
const cardQuery = useGetCardQuery(
- { id: entityId! },
- {
- skip: !entityId || (model !== "card" && model !== "dataset"),
- },
+ { id: entityId!, ignore_error: true },
+ { skip: !entityId || !isCard },
);
const dashboardQuery = useGetDashboardQuery(
- { id: entityId! },
+ { id: entityId!, ignore_error: true },
{ skip: !entityId || model !== "dashboard" },
);
const collectionQuery = useGetCollectionQuery(
- { id: entityId! },
+ { id: entityId!, ignore_error: true },
{ skip: !entityId || model !== "collection" },
);
@@ -440,8 +441,17 @@ export const SmartLinkComponent = memo(
<NodeViewWrapper as="span">
<span className={styles.smartLink}>
<span className={styles.smartLinkInner}>
- <Icon name="warning" className={styles.icon} />
- {error ? t`Failed to load` : t`Unknown`} {model}
+ {isObject(error) && error.status === 403 ? (
+ <>
+ <Icon name="eye_crossed_out" className={styles.icon} />
+ {t`No access`}
+ </>
+ ) : (
+ <>
+ <Icon name="warning" className={styles.icon} />
+ {error ? t`Failed to load` : t`Unknown`} {model}
+ </>
+ )}
</span>
</span>
</NodeViewWrapper>
diff --git a/frontend/src/metabase-types/api/collection.ts b/frontend/src/metabase-types/api/collection.ts
index 947cddd08dc..b835e728a23 100644
--- a/frontend/src/metabase-types/api/collection.ts
+++ b/frontend/src/metabase-types/api/collection.ts
@@ -162,6 +162,7 @@ export interface CollectionListQuery {
export type getCollectionRequest = {
id: CollectionId;
namespace?: "snippets";
+ ignore_error?: boolean;
};
export type ListCollectionItemsSortColumn =
diff --git a/frontend/src/metabase/api/collection.ts b/frontend/src/metabase/api/collection.ts
index 1a4d8cfb274..ad5a7a6239e 100644
--- a/frontend/src/metabase/api/collection.ts
+++ b/frontend/src/metabase/api/collection.ts
@@ -70,11 +70,12 @@ export const collectionApi = Api.injectEndpoints({
],
}),
getCollection: builder.query<Collection, getCollectionRequest>({
- query: ({ id, ...params }) => {
+ query: ({ id, ignore_error, ...params }) => {
return {
method: "GET",
url: `/api/collection/${id}`,
params,
+ noEvent: ignore_error,
};
},
providesTags: (collection) =>