UNPKG

@keycloakify/keycloak-admin-ui

Version:
68 lines 5.27 kB
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { ActionGroup, AlertVariant, Button, ButtonVariant, Divider, EmptyState, EmptyStateActions, EmptyStateBody, EmptyStateFooter, EmptyStateHeader, EmptyStateIcon, PageSection, Toolbar, ToolbarContent, ToolbarItem, Select, MenuToggle, SelectList, SelectOption, } from "@patternfly/react-core"; import { PlusCircleIcon } from "@patternfly/react-icons"; import { useEffect, useMemo, useState } from "react"; import { FormProvider, useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { useAdminClient } from "../../admin-client"; import { useAlerts } from "../../ui-shared"; import { FormAccess } from "../../components/form/FormAccess"; import { useRealm } from "../../context/realm-context/RealmContext"; import { useServerInfo } from "../../context/server-info/ServerInfoProvider"; import { PolicyRow } from "../../authentication/policies/PolicyRow"; import { parsePolicy, serializePolicy } from "../../authentication/policies/util"; const PolicySelect = ({ onSelect, selectedPolicies }) => { const { t } = useTranslation(); const { passwordPolicies } = useServerInfo(); const [open, setOpen] = useState(false); const policies = useMemo(() => passwordPolicies === null || passwordPolicies === void 0 ? void 0 : passwordPolicies.filter((p) => selectedPolicies.find((o) => o.id === p.id) === undefined), [selectedPolicies]); return (_jsx(Select, { onSelect: (_, selection) => { onSelect(selection); setOpen(false); }, toggle: (ref) => (_jsx(MenuToggle, { ref: ref, onClick: () => setOpen(!open), isExpanded: open, isDisabled: (policies === null || policies === void 0 ? void 0 : policies.length) === 0, style: { width: "300px" }, "data-testid": "add-policy", children: t("addPolicy") })), isOpen: open, children: _jsx(SelectList, { children: policies === null || policies === void 0 ? void 0 : policies.map((policy) => (_jsx(SelectOption, { value: policy, children: policy.displayName }, policy.id))) }) })); }; export const PasswordPolicy = ({ realm, realmUpdated, }) => { const { adminClient } = useAdminClient(); const { t } = useTranslation(); const { passwordPolicies } = useServerInfo(); const { addAlert, addError } = useAlerts(); const { realm: realmName } = useRealm(); const [rows, setRows] = useState([]); const onSelect = (row) => { setRows([...rows, row]); setValue(row.id, row.defaultValue, { shouldDirty: true }); }; const form = useForm({ defaultValues: {}, }); const { handleSubmit, setValue, reset, formState: { isDirty }, } = form; const setupForm = (realm) => { reset(); const values = parsePolicy(realm.passwordPolicy || "", passwordPolicies); values.forEach((v) => { setValue(v.id, v.value); }); setRows(values); }; useEffect(() => setupForm(realm), []); const save = async (values) => { const updatedRealm = { ...realm, passwordPolicy: serializePolicy(rows, values), }; try { await adminClient.realms.update({ realm: realmName }, updatedRealm); realmUpdated(updatedRealm); setupForm(updatedRealm); addAlert(t("updatePasswordPolicySuccess"), AlertVariant.success); } catch (error) { addError("updatePasswordPolicyError", error); } }; return (_jsxs(PageSection, { variant: "light", className: "pf-v5-u-p-0", children: [(rows.length !== 0 || realm.passwordPolicy) && (_jsxs(_Fragment, { children: [_jsx(Toolbar, { children: _jsx(ToolbarContent, { children: _jsx(ToolbarItem, { children: _jsx(PolicySelect, { onSelect: onSelect, selectedPolicies: rows }) }) }) }), _jsx(Divider, {}), _jsx(PageSection, { variant: "light", children: _jsx(FormProvider, { ...form, children: _jsxs(FormAccess, { className: "keycloak__policies_authentication__form", role: "manage-realm", isHorizontal: true, onSubmit: handleSubmit(save), children: [rows.map((r, index) => (_jsx(PolicyRow, { policy: r, onRemove: (id) => { setRows(rows.filter((r) => r.id !== id)); setValue(r.id, "", { shouldDirty: true }); } }, `${r.id}-${index}`))), _jsxs(ActionGroup, { children: [_jsx(Button, { "data-testid": "save", variant: "primary", type: "submit", isDisabled: !isDirty, children: t("save") }), _jsx(Button, { "data-testid": "reload", variant: ButtonVariant.link, onClick: () => setupForm(realm), children: t("reload") })] })] }) }) })] })), !rows.length && !realm.passwordPolicy && (_jsxs(EmptyState, { "data-testid": "empty-state", variant: "lg", children: [_jsx(EmptyStateHeader, { titleText: _jsx(_Fragment, { children: t("noPasswordPolicies") }), icon: _jsx(EmptyStateIcon, { icon: PlusCircleIcon }), headingLevel: "h1" }), _jsx(EmptyStateBody, { children: t("noPasswordPoliciesInstructions") }), _jsx(EmptyStateFooter, { children: _jsx(EmptyStateActions, { children: _jsx(PolicySelect, { onSelect: onSelect, selectedPolicies: [] }) }) })] }))] })); }; //# sourceMappingURL=PasswordPolicy.js.map