Disallow deletion of restricted user attributes (#2734)

This commit is contained in:
Jon Koops 2022-06-02 09:04:11 +02:00 committed by GitHub
parent 68e8a6fefc
commit f14dd1f86e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View file

@ -20,11 +20,13 @@ export type Field<T> = {
cellRenderer?: (row: T) => ReactNode; cellRenderer?: (row: T) => ReactNode;
}; };
export type Action<T> = IAction & { isActionable?: (item: T) => boolean };
type DraggableTableProps<T> = Omit<TableComposableProps, "data" | "ref"> & { type DraggableTableProps<T> = Omit<TableComposableProps, "data" | "ref"> & {
keyField: string; keyField: string;
columns: Field<T>[]; columns: Field<T>[];
data: T[]; data: T[];
actions?: IAction[]; actions?: Action<T>[];
onDragFinish: (dragged: string, newOrder: string[]) => void; onDragFinish: (dragged: string, newOrder: string[]) => void;
}; };
@ -217,7 +219,14 @@ export function DraggableTable<T>({
))} ))}
{actions && ( {actions && (
<Td isActionCell> <Td isActionCell>
<ActionsColumn items={actions} rowData={row} /> <ActionsColumn
items={actions.map(({ isActionable, ...action }) =>
isActionable
? { ...action, isDisabled: !isActionable(row) }
: action
)}
rowData={row}
/>
</Td> </Td>
)} )}
</Tr> </Tr>

View file

@ -24,6 +24,8 @@ import { toAttribute } from "../routes/Attribute";
import type { UserProfileAttribute } from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig"; import type { UserProfileAttribute } from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig";
import useToggle from "../../utils/useToggle"; import useToggle from "../../utils/useToggle";
const RESTRICTED_ATTRIBUTES = ["username", "email"];
type movedAttributeType = UserProfileAttribute; type movedAttributeType = UserProfileAttribute;
export const AttributesTab = () => { export const AttributesTab = () => {
@ -182,6 +184,7 @@ export const AttributesTab = () => {
}, },
{ {
title: t("common:delete"), title: t("common:delete"),
isActionable: ({ name }) => !RESTRICTED_ATTRIBUTES.includes(name!),
onClick: (_key, _idx, component) => { onClick: (_key, _idx, component) => {
setAttributeToDelete(component.name); setAttributeToDelete(component.name);
toggleDeleteDialog(); toggleDeleteDialog();