2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2021-09-23 13:41:29 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
Divider,
|
|
|
|
Flex,
|
|
|
|
FlexItem,
|
|
|
|
PageSection,
|
|
|
|
Radio,
|
2021-11-24 16:19:02 +00:00
|
|
|
Switch,
|
2021-09-23 13:41:29 +00:00
|
|
|
Title,
|
|
|
|
ToolbarItem,
|
|
|
|
} from "@patternfly/react-core";
|
2021-09-09 10:45:19 +00:00
|
|
|
|
2021-09-23 13:41:29 +00:00
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2021-09-29 08:51:47 +00:00
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
2021-11-24 16:19:02 +00:00
|
|
|
import { prettyPrintJSON } from "../util";
|
2021-09-23 13:41:29 +00:00
|
|
|
import { CodeEditor, Language } from "@patternfly/react-code-editor";
|
2022-08-22 15:51:19 +00:00
|
|
|
import { Link } from "react-router-dom-v5-compat";
|
2022-08-16 13:09:14 +00:00
|
|
|
import { useNavigate } from "react-router-dom-v5-compat";
|
2021-09-23 13:41:29 +00:00
|
|
|
import type ClientPolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyRepresentation";
|
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2021-09-09 10:45:19 +00:00
|
|
|
|
2021-10-12 15:03:56 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2021-10-29 14:19:57 +00:00
|
|
|
import { toAddClientPolicy } from "./routes/AddClientPolicy";
|
2021-10-13 20:54:51 +00:00
|
|
|
import { toEditClientPolicy } from "./routes/EditClientPolicy";
|
2021-11-16 11:10:10 +00:00
|
|
|
import { KeycloakSpinner } from "../components/keycloak-spinner/KeycloakSpinner";
|
2021-11-24 16:19:02 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
import { toClientPolicies } from "./routes/ClientPolicies";
|
2022-03-14 10:16:43 +00:00
|
|
|
|
|
|
|
import "./realm-settings-section.css";
|
|
|
|
|
2021-09-09 10:45:19 +00:00
|
|
|
export const PoliciesTab = () => {
|
2021-09-23 13:41:29 +00:00
|
|
|
const { t } = useTranslation("realm-settings");
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-09-23 13:41:29 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-10-12 15:03:56 +00:00
|
|
|
const { realm } = useRealm();
|
2022-08-16 13:09:14 +00:00
|
|
|
const navigate = useNavigate();
|
2021-09-23 13:41:29 +00:00
|
|
|
const [show, setShow] = useState(false);
|
2021-09-29 08:51:47 +00:00
|
|
|
const [policies, setPolicies] = useState<ClientPolicyRepresentation[]>();
|
2021-09-23 13:41:29 +00:00
|
|
|
const [selectedPolicy, setSelectedPolicy] =
|
|
|
|
useState<ClientPolicyRepresentation>();
|
2021-10-12 15:03:56 +00:00
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
const [code, setCode] = useState<string>();
|
|
|
|
const [tablePolicies, setTablePolicies] =
|
|
|
|
useState<ClientPolicyRepresentation[]>();
|
|
|
|
const refresh = () => setKey(key + 1);
|
2021-09-23 13:41:29 +00:00
|
|
|
|
2021-11-24 16:19:02 +00:00
|
|
|
const form = useForm<Record<string, boolean>>({ mode: "onChange" });
|
|
|
|
|
2021-09-29 08:51:47 +00:00
|
|
|
useFetch(
|
|
|
|
() => adminClient.clientPolicies.listPolicies(),
|
2021-10-12 15:03:56 +00:00
|
|
|
(policies) => {
|
|
|
|
setPolicies(policies.policies),
|
|
|
|
setTablePolicies(policies.policies || []),
|
|
|
|
setCode(prettyPrintJSON(policies.policies));
|
|
|
|
},
|
|
|
|
[key]
|
2021-09-29 08:51:47 +00:00
|
|
|
);
|
2021-09-23 13:41:29 +00:00
|
|
|
|
2021-09-29 08:51:47 +00:00
|
|
|
const loader = async () => policies ?? [];
|
2021-09-23 13:41:29 +00:00
|
|
|
|
2021-11-24 16:19:02 +00:00
|
|
|
const saveStatus = async () => {
|
|
|
|
const switchValues = form.getValues();
|
|
|
|
|
|
|
|
const updatedPolicies = policies?.map<ClientPolicyRepresentation>(
|
|
|
|
(policy) => {
|
|
|
|
const enabled = switchValues[policy.name!];
|
|
|
|
|
|
|
|
return {
|
|
|
|
...policy,
|
|
|
|
enabled,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.updatePolicy({
|
|
|
|
policies: updatedPolicies,
|
|
|
|
});
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toClientPolicies({ realm, tab: "policies" }));
|
2021-11-24 16:19:02 +00:00
|
|
|
addAlert(
|
|
|
|
t("realm-settings:updateClientPolicySuccess"),
|
|
|
|
AlertVariant.success
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
addError("realm-settings:updateClientPolicyError", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-13 20:54:51 +00:00
|
|
|
const ClientPolicyDetailLink = ({ name }: ClientPolicyRepresentation) => (
|
|
|
|
<Link to={toEditClientPolicy({ realm, policyName: name! })}>{name}</Link>
|
|
|
|
);
|
|
|
|
|
2022-03-14 10:16:43 +00:00
|
|
|
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();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-11-24 16:19:02 +00:00
|
|
|
return (
|
2022-03-14 10:16:43 +00:00
|
|
|
<>
|
|
|
|
<DisableConfirm />
|
|
|
|
<Controller
|
|
|
|
name={clientPolicy.name!}
|
|
|
|
data-testid={`${clientPolicy.name!}-switch`}
|
|
|
|
defaultValue={clientPolicy.enabled}
|
|
|
|
control={form.control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Switch
|
|
|
|
label={t("common:enabled")}
|
|
|
|
labelOff={t("common:disabled")}
|
|
|
|
isChecked={value}
|
|
|
|
onChange={(value) => {
|
|
|
|
if (!value) {
|
|
|
|
toggleDisableDialog();
|
|
|
|
} else {
|
|
|
|
onChange(value);
|
|
|
|
saveStatus();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</>
|
2021-11-24 16:19:02 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-10-12 15:03:56 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
2021-09-23 13:41:29 +00:00
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: t("deleteClientPolicyConfirmTitle"),
|
|
|
|
messageKey: t("deleteClientPolicyConfirm", {
|
|
|
|
policyName: selectedPolicy?.name,
|
|
|
|
}),
|
|
|
|
continueButtonLabel: t("delete"),
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
2021-10-12 15:03:56 +00:00
|
|
|
const updatedPolicies = policies?.filter(
|
|
|
|
(policy) => policy.name !== selectedPolicy?.name
|
|
|
|
);
|
|
|
|
|
2021-09-23 13:41:29 +00:00
|
|
|
try {
|
2021-10-12 15:03:56 +00:00
|
|
|
await adminClient.clientPolicies.updatePolicy({
|
|
|
|
policies: updatedPolicies,
|
|
|
|
});
|
2021-09-23 13:41:29 +00:00
|
|
|
addAlert(t("deleteClientPolicySuccess"), AlertVariant.success);
|
2021-10-12 15:03:56 +00:00
|
|
|
refresh();
|
2021-09-23 13:41:29 +00:00
|
|
|
} catch (error) {
|
|
|
|
addError(t("deleteClientPolicyError"), error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-09-29 08:51:47 +00:00
|
|
|
if (!policies) {
|
2021-11-16 11:10:10 +00:00
|
|
|
return <KeycloakSpinner />;
|
2021-09-29 08:51:47 +00:00
|
|
|
}
|
2021-09-09 10:45:19 +00:00
|
|
|
return (
|
2021-09-23 13:41:29 +00:00
|
|
|
<>
|
|
|
|
<DeleteConfirm />
|
|
|
|
<PageSection>
|
|
|
|
<Flex className="kc-policies-config-section">
|
|
|
|
<FlexItem>
|
|
|
|
<Title headingLevel="h1" size="md">
|
|
|
|
{t("policiesConfigType")}
|
|
|
|
</Title>
|
|
|
|
</FlexItem>
|
|
|
|
<FlexItem>
|
|
|
|
<Radio
|
|
|
|
isChecked={!show}
|
2021-09-29 08:51:47 +00:00
|
|
|
name="policiesView"
|
2021-09-23 13:41:29 +00:00
|
|
|
onChange={() => setShow(false)}
|
|
|
|
label={t("policiesConfigTypes.formView")}
|
2021-09-29 08:51:47 +00:00
|
|
|
id="formView-policiesView"
|
2021-10-12 15:03:56 +00:00
|
|
|
data-testid="formView-policiesView"
|
2021-09-23 13:41:29 +00:00
|
|
|
className="kc-form-radio-btn pf-u-mr-sm pf-u-ml-sm"
|
|
|
|
/>
|
|
|
|
</FlexItem>
|
|
|
|
<FlexItem>
|
|
|
|
<Radio
|
|
|
|
isChecked={show}
|
2021-09-29 08:51:47 +00:00
|
|
|
name="policiesView"
|
2021-09-23 13:41:29 +00:00
|
|
|
onChange={() => setShow(true)}
|
|
|
|
label={t("policiesConfigTypes.jsonEditor")}
|
2021-09-29 08:51:47 +00:00
|
|
|
id="jsonEditor-policiesView"
|
2021-10-12 15:03:56 +00:00
|
|
|
data-testid="jsonEditor-policiesView"
|
2021-09-23 13:41:29 +00:00
|
|
|
className="kc-editor-radio-btn"
|
|
|
|
/>
|
|
|
|
</FlexItem>
|
|
|
|
</Flex>
|
|
|
|
</PageSection>
|
|
|
|
<Divider />
|
|
|
|
{!show ? (
|
|
|
|
<KeycloakDataTable
|
2021-09-29 08:51:47 +00:00
|
|
|
key={policies.length}
|
2021-09-23 13:41:29 +00:00
|
|
|
emptyState={
|
|
|
|
<ListEmptyState
|
|
|
|
message={t("realm-settings:noClientPolicies")}
|
|
|
|
instructions={t("realm-settings:noClientPoliciesInstructions")}
|
|
|
|
primaryActionText={t("realm-settings:createClientPolicy")}
|
2022-08-16 13:09:14 +00:00
|
|
|
onPrimaryAction={() => navigate(toAddClientPolicy({ realm }))}
|
2021-09-23 13:41:29 +00:00
|
|
|
/>
|
|
|
|
}
|
2021-09-29 08:51:47 +00:00
|
|
|
ariaLabelKey="realm-settings:clientPolicies"
|
2021-09-23 13:41:29 +00:00
|
|
|
searchPlaceholderKey="realm-settings:clientPolicySearch"
|
|
|
|
loader={loader}
|
|
|
|
toolbarItem={
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button
|
|
|
|
id="createPolicy"
|
2021-10-12 15:03:56 +00:00
|
|
|
component={(props) => (
|
2021-10-29 14:19:57 +00:00
|
|
|
<Link {...props} to={toAddClientPolicy({ realm })} />
|
2021-10-12 15:03:56 +00:00
|
|
|
)}
|
2021-09-23 13:41:29 +00:00
|
|
|
data-testid="createPolicy"
|
|
|
|
>
|
|
|
|
{t("createClientPolicy")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
}
|
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
title: t("common:delete"),
|
|
|
|
onRowClick: (item) => {
|
|
|
|
toggleDeleteDialog();
|
|
|
|
setSelectedPolicy(item);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "name",
|
2021-10-13 20:54:51 +00:00
|
|
|
cellRenderer: ClientPolicyDetailLink,
|
2021-09-23 13:41:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "enabled",
|
2021-11-24 16:19:02 +00:00
|
|
|
displayKey: "realm-settings:status",
|
2022-03-14 10:16:43 +00:00
|
|
|
cellRenderer: (clientPolicy) => (
|
|
|
|
<SwitchRenderer clientPolicy={clientPolicy} />
|
|
|
|
),
|
2021-09-23 13:41:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description",
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<div className="pf-u-mt-md pf-u-ml-lg">
|
|
|
|
<CodeEditor
|
|
|
|
isLineNumbersVisible
|
|
|
|
isLanguageLabelVisible
|
2021-10-12 15:03:56 +00:00
|
|
|
isReadOnly={false}
|
2021-09-23 13:41:29 +00:00
|
|
|
code={code}
|
|
|
|
language={Language.json}
|
|
|
|
height="30rem"
|
2022-03-14 10:16:43 +00:00
|
|
|
onChange={setCode}
|
2021-09-23 13:41:29 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="pf-u-mt-md">
|
|
|
|
<Button
|
|
|
|
variant={ButtonVariant.primary}
|
|
|
|
className="pf-u-mr-md pf-u-ml-lg"
|
2021-10-12 15:03:56 +00:00
|
|
|
data-testid="jsonEditor-policies-saveBtn"
|
|
|
|
onClick={save}
|
2021-09-23 13:41:29 +00:00
|
|
|
>
|
|
|
|
{t("save")}
|
|
|
|
</Button>
|
2021-10-12 15:03:56 +00:00
|
|
|
<Button
|
2022-07-12 08:39:51 +00:00
|
|
|
variant={ButtonVariant.link}
|
2021-10-12 15:03:56 +00:00
|
|
|
data-testid="jsonEditor-reloadBtn"
|
|
|
|
onClick={() => {
|
|
|
|
setCode(prettyPrintJSON(tablePolicies));
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("reload")}
|
|
|
|
</Button>
|
2021-09-23 13:41:29 +00:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
2021-09-09 10:45:19 +00:00
|
|
|
);
|
|
|
|
};
|