2021-10-12 15:03:56 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
2021-10-21 18:40:02 +00:00
|
|
|
DataList,
|
|
|
|
DataListCell,
|
|
|
|
DataListItem,
|
|
|
|
DataListItemCells,
|
|
|
|
DataListItemRow,
|
2021-10-12 15:03:56 +00:00
|
|
|
Divider,
|
|
|
|
DropdownItem,
|
|
|
|
Flex,
|
|
|
|
FlexItem,
|
|
|
|
FormGroup,
|
|
|
|
PageSection,
|
|
|
|
Text,
|
|
|
|
TextArea,
|
|
|
|
TextInput,
|
|
|
|
TextVariants,
|
|
|
|
ValidatedOptions,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
import { FormAccess } from "../components/form-access/FormAccess";
|
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
2021-10-13 20:54:51 +00:00
|
|
|
import { Link, useHistory, useParams } from "react-router-dom";
|
2021-10-12 15:03:56 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
|
|
|
import { HelpItem } from "../components/help-enabler/HelpItem";
|
2021-10-21 18:40:02 +00:00
|
|
|
import { PlusCircleIcon, TrashIcon } from "@patternfly/react-icons";
|
2021-10-12 15:03:56 +00:00
|
|
|
import "./RealmSettingsSection.css";
|
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
|
|
|
import type ClientPolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyRepresentation";
|
|
|
|
import { toClientPolicies } from "./routes/ClientPolicies";
|
2021-10-21 18:40:02 +00:00
|
|
|
import { toNewClientPolicyCondition } from "./routes/AddCondition";
|
|
|
|
import { useServerInfo } from "../context/server-info/ServerInfoProvider";
|
2021-10-13 20:54:51 +00:00
|
|
|
import type { EditClientPolicyParams } from "./routes/EditClientPolicy";
|
2021-10-12 15:03:56 +00:00
|
|
|
|
|
|
|
type NewClientPolicyForm = Required<ClientPolicyRepresentation>;
|
|
|
|
|
|
|
|
const defaultValues: NewClientPolicyForm = {
|
|
|
|
name: "",
|
|
|
|
description: "",
|
|
|
|
conditions: [],
|
|
|
|
enabled: true,
|
|
|
|
profiles: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
export const NewClientPolicyForm = () => {
|
|
|
|
const { t } = useTranslation("realm-settings");
|
2021-10-13 20:54:51 +00:00
|
|
|
const { errors, reset: resetForm } = useForm<NewClientPolicyForm>({
|
2021-10-12 15:03:56 +00:00
|
|
|
defaultValues,
|
|
|
|
});
|
|
|
|
const { realm } = useRealm();
|
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
const adminClient = useAdminClient();
|
2021-10-21 18:40:02 +00:00
|
|
|
const [policies, setPolicies] = useState<ClientPolicyRepresentation[]>([]);
|
|
|
|
const [currentPolicy, setCurrentPolicy] =
|
|
|
|
useState<ClientPolicyRepresentation>();
|
2021-10-12 15:03:56 +00:00
|
|
|
const [
|
|
|
|
showAddConditionsAndProfilesForm,
|
|
|
|
setShowAddConditionsAndProfilesForm,
|
|
|
|
] = useState(false);
|
|
|
|
|
2021-10-21 18:40:02 +00:00
|
|
|
const [conditionToDelete, setConditionToDelete] =
|
|
|
|
useState<{ idx: number; name: string }>();
|
|
|
|
|
|
|
|
const { policyName } = useParams<EditClientPolicyParams>();
|
2021-10-12 15:03:56 +00:00
|
|
|
|
|
|
|
const history = useHistory();
|
2021-10-13 20:54:51 +00:00
|
|
|
const form = useForm<ClientPolicyRepresentation>({ mode: "onChange" });
|
2021-10-21 18:40:02 +00:00
|
|
|
const { handleSubmit } = form;
|
|
|
|
|
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
const refresh = () => setKey(new Date().getTime());
|
2021-10-12 15:03:56 +00:00
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() => adminClient.clientPolicies.listPolicies(),
|
|
|
|
(policies) => {
|
2021-10-13 20:54:51 +00:00
|
|
|
const currentPolicy = policies.policies?.find(
|
|
|
|
(item) => item.name === policyName
|
|
|
|
);
|
2021-10-21 18:40:02 +00:00
|
|
|
setPolicies(policies.policies ?? []);
|
2021-10-13 20:54:51 +00:00
|
|
|
if (currentPolicy) {
|
|
|
|
setupForm(currentPolicy);
|
2021-10-21 18:40:02 +00:00
|
|
|
setCurrentPolicy(currentPolicy);
|
|
|
|
setShowAddConditionsAndProfilesForm(true);
|
2021-10-13 20:54:51 +00:00
|
|
|
}
|
2021-10-12 15:03:56 +00:00
|
|
|
},
|
2021-10-21 18:40:02 +00:00
|
|
|
[key]
|
2021-10-12 15:03:56 +00:00
|
|
|
);
|
|
|
|
|
2021-10-13 20:54:51 +00:00
|
|
|
const setupForm = (policy: ClientPolicyRepresentation) => {
|
|
|
|
resetForm();
|
|
|
|
Object.entries(policy).map(([key, value]) => {
|
|
|
|
form.setValue(key, value);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-10-21 18:40:02 +00:00
|
|
|
const policy = policies.filter((policy) => policy.name === policyName);
|
|
|
|
const policyConditions = policy[0]?.conditions || [];
|
|
|
|
|
|
|
|
const serverInfo = useServerInfo();
|
|
|
|
|
|
|
|
const conditionTypes =
|
|
|
|
serverInfo.componentTypes?.[
|
|
|
|
"org.keycloak.services.clientpolicy.condition.ClientPolicyConditionProvider"
|
|
|
|
];
|
|
|
|
|
2021-10-13 20:54:51 +00:00
|
|
|
const save = async () => {
|
|
|
|
const createdForm = form.getValues();
|
2021-10-12 15:03:56 +00:00
|
|
|
const createdPolicy = {
|
2021-10-13 20:54:51 +00:00
|
|
|
...createdForm,
|
2021-10-12 15:03:56 +00:00
|
|
|
profiles: [],
|
|
|
|
conditions: [],
|
|
|
|
};
|
|
|
|
|
2021-10-13 20:54:51 +00:00
|
|
|
const policyNameExists = policies.find(
|
|
|
|
(policy) => policy.name === createdPolicy.name
|
|
|
|
);
|
|
|
|
|
|
|
|
const res = policies.map((policy) =>
|
|
|
|
policy.name === createdPolicy.name ? createdPolicy : policy
|
|
|
|
);
|
|
|
|
|
|
|
|
const allPolicies = policyNameExists ? res : policies.concat(createdForm);
|
2021-10-12 15:03:56 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.updatePolicy({
|
|
|
|
policies: allPolicies,
|
|
|
|
});
|
|
|
|
addAlert(
|
|
|
|
t("realm-settings:createClientPolicySuccess"),
|
|
|
|
AlertVariant.success
|
|
|
|
);
|
2021-10-21 18:40:02 +00:00
|
|
|
history.push(
|
|
|
|
`/${realm}/realm-settings/clientPolicies/${
|
|
|
|
form.getValues().name
|
|
|
|
}/edit-policy`
|
|
|
|
);
|
2021-10-12 15:03:56 +00:00
|
|
|
setShowAddConditionsAndProfilesForm(true);
|
2021-10-21 18:40:02 +00:00
|
|
|
refresh();
|
2021-10-12 15:03:56 +00:00
|
|
|
} catch (error) {
|
2021-10-21 18:40:02 +00:00
|
|
|
addError("realm-settings:createClientPolicyError", error);
|
2021-10-12 15:03:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
2021-10-19 14:31:27 +00:00
|
|
|
titleKey: t("deleteClientPolicyConfirmTitle"),
|
|
|
|
messageKey: t("deleteClientPolicyConfirm", {
|
|
|
|
policyName: policyName,
|
|
|
|
}),
|
2021-10-12 15:03:56 +00:00
|
|
|
continueButtonLabel: t("delete"),
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
const updatedPolicies = policies.filter(
|
2021-10-19 14:31:27 +00:00
|
|
|
(policy) => policy.name !== policyName
|
2021-10-12 15:03:56 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.updatePolicy({
|
|
|
|
policies: updatedPolicies,
|
|
|
|
});
|
2021-10-19 14:31:27 +00:00
|
|
|
addAlert(t("deleteClientPolicySuccess"), AlertVariant.success);
|
2021-10-12 15:03:56 +00:00
|
|
|
history.push(toClientPolicies({ realm }));
|
|
|
|
} catch (error) {
|
2021-10-19 14:31:27 +00:00
|
|
|
addError(t("deleteClientPolicyError"), error);
|
2021-10-12 15:03:56 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-10-21 18:40:02 +00:00
|
|
|
const [toggleDeleteConditionDialog, DeleteConditionConfirm] =
|
|
|
|
useConfirmDialog({
|
|
|
|
titleKey: t("deleteClientPolicyConditionConfirmTitle"),
|
|
|
|
messageKey: t("deleteClientPolicyConditionConfirm", {
|
|
|
|
condition: conditionToDelete?.name,
|
|
|
|
}),
|
|
|
|
continueButtonLabel: t("delete"),
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
if (conditionToDelete?.name) {
|
|
|
|
currentPolicy?.conditions?.splice(conditionToDelete.idx!, 1);
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.updatePolicy({
|
|
|
|
policies: policies,
|
|
|
|
});
|
|
|
|
addAlert(t("deleteConditionSuccess"), AlertVariant.success);
|
|
|
|
history.push(
|
|
|
|
`/${realm}/realm-settings/clientPolicies/${
|
|
|
|
form.getValues().name
|
|
|
|
}/edit-policy`
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
addError(t("deleteConditionError"), error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const updatedPolicies = policies.filter(
|
|
|
|
(policy) => policy.name !== policyName
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.updatePolicy({
|
|
|
|
policies: updatedPolicies,
|
|
|
|
});
|
|
|
|
addAlert(t("deleteClientSuccess"), AlertVariant.success);
|
|
|
|
history.push(toClientPolicies({ realm }));
|
|
|
|
} catch (error) {
|
|
|
|
addError(t("deleteClientError"), error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const reset = () => {
|
|
|
|
form.setValue("name", currentPolicy?.name);
|
|
|
|
form.setValue("description", currentPolicy?.description);
|
|
|
|
};
|
|
|
|
|
2021-10-12 15:03:56 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DeleteConfirm />
|
2021-10-21 18:40:02 +00:00
|
|
|
<DeleteConditionConfirm />
|
2021-10-12 15:03:56 +00:00
|
|
|
<ViewHeader
|
|
|
|
titleKey={
|
2021-10-13 20:54:51 +00:00
|
|
|
showAddConditionsAndProfilesForm || policyName
|
2021-10-21 18:40:02 +00:00
|
|
|
? policyName!
|
2021-10-12 15:03:56 +00:00
|
|
|
: t("createPolicy")
|
|
|
|
}
|
|
|
|
divider
|
|
|
|
dropdownItems={
|
2021-10-19 14:31:27 +00:00
|
|
|
showAddConditionsAndProfilesForm || policyName
|
2021-10-12 15:03:56 +00:00
|
|
|
? [
|
|
|
|
<DropdownItem
|
|
|
|
key="delete"
|
|
|
|
value="delete"
|
|
|
|
onClick={() => {
|
2021-10-19 14:31:27 +00:00
|
|
|
toggleDeleteDialog();
|
2021-10-12 15:03:56 +00:00
|
|
|
}}
|
2021-10-19 14:31:27 +00:00
|
|
|
data-testid="deleteClientPolicyDropdown"
|
2021-10-12 15:03:56 +00:00
|
|
|
>
|
2021-10-19 14:31:27 +00:00
|
|
|
{t("deleteClientPolicy")}
|
2021-10-12 15:03:56 +00:00
|
|
|
</DropdownItem>,
|
|
|
|
]
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<PageSection variant="light">
|
2021-10-21 18:40:02 +00:00
|
|
|
<FormAccess
|
|
|
|
onSubmit={handleSubmit(save)}
|
|
|
|
isHorizontal
|
|
|
|
role="view-realm"
|
|
|
|
className="pf-u-mt-lg"
|
|
|
|
>
|
2021-10-12 15:03:56 +00:00
|
|
|
<FormGroup
|
|
|
|
label={t("common:name")}
|
|
|
|
fieldId="kc-name"
|
|
|
|
isRequired
|
|
|
|
helperTextInvalid={t("common:required")}
|
|
|
|
validated={
|
|
|
|
errors.name ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<TextInput
|
2021-10-13 20:54:51 +00:00
|
|
|
ref={form.register({ required: true })}
|
2021-10-12 15:03:56 +00:00
|
|
|
type="text"
|
|
|
|
id="kc-client-profile-name"
|
|
|
|
name="name"
|
|
|
|
data-testid="client-policy-name"
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup label={t("common:description")} fieldId="kc-description">
|
|
|
|
<TextArea
|
|
|
|
name="description"
|
|
|
|
aria-label={t("description")}
|
2021-10-13 20:54:51 +00:00
|
|
|
ref={form.register()}
|
2021-10-12 15:03:56 +00:00
|
|
|
type="text"
|
|
|
|
id="kc-client-policy-description"
|
|
|
|
data-testid="client-policy-description"
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<ActionGroup>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
2021-10-21 18:40:02 +00:00
|
|
|
type="submit"
|
2021-10-12 15:03:56 +00:00
|
|
|
data-testid="saveCreatePolicy"
|
|
|
|
>
|
|
|
|
{t("common:save")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
id="cancelCreatePolicy"
|
2021-10-13 20:54:51 +00:00
|
|
|
variant="secondary"
|
2021-10-12 15:03:56 +00:00
|
|
|
onClick={() =>
|
2021-10-21 18:40:02 +00:00
|
|
|
showAddConditionsAndProfilesForm || policyName
|
|
|
|
? reset()
|
2021-10-12 15:03:56 +00:00
|
|
|
: history.push(toClientPolicies({ realm }))
|
|
|
|
}
|
|
|
|
data-testid="cancelCreatePolicy"
|
|
|
|
>
|
|
|
|
{showAddConditionsAndProfilesForm
|
2021-10-21 18:40:02 +00:00
|
|
|
? t("common:revert")
|
2021-10-12 15:03:56 +00:00
|
|
|
: t("common:cancel")}
|
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
2021-10-21 18:40:02 +00:00
|
|
|
{(showAddConditionsAndProfilesForm || form.formState.isSubmitted) && (
|
2021-10-12 15:03:56 +00:00
|
|
|
<>
|
|
|
|
<Flex>
|
|
|
|
<FlexItem>
|
|
|
|
<Text className="kc-conditions" component={TextVariants.h1}>
|
|
|
|
{t("conditions")}
|
|
|
|
<HelpItem
|
|
|
|
helpText={t("realm-settings-help:conditions")}
|
|
|
|
forLabel={t("conditionsHelpItem")}
|
|
|
|
forID={t("conditions")}
|
|
|
|
/>
|
|
|
|
</Text>
|
|
|
|
</FlexItem>
|
|
|
|
<FlexItem align={{ default: "alignRight" }}>
|
|
|
|
<Button
|
|
|
|
id="addCondition"
|
|
|
|
component={(props) => (
|
|
|
|
<Link
|
|
|
|
{...props}
|
2021-10-21 18:40:02 +00:00
|
|
|
to={toNewClientPolicyCondition({
|
|
|
|
realm,
|
|
|
|
policyName: form.getValues().name!,
|
|
|
|
})}
|
2021-10-12 15:03:56 +00:00
|
|
|
></Link>
|
|
|
|
)}
|
|
|
|
variant="link"
|
|
|
|
className="kc-addCondition"
|
|
|
|
data-testid="cancelCreateProfile"
|
|
|
|
icon={<PlusCircleIcon />}
|
|
|
|
>
|
|
|
|
{t("realm-settings:addCondition")}
|
|
|
|
</Button>
|
|
|
|
</FlexItem>
|
|
|
|
</Flex>
|
2021-10-21 18:40:02 +00:00
|
|
|
{policyConditions.length > 0 ? (
|
|
|
|
<DataList aria-label={t("conditions")} isCompact>
|
|
|
|
{policyConditions.map((condition, idx) => (
|
|
|
|
<DataListItem
|
|
|
|
aria-labelledby={"conditions-list-item"}
|
|
|
|
key={`list-item-${idx}`}
|
|
|
|
id={condition.condition}
|
|
|
|
>
|
|
|
|
<DataListItemRow data-testid="conditions-list-row">
|
|
|
|
<DataListItemCells
|
|
|
|
dataListCells={[
|
|
|
|
<DataListCell
|
|
|
|
key={`name-${idx}`}
|
|
|
|
data-testid="condition-type"
|
|
|
|
>
|
|
|
|
{Object.keys(condition.configuration!).length !==
|
|
|
|
0 ? (
|
|
|
|
<Link
|
|
|
|
key={condition.condition}
|
|
|
|
data-testid="condition-type-link"
|
|
|
|
to={""}
|
|
|
|
className="kc-condition-link"
|
|
|
|
>
|
|
|
|
{condition.condition}
|
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
condition.condition
|
|
|
|
)}
|
|
|
|
{conditionTypes?.map(
|
|
|
|
(type) =>
|
|
|
|
type.id === condition.condition && (
|
|
|
|
<>
|
|
|
|
<HelpItem
|
|
|
|
helpText={type.helpText}
|
|
|
|
forLabel={t("conditionTypeHelpText")}
|
|
|
|
forID={t(`common:helpLabel`, {
|
|
|
|
label: t("conditionTypeHelpText"),
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
isInline
|
|
|
|
icon={
|
|
|
|
<TrashIcon
|
|
|
|
className="kc-conditionType-trash-icon"
|
|
|
|
data-testid="deleteClientProfileDropdown"
|
|
|
|
onClick={() => {
|
|
|
|
toggleDeleteConditionDialog();
|
|
|
|
setConditionToDelete({
|
|
|
|
idx: idx,
|
|
|
|
name: type.id!,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
></Button>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
</DataListCell>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</DataListItemRow>
|
|
|
|
</DataListItem>
|
|
|
|
))}
|
|
|
|
</DataList>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<Divider />
|
|
|
|
<Text
|
|
|
|
className="kc-emptyConditions"
|
|
|
|
component={TextVariants.h6}
|
|
|
|
>
|
|
|
|
{t("realm-settings:emptyConditions")}
|
|
|
|
</Text>
|
|
|
|
</>
|
|
|
|
)}
|
2021-10-12 15:03:56 +00:00
|
|
|
</>
|
|
|
|
)}
|
2021-10-21 18:40:02 +00:00
|
|
|
{(showAddConditionsAndProfilesForm || form.formState.isSubmitted) && (
|
2021-10-12 15:03:56 +00:00
|
|
|
<>
|
|
|
|
<Flex>
|
|
|
|
<FlexItem>
|
|
|
|
<Text
|
|
|
|
className="kc-client-profiles"
|
|
|
|
component={TextVariants.h1}
|
|
|
|
>
|
|
|
|
{t("clientProfiles")}
|
|
|
|
<HelpItem
|
|
|
|
helpText={t("realm-settings-help:clientProfiles")}
|
|
|
|
forLabel={t("clientProfilesHelpItem")}
|
|
|
|
forID={t("clientProfiles")}
|
|
|
|
/>
|
|
|
|
</Text>
|
|
|
|
</FlexItem>
|
|
|
|
<FlexItem align={{ default: "alignRight" }}>
|
|
|
|
<Button
|
|
|
|
id="addExecutor"
|
|
|
|
variant="link"
|
|
|
|
className="kc-addClientProfile"
|
|
|
|
data-testid="cancelCreateProfile"
|
|
|
|
icon={<PlusCircleIcon />}
|
|
|
|
>
|
|
|
|
{t("realm-settings:addClientProfile")}
|
|
|
|
</Button>
|
|
|
|
</FlexItem>
|
|
|
|
</Flex>
|
|
|
|
<Divider />
|
|
|
|
<Text
|
|
|
|
className="kc-emptyClientProfiles"
|
|
|
|
component={TextVariants.h6}
|
|
|
|
>
|
|
|
|
{t("realm-settings:emptyProfiles")}
|
|
|
|
</Text>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</FormAccess>
|
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|