import { useState } from "react"; import { AlertVariant, Button, ButtonVariant, Divider, Flex, FlexItem, PageSection, Radio, Switch, Title, ToolbarItem, } from "@patternfly/react-core"; import { ListEmptyState } from "../components/list-empty-state/ListEmptyState"; import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable"; import { useTranslation } from "react-i18next"; import { useAdminClient, useFetch } from "../context/auth/AdminClient"; import { prettyPrintJSON } from "../util"; import { CodeEditor, Language } from "@patternfly/react-code-editor"; import { Link } from "react-router-dom-v5-compat"; import { useNavigate } from "react-router-dom-v5-compat"; import type ClientPolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyRepresentation"; import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog"; import { useAlerts } from "../components/alert/Alerts"; import { useRealm } from "../context/realm-context/RealmContext"; import { toAddClientPolicy } from "./routes/AddClientPolicy"; import { toEditClientPolicy } from "./routes/EditClientPolicy"; import { KeycloakSpinner } from "../components/keycloak-spinner/KeycloakSpinner"; import { Controller, useForm } from "react-hook-form"; import { toClientPolicies } from "./routes/ClientPolicies"; import "./realm-settings-section.css"; export const PoliciesTab = () => { const { t } = useTranslation("realm-settings"); const { adminClient } = useAdminClient(); const { addAlert, addError } = useAlerts(); const { realm } = useRealm(); const navigate = useNavigate(); const [show, setShow] = useState(false); const [policies, setPolicies] = useState(); const [selectedPolicy, setSelectedPolicy] = useState(); const [key, setKey] = useState(0); const [code, setCode] = useState(); const [tablePolicies, setTablePolicies] = useState(); const refresh = () => setKey(key + 1); const form = useForm>({ mode: "onChange" }); useFetch( () => adminClient.clientPolicies.listPolicies(), (policies) => { setPolicies(policies.policies), setTablePolicies(policies.policies || []), setCode(prettyPrintJSON(policies.policies)); }, [key] ); const loader = async () => policies ?? []; const saveStatus = async () => { const switchValues = form.getValues(); const updatedPolicies = policies?.map( (policy) => { const enabled = switchValues[policy.name!]; return { ...policy, enabled, }; } ); try { await adminClient.clientPolicies.updatePolicy({ policies: updatedPolicies, }); navigate(toClientPolicies({ realm, tab: "policies" })); addAlert( t("realm-settings:updateClientPolicySuccess"), AlertVariant.success ); } catch (error) { addError("realm-settings:updateClientPolicyError", error); } }; const ClientPolicyDetailLink = ({ name }: ClientPolicyRepresentation) => ( {name} ); const SwitchRenderer = ({ clientPolicy, }: { clientPolicy: ClientPolicyRepresentation; }) => { const [toggleDisableDialog, DisableConfirm] = useConfirmDialog({ titleKey: "realm-settings:disablePolicyConfirmTitle", messageKey: "realm-settings:disablePolicyConfirm", continueButtonLabel: "common:disable", onConfirm: () => { form.setValue(clientPolicy.name!, false); saveStatus(); }, }); return ( <> ( { if (!value) { toggleDisableDialog(); } else { onChange(value); saveStatus(); } }} /> )} /> ); }; const save = async () => { if (!code) { return; } try { const obj: ClientPolicyRepresentation[] = JSON.parse(code); try { await adminClient.clientPolicies.updatePolicy({ policies: obj, }); addAlert( t("realm-settings:updateClientPoliciesSuccess"), AlertVariant.success ); refresh(); } catch (error) { addError("realm-settings:updateClientPoliciesError", error); } } catch (error) { console.warn("Invalid json, ignoring value using {}"); addError("realm-settings:updateClientPoliciesError", error); } }; const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({ titleKey: t("deleteClientPolicyConfirmTitle"), messageKey: t("deleteClientPolicyConfirm", { policyName: selectedPolicy?.name, }), continueButtonLabel: t("delete"), continueButtonVariant: ButtonVariant.danger, onConfirm: async () => { const updatedPolicies = policies?.filter( (policy) => policy.name !== selectedPolicy?.name ); try { await adminClient.clientPolicies.updatePolicy({ policies: updatedPolicies, }); addAlert(t("deleteClientPolicySuccess"), AlertVariant.success); refresh(); } catch (error) { addError(t("deleteClientPolicyError"), error); } }, }); if (!policies) { return ; } return ( <> {t("policiesConfigType")} setShow(false)} label={t("policiesConfigTypes.formView")} id="formView-policiesView" data-testid="formView-policiesView" className="kc-form-radio-btn pf-u-mr-sm pf-u-ml-sm" /> setShow(true)} label={t("policiesConfigTypes.jsonEditor")} id="jsonEditor-policiesView" data-testid="jsonEditor-policiesView" className="kc-editor-radio-btn" /> {!show ? ( navigate(toAddClientPolicy({ realm }))} /> } ariaLabelKey="realm-settings:clientPolicies" searchPlaceholderKey="realm-settings:clientPolicySearch" loader={loader} toolbarItem={ } actions={[ { title: t("common:delete"), onRowClick: (item) => { toggleDeleteDialog(); setSelectedPolicy(item); }, }, ]} columns={[ { name: "name", cellRenderer: ClientPolicyDetailLink, }, { name: "enabled", displayKey: "realm-settings:status", cellRenderer: (clientPolicy) => ( ), }, { name: "description", }, ]} /> ) : ( <>
)} ); };