The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Get user attributes from attributes map (#66274)
Edward Paget99fd1809cbac
diff --git a/e2e/test/scenarios/embedding/interactive-embedding.cy.spec.js b/e2e/test/scenarios/embedding/interactive-embedding.cy.spec.jsindex 93b80c5745f..699da39a7ec 100644--- a/e2e/test/scenarios/embedding/interactive-embedding.cy.spec.js+++ b/e2e/test/scenarios/embedding/interactive-embedding.cy.spec.js@@ -1892,6 +1892,14 @@ describe("scenarios > embedding > full app - jwt sso integration", () => {H.updateSetting("jwt-shared-secret", jwtSecret);H.updateSetting("jwt-enabled", true);+ cy.intercept("POST", `/api/card/${ORDERS_QUESTION_ID}/query`).as(+ "getCardQuery",+ );+ addLinkClickBehavior({+ dashboardId,+ linkTemplate: "/question/{{test_attribute}}",+ });+cy.signOut(); // we *need* to sign out, otherwise the SSO process won't kick in});@@ -1937,6 +1945,43 @@ describe("scenarios > embedding > full app - jwt sso integration", () => {cy.url().should("equal", `${baseUrl}/dashboard/${dashboardId}`);H.main().findByText("Orders in a dashboard").should("be.visible");});++ it("should pass JWT user attributes to click behavior custom destinations (metabase#65942)", () => {+ const jwtAttributeValue = ORDERS_QUESTION_ID;++ // 1) set up a click behavior that uses a user attribute in the URL+ // Added in beforeeach so it uses the admin user++ // 2) sign a jwt for the user with a custom attribute+ cy.task("signJwt", {+ payload: {+ email: USERS.normal.email,+ exp: Math.round(Date.now() / 1000) + 10 * 60,+ test_attribute: jwtAttributeValue,+ },+ secret: jwtSecret,+ }).then((jwtToken) => {+ // 3) mock the JWT provider to redirect to the auth/sso endpoint with the JWT+ cy.intercept(/http:\/\/localhost:8888\/.*/, (req) => {+ const redirectUrl = `${baseUrl}/auth/sso?jwt=${jwtToken}&return_to=/dashboard/${dashboardId}`;+ req.redirect(redirectUrl);+ }).as("jwt-provider");+ });++ // 4) visit the dashboard as embedded+ H.visitFullAppEmbeddingUrl({ url: `/dashboard/${dashboardId}` });++ cy.wait("@jwt-provider");++ // 5) verify user is on dashboard+ cy.url().should("equal", `${baseUrl}/dashboard/${dashboardId}`);++ cy.findAllByRole("gridcell").first().click();+ cy.wait("@getCardQuery");++ cy.findByTestId("question-filter-header").realHover();+ cy.findByTestId("main-logo").should("be.visible");+ });});const visitQuestionUrl = (urlOptions) => {diff --git a/frontend/src/metabase-types/api/mocks/user.ts b/frontend/src/metabase-types/api/mocks/user.tsindex 6cdfa2efabf..d51337786cc 100644--- a/frontend/src/metabase-types/api/mocks/user.ts+++ b/frontend/src/metabase-types/api/mocks/user.ts@@ -8,6 +8,7 @@ export const createMockUser = (opts?: Partial<User>): User => ({custom_homepage: null,email: "[email protected]",locale: null,+ attributes: null,login_attributes: null,is_active: true,is_qbnewb: false,diff --git a/frontend/src/metabase-types/api/user.ts b/frontend/src/metabase-types/api/user.tsindex e6c0274e27e..20ffd955392 100644--- a/frontend/src/metabase-types/api/user.ts+++ b/frontend/src/metabase-types/api/user.ts@@ -42,6 +42,7 @@ export interface BaseUser {}export interface User extends BaseUser {+ attributes: UserAttributeMap | null;login_attributes: UserAttributeMap | null;structured_attributes?: StructuredUserAttributes;user_group_memberships?: { id: number; is_group_manager: boolean }[];diff --git a/frontend/src/metabase/selectors/user.ts b/frontend/src/metabase/selectors/user.tsindex 7b280dfeb42..666ade3c3df 100644--- a/frontend/src/metabase/selectors/user.ts+++ b/frontend/src/metabase/selectors/user.ts@@ -32,7 +32,7 @@ export const canAccessSettings = createSelector(export const getUserAttributes = createSelector([getUser],- (user) => user?.login_attributes || {},+ (user) => user?.attributes || {},);export const getUserPersonalCollectionId = createSelector(diff --git a/frontend/src/metabase/selectors/user.unit.spec.ts b/frontend/src/metabase/selectors/user.unit.spec.tsindex b7217d55a8c..7a76bd07b4e 100644--- a/frontend/src/metabase/selectors/user.unit.spec.ts+++ b/frontend/src/metabase/selectors/user.unit.spec.ts@@ -1,7 +1,7 @@import { createMockUser } from "metabase-types/api/mocks";import { createMockState } from "metabase-types/store/mocks";-import { getUserIsAdmin } from "./user";+import { getUserAttributes, getUserIsAdmin } from "./user";describe("metabase/selectors/user", () => {it("should return true if user is an admin", () => {@@ -19,4 +19,33 @@ describe("metabase/selectors/user", () => {expect(getUserIsAdmin(state)).toBe(false);});++ describe("getUserAttributes", () => {+ it("should return user attributes including JWT-sourced attributes", () => {+ const state = createMockState({+ currentUser: createMockUser({+ attributes: { jwt_attr: "jwt_value", manual_attr: "manual_value" },+ }),+ });++ expect(getUserAttributes(state)).toEqual({+ jwt_attr: "jwt_value",+ manual_attr: "manual_value",+ });+ });++ it("should return empty object when attributes is null", () => {+ const state = createMockState({+ currentUser: createMockUser({ attributes: null }),+ });++ expect(getUserAttributes(state)).toEqual({});+ });++ it("should return empty object when no current user", () => {+ const state = createMockState({ currentUser: null });++ expect(getUserAttributes(state)).toEqual({});+ });+ });});