2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2021-10-21 18:40:02 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2021-11-01 22:18:27 +00:00
|
|
|
import { Controller, FormProvider, useForm } from "react-hook-form";
|
2021-10-21 18:40:02 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
FormGroup,
|
|
|
|
PageSection,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
|
|
|
import { FormAccess } from "../components/form-access/FormAccess";
|
|
|
|
import { FormPanel } from "../components/scroll-form/FormPanel";
|
|
|
|
import { HelpItem } from "../components/help-enabler/HelpItem";
|
|
|
|
import { useServerInfo } from "../context/server-info/ServerInfoProvider";
|
|
|
|
import type ClientPolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyRepresentation";
|
2022-02-02 10:33:57 +00:00
|
|
|
import { camelCase } from "lodash-es";
|
2021-10-21 18:40:02 +00:00
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2022-12-07 14:23:12 +00:00
|
|
|
import { useParams, useNavigate } from "react-router-dom-v5-compat";
|
2021-10-21 18:40:02 +00:00
|
|
|
import type ComponentTypeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentTypeRepresentation";
|
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2021-11-01 22:18:27 +00:00
|
|
|
import type { ConfigPropertyRepresentation } from "@keycloak/keycloak-admin-client/lib/defs/authenticatorConfigInfoRepresentation";
|
|
|
|
import type ClientPolicyConditionRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyConditionRepresentation";
|
2022-10-06 10:29:16 +00:00
|
|
|
import { toEditClientPolicy } from "./routes/EditClientPolicy";
|
2021-11-09 18:27:25 +00:00
|
|
|
import type { EditClientPolicyConditionParams } from "./routes/EditCondition";
|
2022-02-21 16:30:57 +00:00
|
|
|
import { DynamicComponents } from "../components/dynamic/DynamicComponents";
|
|
|
|
|
2021-11-01 22:18:27 +00:00
|
|
|
export type ItemType = { value: string };
|
2021-10-21 18:40:02 +00:00
|
|
|
|
2021-11-22 12:41:43 +00:00
|
|
|
type ConfigProperty = ConfigPropertyRepresentation & {
|
|
|
|
config: any;
|
|
|
|
};
|
|
|
|
|
2021-10-29 16:11:06 +00:00
|
|
|
export default function NewClientPolicyCondition() {
|
2021-10-21 18:40:02 +00:00
|
|
|
const { t } = useTranslation("realm-settings");
|
|
|
|
const { addAlert, addError } = useAlerts();
|
2022-08-16 13:09:14 +00:00
|
|
|
const navigate = useNavigate();
|
2021-10-21 18:40:02 +00:00
|
|
|
const { realm } = useRealm();
|
|
|
|
|
|
|
|
const [openConditionType, setOpenConditionType] = useState(false);
|
|
|
|
const [policies, setPolicies] = useState<ClientPolicyRepresentation[]>([]);
|
2021-11-23 14:59:04 +00:00
|
|
|
|
2021-10-21 18:40:02 +00:00
|
|
|
const [condition, setCondition] = useState<
|
|
|
|
ClientPolicyConditionRepresentation[]
|
|
|
|
>([]);
|
2021-11-09 18:27:25 +00:00
|
|
|
const [conditionData, setConditionData] =
|
|
|
|
useState<ClientPolicyConditionRepresentation>();
|
2021-10-21 18:40:02 +00:00
|
|
|
const [conditionType, setConditionType] = useState("");
|
2021-11-01 22:18:27 +00:00
|
|
|
const [conditionProperties, setConditionProperties] = useState<
|
|
|
|
ConfigPropertyRepresentation[]
|
|
|
|
>([]);
|
2021-10-21 18:40:02 +00:00
|
|
|
|
2022-10-06 10:29:16 +00:00
|
|
|
const { policyName, conditionName } =
|
|
|
|
useParams<EditClientPolicyConditionParams>();
|
2021-10-21 18:40:02 +00:00
|
|
|
|
|
|
|
const serverInfo = useServerInfo();
|
2022-02-21 16:30:57 +00:00
|
|
|
const form = useForm({
|
2021-11-22 12:41:43 +00:00
|
|
|
shouldUnregister: false,
|
|
|
|
});
|
2021-10-21 18:40:02 +00:00
|
|
|
|
|
|
|
const conditionTypes =
|
|
|
|
serverInfo.componentTypes?.[
|
|
|
|
"org.keycloak.services.clientpolicy.condition.ClientPolicyConditionProvider"
|
|
|
|
];
|
|
|
|
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-10-21 18:40:02 +00:00
|
|
|
|
2022-02-21 16:30:57 +00:00
|
|
|
const setupForm = (condition: ClientPolicyConditionRepresentation) => {
|
|
|
|
form.reset({ config: condition.configuration || {} });
|
2021-11-09 18:27:25 +00:00
|
|
|
};
|
|
|
|
|
2021-10-21 18:40:02 +00:00
|
|
|
useFetch(
|
|
|
|
() => adminClient.clientPolicies.listPolicies(),
|
2021-11-23 14:59:04 +00:00
|
|
|
|
2021-10-21 18:40:02 +00:00
|
|
|
(policies) => {
|
|
|
|
setPolicies(policies.policies ?? []);
|
2021-11-23 14:59:04 +00:00
|
|
|
|
2021-11-09 18:27:25 +00:00
|
|
|
if (conditionName) {
|
|
|
|
const currentPolicy = policies.policies?.find(
|
|
|
|
(item) => item.name === policyName
|
|
|
|
);
|
|
|
|
|
|
|
|
const typeAndConfigData = currentPolicy?.conditions?.find(
|
|
|
|
(item) => item.condition === conditionName
|
|
|
|
);
|
|
|
|
|
|
|
|
const currentCondition = conditionTypes?.find(
|
|
|
|
(condition) => condition.id === conditionName
|
|
|
|
);
|
|
|
|
|
|
|
|
setConditionData(typeAndConfigData!);
|
|
|
|
setConditionProperties(currentCondition?.properties!);
|
2022-02-21 16:30:57 +00:00
|
|
|
setupForm(typeAndConfigData!);
|
2021-11-09 18:27:25 +00:00
|
|
|
}
|
2021-10-21 18:40:02 +00:00
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2021-11-22 12:41:43 +00:00
|
|
|
const save = async (configPolicy: ConfigProperty) => {
|
|
|
|
const configValues = configPolicy.config;
|
2021-11-09 18:27:25 +00:00
|
|
|
|
2021-11-23 14:59:04 +00:00
|
|
|
const writeConfig = () => {
|
|
|
|
return conditionProperties.reduce((r: any, p) => {
|
2022-02-21 16:30:57 +00:00
|
|
|
r[p.name!] = configValues[p.name!];
|
2021-11-22 12:41:43 +00:00
|
|
|
return r;
|
|
|
|
}, {});
|
2021-11-23 14:59:04 +00:00
|
|
|
};
|
2021-11-09 18:27:25 +00:00
|
|
|
|
2021-11-01 22:18:27 +00:00
|
|
|
const updatedPolicies = policies.map((policy) => {
|
|
|
|
if (policy.name !== policyName) {
|
|
|
|
return policy;
|
|
|
|
}
|
|
|
|
|
2021-11-09 18:27:25 +00:00
|
|
|
let conditions = policy.conditions ?? [];
|
|
|
|
|
|
|
|
if (conditionName) {
|
|
|
|
const createdCondition = {
|
|
|
|
condition: conditionData?.condition,
|
|
|
|
configuration: writeConfig(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const index = conditions.findIndex(
|
|
|
|
(condition) => conditionName === condition.condition
|
|
|
|
);
|
|
|
|
|
|
|
|
if (index === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newConditions = [
|
|
|
|
...conditions.slice(0, index),
|
|
|
|
createdCondition,
|
|
|
|
...conditions.slice(index + 1),
|
|
|
|
];
|
|
|
|
|
|
|
|
return {
|
|
|
|
...policy,
|
|
|
|
conditions: newConditions,
|
|
|
|
};
|
|
|
|
}
|
2021-11-01 22:18:27 +00:00
|
|
|
|
2021-11-09 18:27:25 +00:00
|
|
|
conditions = conditions.concat({
|
2021-11-01 22:18:27 +00:00
|
|
|
condition: condition[0].condition,
|
|
|
|
configuration: writeConfig(),
|
|
|
|
});
|
2021-10-21 18:40:02 +00:00
|
|
|
|
2021-11-01 22:18:27 +00:00
|
|
|
return {
|
|
|
|
...policy,
|
|
|
|
conditions,
|
|
|
|
};
|
2021-11-09 18:27:25 +00:00
|
|
|
}) as ClientPolicyRepresentation[];
|
2021-10-21 18:40:02 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.updatePolicy({
|
2021-11-01 22:18:27 +00:00
|
|
|
policies: updatedPolicies,
|
2021-10-21 18:40:02 +00:00
|
|
|
});
|
2021-11-01 22:18:27 +00:00
|
|
|
setPolicies(updatedPolicies);
|
2022-10-06 10:29:16 +00:00
|
|
|
navigate(toEditClientPolicy({ realm, policyName: policyName! }));
|
2021-10-21 18:40:02 +00:00
|
|
|
addAlert(
|
2021-11-09 18:27:25 +00:00
|
|
|
conditionName
|
|
|
|
? t("realm-settings:updateClientConditionSuccess")
|
|
|
|
: t("realm-settings:createClientConditionSuccess"),
|
2021-10-21 18:40:02 +00:00
|
|
|
AlertVariant.success
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
addError("realm-settings:createClientConditionError", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PageSection variant="light">
|
2021-11-09 18:27:25 +00:00
|
|
|
<FormPanel
|
|
|
|
className="kc-login-screen"
|
|
|
|
title={conditionName ? t("editCondition") : t("addCondition")}
|
|
|
|
>
|
2021-10-21 18:40:02 +00:00
|
|
|
<FormAccess
|
|
|
|
isHorizontal
|
|
|
|
role="manage-realm"
|
|
|
|
className="pf-u-mt-lg"
|
2021-11-22 12:41:43 +00:00
|
|
|
onSubmit={form.handleSubmit(save)}
|
2021-10-21 18:40:02 +00:00
|
|
|
>
|
|
|
|
<FormGroup
|
|
|
|
label={t("conditionType")}
|
|
|
|
fieldId="conditionType"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={
|
|
|
|
conditionType
|
2021-12-14 14:56:36 +00:00
|
|
|
? `realm-settings-help:${camelCase(
|
|
|
|
conditionType.replace(/-/g, " ")
|
|
|
|
)}`
|
2022-08-24 16:47:58 +00:00
|
|
|
: "realm-settings-help:conditions"
|
2021-10-21 18:40:02 +00:00
|
|
|
}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="realm-settings:conditionType"
|
2021-10-21 18:40:02 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="conditions"
|
|
|
|
defaultValue={"any-client"}
|
2021-11-22 12:41:43 +00:00
|
|
|
control={form.control}
|
2021-10-21 18:40:02 +00:00
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
placeholderText={t("selectACondition")}
|
2021-11-22 12:41:43 +00:00
|
|
|
className="kc-conditionType-select"
|
2021-11-09 18:27:25 +00:00
|
|
|
data-testid="conditionType-select"
|
2021-10-21 18:40:02 +00:00
|
|
|
toggleId="provider"
|
2021-11-09 18:27:25 +00:00
|
|
|
isDisabled={!!conditionName}
|
2021-10-21 18:40:02 +00:00
|
|
|
onToggle={(toggle) => setOpenConditionType(toggle)}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
onChange(value);
|
2021-11-01 22:18:27 +00:00
|
|
|
setConditionProperties(
|
|
|
|
(value as ComponentTypeRepresentation).properties
|
|
|
|
);
|
2021-10-21 18:40:02 +00:00
|
|
|
setConditionType((value as ComponentTypeRepresentation).id);
|
|
|
|
setCondition([
|
|
|
|
{
|
|
|
|
condition: (value as ComponentTypeRepresentation).id,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
setOpenConditionType(false);
|
|
|
|
}}
|
2021-11-09 18:27:25 +00:00
|
|
|
selections={conditionName ? conditionName : conditionType}
|
2021-10-21 18:40:02 +00:00
|
|
|
variant={SelectVariant.single}
|
|
|
|
aria-label={t("conditionType")}
|
|
|
|
isOpen={openConditionType}
|
|
|
|
>
|
|
|
|
{conditionTypes?.map((condition) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={condition.id === value}
|
|
|
|
description={t(
|
2021-11-01 22:18:27 +00:00
|
|
|
`realm-settings-help:${camelCase(
|
|
|
|
condition.id.replace(/-/g, " ")
|
|
|
|
)}`
|
2021-10-21 18:40:02 +00:00
|
|
|
)}
|
|
|
|
key={condition.id}
|
|
|
|
value={condition}
|
|
|
|
>
|
|
|
|
{condition.id}
|
|
|
|
</SelectOption>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
2021-11-22 12:41:43 +00:00
|
|
|
|
2021-11-01 22:18:27 +00:00
|
|
|
<FormProvider {...form}>
|
2022-02-21 16:30:57 +00:00
|
|
|
<DynamicComponents properties={conditionProperties} />
|
2021-11-01 22:18:27 +00:00
|
|
|
</FormProvider>
|
2021-10-21 18:40:02 +00:00
|
|
|
<ActionGroup>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
2021-11-09 18:27:25 +00:00
|
|
|
data-testid="addCondition-saveBtn"
|
|
|
|
isDisabled={conditionType === "" && !conditionName}
|
2021-10-21 18:40:02 +00:00
|
|
|
>
|
2021-11-09 18:27:25 +00:00
|
|
|
{conditionName ? t("common:save") : t("common:add")}
|
2021-10-21 18:40:02 +00:00
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
2021-11-09 18:27:25 +00:00
|
|
|
data-testid="addCondition-cancelBtn"
|
2021-10-21 18:40:02 +00:00
|
|
|
onClick={() =>
|
2022-10-06 10:29:16 +00:00
|
|
|
navigate(toEditClientPolicy({ realm, policyName: policyName! }))
|
2021-10-21 18:40:02 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
|
|
|
</FormAccess>
|
|
|
|
</FormPanel>
|
|
|
|
</PageSection>
|
|
|
|
);
|
2021-10-29 16:11:06 +00:00
|
|
|
}
|