UNPKG

@keycloakify/keycloak-admin-ui

Version:
137 lines 7.37 kB
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import { useAlerts } from "../../ui-shared"; import { AlertVariant, Badge, Button, ButtonVariant, Checkbox, ToolbarItem, } from "@patternfly/react-core"; import { cellWidth } from "@patternfly/react-table"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import { useAdminClient } from "../../admin-client"; import { emptyFormatter, upperCaseFormatter } from "../../util"; import { translationFormatter } from "../../utils/translationFormatter"; import { useConfirmDialog } from "../../components/confirm-dialog/ConfirmDialog"; import { ListEmptyState } from "../../ui-shared"; import { KeycloakDataTable } from "../../ui-shared"; import { AddRoleMappingModal } from "../../components/role-mapping/AddRoleMappingModal"; import { deleteMapping, getEffectiveRoles, getMapping } from "../../components/role-mapping/queries"; import { getEffectiveClientRoles } from "../../components/role-mapping/resource"; import "./role-mapping.css"; export const mapRoles = (assignedRoles, effectiveRoles, hide) => [ ...(hide ? assignedRoles.map((row) => ({ ...row, role: { ...row.role, isInherited: false, }, })) : effectiveRoles.map((row) => ({ ...row, role: { ...row.role, isInherited: assignedRoles.find((r) => r.role.id === row.role.id) === undefined, }, }))), ]; export const ServiceRole = ({ role, client }) => (_jsxs(_Fragment, { children: [(client === null || client === void 0 ? void 0 : client.clientId) && (_jsx(Badge, { isRead: true, className: "keycloak-admin--role-mapping__client-name", children: client.clientId })), role.name] })); export const RoleMapping = ({ name, id, type, isManager = true, save, }) => { const { adminClient } = useAdminClient(); const { t } = useTranslation(); const { addAlert, addError } = useAlerts(); const [key, setKey] = useState(0); const refresh = () => setKey(key + 1); const [hide, setHide] = useState(true); const [showAssign, setShowAssign] = useState(false); const [selected, setSelected] = useState([]); const assignRoles = async (rows) => { await save(rows); refresh(); }; const loader = async () => { var _a; let effectiveRoles = []; let effectiveClientRoles = []; if (!hide) { effectiveRoles = await getEffectiveRoles(adminClient, type, id); effectiveClientRoles = (await getEffectiveClientRoles(adminClient, { type, id, })).map((e) => ({ client: { clientId: e.client, id: e.clientId }, role: { id: e.id, name: e.role, description: e.description }, })); effectiveRoles = effectiveRoles.filter((role) => !effectiveClientRoles.some((clientRole) => clientRole.role.id === role.role.id)); } const roles = await getMapping(adminClient, type, id); const realmRolesMapping = ((_a = roles.realmMappings) === null || _a === void 0 ? void 0 : _a.map((role) => ({ role }))) || []; const clientMapping = Object.values(roles.clientMappings || {}) .map((client) => client.mappings.map((role) => ({ client: { clientId: client.client, ...client }, role, }))) .flat(); return [ ...mapRoles([...clientMapping, ...realmRolesMapping], [...effectiveClientRoles, ...effectiveRoles], hide), ]; }; const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({ titleKey: "removeMappingTitle", messageKey: t("removeMappingConfirm", { count: selected.length }), continueButtonLabel: "remove", continueButtonVariant: ButtonVariant.danger, onCancel: () => { setSelected([]); refresh(); }, onConfirm: async () => { try { await Promise.all(deleteMapping(adminClient, type, id, selected)); addAlert(t("clientScopeRemoveSuccess"), AlertVariant.success); setSelected([]); refresh(); } catch (error) { addError("clientScopeRemoveError", error); } }, }); return (_jsxs(_Fragment, { children: [showAssign && (_jsx(AddRoleMappingModal, { id: id, type: type, name: name, onAssign: assignRoles, onClose: () => setShowAssign(false) })), _jsx(DeleteConfirm, {}), _jsx(KeycloakDataTable, { "data-testid": "assigned-roles", loader: loader, canSelectAll: true, onSelect: (rows) => setSelected(rows), searchPlaceholderKey: "searchByName", ariaLabelKey: "clientScopeList", isRowDisabled: (value) => value.role.isInherited || false, toolbarItem: _jsxs(_Fragment, { children: [_jsx(ToolbarItem, { children: _jsx(Checkbox, { label: t("hideInheritedRoles"), id: "hideInheritedRoles", "data-testid": "hideInheritedRoles", isChecked: hide, onChange: (_event, check) => { setHide(check); refresh(); } }) }), isManager && (_jsxs(_Fragment, { children: [_jsx(ToolbarItem, { children: _jsx(Button, { "data-testid": "assignRole", onClick: () => setShowAssign(true), children: t("assignRole") }) }), _jsx(ToolbarItem, { children: _jsx(Button, { variant: "link", "data-testid": "unAssignRole", onClick: toggleDeleteDialog, isDisabled: selected.length === 0, children: t("unAssignRole") }) })] }))] }), actions: isManager ? [ { title: t("unAssignRole"), onRowClick: async (role) => { setSelected([role]); toggleDeleteDialog(); return false; }, }, ] : [], columns: [ { name: "role.name", displayKey: "name", transforms: [cellWidth(30)], cellRenderer: ServiceRole, }, { name: "role.isInherited", displayKey: "inherent", cellFormatters: [upperCaseFormatter(), emptyFormatter()], }, { name: "role.description", displayKey: "description", cellFormatters: [translationFormatter(t)], }, ], emptyState: _jsx(ListEmptyState, { message: t(`noRoles-${type}`), instructions: t(`noRolesInstructions-${type}`), primaryActionText: t("assignRole"), onPrimaryAction: () => setShowAssign(true), secondaryActions: [ { text: t("showInheritedRoles"), onClick: () => { setHide(false); refresh(); }, }, ] }) }, `${id}${key}`)] })); }; //# sourceMappingURL=RoleMapping.js.map