From f14dd1f86e37358484444c8f7384dace571a6ea3 Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Thu, 2 Jun 2022 09:04:11 +0200 Subject: [PATCH] Disallow deletion of restricted user attributes (#2734) --- src/authentication/components/DraggableTable.tsx | 13 +++++++++++-- src/realm-settings/user-profile/AttributesTab.tsx | 3 +++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/authentication/components/DraggableTable.tsx b/src/authentication/components/DraggableTable.tsx index 3402977c61..c6063949ca 100644 --- a/src/authentication/components/DraggableTable.tsx +++ b/src/authentication/components/DraggableTable.tsx @@ -20,11 +20,13 @@ export type Field = { cellRenderer?: (row: T) => ReactNode; }; +export type Action = IAction & { isActionable?: (item: T) => boolean }; + type DraggableTableProps = Omit & { keyField: string; columns: Field[]; data: T[]; - actions?: IAction[]; + actions?: Action[]; onDragFinish: (dragged: string, newOrder: string[]) => void; }; @@ -217,7 +219,14 @@ export function DraggableTable({ ))} {actions && ( - + + isActionable + ? { ...action, isDisabled: !isActionable(row) } + : action + )} + rowData={row} + /> )} diff --git a/src/realm-settings/user-profile/AttributesTab.tsx b/src/realm-settings/user-profile/AttributesTab.tsx index c0e989b91f..98ed7a8533 100644 --- a/src/realm-settings/user-profile/AttributesTab.tsx +++ b/src/realm-settings/user-profile/AttributesTab.tsx @@ -24,6 +24,8 @@ import { toAttribute } from "../routes/Attribute"; import type { UserProfileAttribute } from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig"; import useToggle from "../../utils/useToggle"; +const RESTRICTED_ATTRIBUTES = ["username", "email"]; + type movedAttributeType = UserProfileAttribute; export const AttributesTab = () => { @@ -182,6 +184,7 @@ export const AttributesTab = () => { }, { title: t("common:delete"), + isActionable: ({ name }) => !RESTRICTED_ATTRIBUTES.includes(name!), onClick: (_key, _idx, component) => { setAttributeToDelete(component.name); toggleDeleteDialog();