2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2021-10-12 15:03:56 +00:00
|
|
|
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,
|
|
|
|
TextVariants,
|
|
|
|
ValidatedOptions,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2021-11-09 19:36:27 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2021-10-12 15:03:56 +00:00
|
|
|
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";
|
2022-04-21 15:03:26 +00:00
|
|
|
import { KeycloakTextInput } from "../components/keycloak-text-input/KeycloakTextInput";
|
2022-05-11 09:46:14 +00:00
|
|
|
import { KeycloakTextArea } from "../components/keycloak-text-area/KeycloakTextArea";
|
2021-10-21 18:40:02 +00:00
|
|
|
import { PlusCircleIcon, TrashIcon } from "@patternfly/react-icons";
|
2021-10-12 15:03:56 +00:00
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
|
|
|
import type ClientPolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyRepresentation";
|
2021-10-21 18:40:02 +00:00
|
|
|
import { toNewClientPolicyCondition } from "./routes/AddCondition";
|
|
|
|
import { useServerInfo } from "../context/server-info/ServerInfoProvider";
|
2021-11-09 18:27:25 +00:00
|
|
|
import { toEditClientPolicyCondition } from "./routes/EditCondition";
|
2022-03-14 10:15:03 +00:00
|
|
|
import { toClientProfile } from "./routes/ClientProfile";
|
|
|
|
|
2022-02-08 22:10:46 +00:00
|
|
|
import {
|
|
|
|
EditClientPolicyParams,
|
|
|
|
toEditClientPolicy,
|
|
|
|
} from "./routes/EditClientPolicy";
|
2021-10-29 14:19:57 +00:00
|
|
|
import { AddClientProfileModal } from "./AddClientProfileModal";
|
|
|
|
import type ClientProfileRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientProfileRepresentation";
|
2022-01-31 07:19:44 +00:00
|
|
|
import { toClientPolicies } from "./routes/ClientPolicies";
|
2022-06-28 12:23:38 +00:00
|
|
|
import { KeycloakSpinner } from "../components/keycloak-spinner/KeycloakSpinner";
|
2022-01-31 07:19:44 +00:00
|
|
|
|
2022-02-16 11:39:08 +00:00
|
|
|
import "./realm-settings-section.css";
|
2021-10-12 15:03:56 +00:00
|
|
|
|
|
|
|
type NewClientPolicyForm = Required<ClientPolicyRepresentation>;
|
|
|
|
|
|
|
|
const defaultValues: NewClientPolicyForm = {
|
|
|
|
name: "",
|
|
|
|
description: "",
|
|
|
|
conditions: [],
|
|
|
|
enabled: true,
|
|
|
|
profiles: [],
|
|
|
|
};
|
|
|
|
|
2021-10-29 14:19:57 +00:00
|
|
|
type PolicyDetailAttributes = {
|
|
|
|
idx: number;
|
|
|
|
name: string;
|
|
|
|
};
|
|
|
|
|
2021-10-29 16:11:06 +00:00
|
|
|
export default function NewClientPolicyForm() {
|
2021-10-12 15:03:56 +00:00
|
|
|
const { t } = useTranslation("realm-settings");
|
2022-04-08 12:37:31 +00:00
|
|
|
const {
|
|
|
|
reset: resetForm,
|
|
|
|
formState: { errors },
|
|
|
|
} = useForm<NewClientPolicyForm>({
|
2021-10-12 15:03:56 +00:00
|
|
|
defaultValues,
|
|
|
|
});
|
|
|
|
const { realm } = useRealm();
|
|
|
|
const { addAlert, addError } = useAlerts();
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2022-06-28 12:23:38 +00:00
|
|
|
const [policies, setPolicies] = useState<ClientPolicyRepresentation[]>();
|
2021-10-29 14:19:57 +00:00
|
|
|
const [clientProfiles, setClientProfiles] = useState<
|
|
|
|
ClientProfileRepresentation[]
|
|
|
|
>([]);
|
|
|
|
|
2021-10-21 18:40:02 +00:00
|
|
|
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] =
|
2021-10-29 14:19:57 +00:00
|
|
|
useState<PolicyDetailAttributes>();
|
|
|
|
|
|
|
|
const [profilesModalOpen, setProfilesModalOpen] = useState(false);
|
|
|
|
|
|
|
|
const [profileToDelete, setProfileToDelete] =
|
|
|
|
useState<PolicyDetailAttributes>();
|
2021-10-21 18:40:02 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-11-02 17:11:04 +00:00
|
|
|
const formValues = form.getValues();
|
|
|
|
|
2021-11-09 19:36:27 +00:00
|
|
|
type ClientPoliciesHeaderProps = {
|
|
|
|
onChange: (value: boolean) => void;
|
|
|
|
value: boolean;
|
|
|
|
save: () => void;
|
|
|
|
realmName: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const ClientPoliciesHeader = ({
|
|
|
|
save,
|
|
|
|
onChange,
|
|
|
|
value,
|
|
|
|
}: ClientPoliciesHeaderProps) => {
|
|
|
|
const { t } = useTranslation("realm-settings");
|
|
|
|
|
|
|
|
const [toggleDisableDialog, DisableConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "realm-settings:disablePolicyConfirmTitle",
|
|
|
|
messageKey: "realm-settings:disablePolicyConfirm",
|
|
|
|
continueButtonLabel: "common:disable",
|
|
|
|
onConfirm: () => {
|
|
|
|
onChange(!value);
|
|
|
|
save();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-06-28 12:23:38 +00:00
|
|
|
if (!policies) {
|
|
|
|
return <KeycloakSpinner />;
|
|
|
|
}
|
|
|
|
|
2021-11-09 19:36:27 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DisableConfirm />
|
|
|
|
<DeleteConfirm />
|
|
|
|
<ViewHeader
|
|
|
|
titleKey={
|
|
|
|
showAddConditionsAndProfilesForm || policyName
|
|
|
|
? formValues.name!
|
2022-06-28 12:23:38 +00:00
|
|
|
: "realm-settings:createPolicy"
|
2021-11-09 19:36:27 +00:00
|
|
|
}
|
|
|
|
divider
|
|
|
|
dropdownItems={
|
|
|
|
showAddConditionsAndProfilesForm || policyName
|
|
|
|
? [
|
|
|
|
<DropdownItem
|
|
|
|
key="delete"
|
|
|
|
value="delete"
|
|
|
|
onClick={() => {
|
|
|
|
toggleDeleteDialog();
|
|
|
|
}}
|
|
|
|
data-testid="deleteClientPolicyDropdown"
|
|
|
|
>
|
|
|
|
{t("deleteClientPolicy")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
isEnabled={value}
|
|
|
|
onToggle={(value) => {
|
|
|
|
if (!value) {
|
|
|
|
toggleDisableDialog();
|
|
|
|
} else {
|
|
|
|
onChange(value);
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-10-12 15:03:56 +00:00
|
|
|
useFetch(
|
2021-10-29 14:19:57 +00:00
|
|
|
async () => {
|
|
|
|
const [policies, profiles] = await Promise.all([
|
|
|
|
adminClient.clientPolicies.listPolicies(),
|
|
|
|
adminClient.clientPolicies.listProfiles({
|
|
|
|
includeGlobalProfiles: true,
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return { policies, profiles };
|
|
|
|
},
|
|
|
|
({ policies, profiles }) => {
|
2021-10-13 20:54:51 +00:00
|
|
|
const currentPolicy = policies.policies?.find(
|
|
|
|
(item) => item.name === policyName
|
|
|
|
);
|
2021-10-29 14:19:57 +00:00
|
|
|
|
|
|
|
const allClientProfiles = [
|
|
|
|
...(profiles.globalProfiles ?? []),
|
|
|
|
...(profiles.profiles ?? []),
|
|
|
|
];
|
|
|
|
|
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-29 14:19:57 +00:00
|
|
|
setClientProfiles(allClientProfiles);
|
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
|
|
|
},
|
2022-02-02 11:44:52 +00:00
|
|
|
[]
|
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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-28 12:23:38 +00:00
|
|
|
const policy = (policies || []).filter(
|
|
|
|
(policy) => policy.name === policyName
|
|
|
|
);
|
2021-10-21 18:40:02 +00:00
|
|
|
const policyConditions = policy[0]?.conditions || [];
|
2021-10-29 14:19:57 +00:00
|
|
|
const policyProfiles = policy[0]?.profiles || [];
|
2021-10-21 18:40:02 +00:00
|
|
|
|
|
|
|
const serverInfo = useServerInfo();
|
|
|
|
|
|
|
|
const conditionTypes =
|
|
|
|
serverInfo.componentTypes?.[
|
|
|
|
"org.keycloak.services.clientpolicy.condition.ClientPolicyConditionProvider"
|
|
|
|
];
|
|
|
|
|
2021-10-13 20:54:51 +00:00
|
|
|
const save = async () => {
|
2021-11-01 22:18:27 +00:00
|
|
|
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-11-02 17:11:04 +00:00
|
|
|
const getAllPolicies = () => {
|
2022-06-28 12:23:38 +00:00
|
|
|
const policyNameExists = policies?.some(
|
2021-11-02 17:11:04 +00:00
|
|
|
(policy) => policy.name === createdPolicy.name
|
|
|
|
);
|
2021-10-13 20:54:51 +00:00
|
|
|
|
2021-11-02 17:11:04 +00:00
|
|
|
if (policyNameExists) {
|
2022-06-28 12:23:38 +00:00
|
|
|
return policies?.map((policy) =>
|
2021-11-02 17:11:04 +00:00
|
|
|
policy.name === createdPolicy.name ? createdPolicy : policy
|
|
|
|
);
|
|
|
|
} else if (createdForm.name !== policyName) {
|
|
|
|
return policies
|
2022-06-28 12:23:38 +00:00
|
|
|
?.filter((item) => item.name !== policyName)
|
2021-11-02 17:11:04 +00:00
|
|
|
.concat(createdForm);
|
|
|
|
}
|
2022-06-28 12:23:38 +00:00
|
|
|
return policies?.concat(createdForm);
|
2021-11-02 17:11:04 +00:00
|
|
|
};
|
2021-10-12 15:03:56 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.updatePolicy({
|
2021-11-02 17:11:04 +00:00
|
|
|
policies: getAllPolicies(),
|
2021-10-12 15:03:56 +00:00
|
|
|
});
|
|
|
|
addAlert(
|
2021-11-09 19:36:27 +00:00
|
|
|
policyName
|
|
|
|
? t("realm-settings:updateClientPolicySuccess")
|
|
|
|
: t("realm-settings:createClientPolicySuccess"),
|
2021-10-12 15:03:56 +00:00
|
|
|
AlertVariant.success
|
|
|
|
);
|
2021-10-21 18:40:02 +00:00
|
|
|
history.push(
|
2022-02-08 22:10:46 +00:00
|
|
|
toEditClientPolicy({ realm, policyName: createdForm.name! })
|
2021-10-21 18:40:02 +00:00
|
|
|
);
|
2021-10-12 15:03:56 +00:00
|
|
|
setShowAddConditionsAndProfilesForm(true);
|
|
|
|
} 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 () => {
|
2022-06-28 12:23:38 +00:00
|
|
|
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);
|
2022-01-20 15:34:58 +00:00
|
|
|
history.push(
|
2022-01-31 07:19:44 +00:00
|
|
|
toClientPolicies({
|
2022-01-20 15:34:58 +00:00
|
|
|
realm,
|
2022-01-31 07:19:44 +00:00
|
|
|
tab: "policies",
|
2022-01-20 15:34:58 +00:00
|
|
|
})
|
|
|
|
);
|
2021-10-12 15:03:56 +00:00
|
|
|
} 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(
|
2022-02-08 22:10:46 +00:00
|
|
|
toEditClientPolicy({ realm, policyName: formValues.name! })
|
2021-10-21 18:40:02 +00:00
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
addError(t("deleteConditionError"), error);
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-28 12:23:38 +00:00
|
|
|
const updatedPolicies = policies?.filter(
|
2021-10-21 18:40:02 +00:00
|
|
|
(policy) => policy.name !== policyName
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.updatePolicy({
|
|
|
|
policies: updatedPolicies,
|
|
|
|
});
|
|
|
|
addAlert(t("deleteClientSuccess"), AlertVariant.success);
|
2022-01-20 15:34:58 +00:00
|
|
|
history.push(
|
2022-01-31 07:19:44 +00:00
|
|
|
toClientPolicies({
|
2022-01-20 15:34:58 +00:00
|
|
|
realm,
|
2022-01-31 07:19:44 +00:00
|
|
|
tab: "policies",
|
2022-01-20 15:34:58 +00:00
|
|
|
})
|
|
|
|
);
|
2021-10-21 18:40:02 +00:00
|
|
|
} catch (error) {
|
|
|
|
addError(t("deleteClientError"), error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-10-29 14:19:57 +00:00
|
|
|
const [toggleDeleteProfileDialog, DeleteProfileConfirm] = useConfirmDialog({
|
|
|
|
titleKey: t("deleteClientPolicyProfileConfirmTitle"),
|
|
|
|
messageKey: t("deleteClientPolicyProfileConfirm", {
|
|
|
|
profileName: profileToDelete?.name,
|
|
|
|
policyName,
|
|
|
|
}),
|
|
|
|
continueButtonLabel: t("delete"),
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
if (profileToDelete?.name) {
|
|
|
|
currentPolicy?.profiles?.splice(profileToDelete.idx!, 1);
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.updatePolicy({
|
|
|
|
policies: policies,
|
|
|
|
});
|
|
|
|
addAlert(t("deleteClientPolicyProfileSuccess"), AlertVariant.success);
|
|
|
|
history.push(
|
2022-02-08 22:10:46 +00:00
|
|
|
toEditClientPolicy({ realm, policyName: formValues.name! })
|
2021-10-29 14:19:57 +00:00
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
addError(t("deleteClientPolicyProfileError"), error);
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-28 12:23:38 +00:00
|
|
|
const updatedPolicies = policies?.filter(
|
2021-10-29 14:19:57 +00:00
|
|
|
(policy) => policy.name !== policyName
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.updatePolicy({
|
|
|
|
policies: updatedPolicies,
|
|
|
|
});
|
|
|
|
addAlert(t("deleteClientSuccess"), AlertVariant.success);
|
2022-01-20 15:34:58 +00:00
|
|
|
history.push(
|
2022-01-31 07:19:44 +00:00
|
|
|
toClientPolicies({
|
2022-01-20 15:34:58 +00:00
|
|
|
realm,
|
2022-01-31 07:19:44 +00:00
|
|
|
tab: "policies",
|
2022-01-20 15:34:58 +00:00
|
|
|
})
|
|
|
|
);
|
2021-10-29 14:19:57 +00:00
|
|
|
} catch (error) {
|
|
|
|
addError(t("deleteClientError"), error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-10-21 18:40:02 +00:00
|
|
|
const reset = () => {
|
|
|
|
form.setValue("name", currentPolicy?.name);
|
|
|
|
form.setValue("description", currentPolicy?.description);
|
|
|
|
};
|
|
|
|
|
2021-10-29 14:19:57 +00:00
|
|
|
const toggleModal = () => {
|
|
|
|
setProfilesModalOpen(!profilesModalOpen);
|
|
|
|
};
|
|
|
|
|
|
|
|
const addProfiles = async (profiles: string[]) => {
|
|
|
|
const createdPolicy = {
|
|
|
|
...currentPolicy,
|
2022-02-18 10:51:03 +00:00
|
|
|
profiles: policyProfiles.concat(profiles),
|
2021-10-29 14:19:57 +00:00
|
|
|
conditions: currentPolicy?.conditions,
|
|
|
|
};
|
|
|
|
|
2022-06-28 12:23:38 +00:00
|
|
|
const index = policies?.findIndex(
|
2021-10-29 14:19:57 +00:00
|
|
|
(policy) => createdPolicy.name === policy.name
|
|
|
|
);
|
|
|
|
|
2022-06-28 12:23:38 +00:00
|
|
|
if (!index || index === -1) {
|
2021-10-29 14:19:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newPolicies = [
|
2022-06-28 12:23:38 +00:00
|
|
|
...(policies || []).slice(0, index),
|
2021-10-29 14:19:57 +00:00
|
|
|
createdPolicy,
|
2022-06-28 12:23:38 +00:00
|
|
|
...(policies || []).slice(index + 1),
|
2021-10-29 14:19:57 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.updatePolicy({
|
|
|
|
policies: newPolicies,
|
|
|
|
});
|
|
|
|
setPolicies(newPolicies);
|
2022-02-08 22:10:46 +00:00
|
|
|
history.push(toEditClientPolicy({ realm, policyName: formValues.name! }));
|
2021-10-29 14:19:57 +00:00
|
|
|
addAlert(
|
|
|
|
t("realm-settings:addClientProfileSuccess"),
|
|
|
|
AlertVariant.success
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
addError("realm-settings:addClientProfileError", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-12 15:03:56 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-10-21 18:40:02 +00:00
|
|
|
<DeleteConditionConfirm />
|
2021-10-29 14:19:57 +00:00
|
|
|
<DeleteProfileConfirm />
|
|
|
|
<AddClientProfileModal
|
|
|
|
onConfirm={(profiles: ClientProfileRepresentation[]) => {
|
|
|
|
addProfiles(profiles.map((item) => item.name!));
|
|
|
|
}}
|
|
|
|
allProfiles={policyProfiles}
|
|
|
|
open={profilesModalOpen}
|
|
|
|
toggleDialog={toggleModal}
|
|
|
|
/>
|
2021-11-09 19:36:27 +00:00
|
|
|
<Controller
|
|
|
|
name="enabled"
|
|
|
|
defaultValue={true}
|
|
|
|
control={form.control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<ClientPoliciesHeader
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
realmName={realm}
|
|
|
|
save={save}
|
|
|
|
/>
|
|
|
|
)}
|
2021-10-12 15:03:56 +00:00
|
|
|
/>
|
|
|
|
<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")}
|
2022-06-24 11:43:59 +00:00
|
|
|
fieldId="kc-client-profile-name"
|
2021-10-12 15:03:56 +00:00
|
|
|
isRequired
|
|
|
|
helperTextInvalid={t("common:required")}
|
|
|
|
validated={
|
|
|
|
errors.name ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
|
|
|
>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
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">
|
2022-05-11 09:46:14 +00:00
|
|
|
<KeycloakTextArea
|
2021-10-12 15:03:56 +00:00
|
|
|
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"
|
2021-10-29 14:19:57 +00:00
|
|
|
isDisabled={!formValues.name}
|
2021-10-12 15:03:56 +00:00
|
|
|
>
|
|
|
|
{t("common:save")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
id="cancelCreatePolicy"
|
2022-07-12 08:39:51 +00:00
|
|
|
variant="link"
|
2021-10-12 15:03:56 +00:00
|
|
|
onClick={() =>
|
2021-10-21 18:40:02 +00:00
|
|
|
showAddConditionsAndProfilesForm || policyName
|
|
|
|
? reset()
|
2022-01-20 15:34:58 +00:00
|
|
|
: history.push(
|
2022-01-31 07:19:44 +00:00
|
|
|
toClientPolicies({
|
2022-01-20 15:34:58 +00:00
|
|
|
realm,
|
2022-01-31 07:19:44 +00:00
|
|
|
tab: "policies",
|
2022-01-20 15:34:58 +00:00
|
|
|
})
|
|
|
|
)
|
2021-10-12 15:03:56 +00:00
|
|
|
}
|
|
|
|
data-testid="cancelCreatePolicy"
|
|
|
|
>
|
|
|
|
{showAddConditionsAndProfilesForm
|
2022-07-12 08:39:51 +00:00
|
|
|
? t("common:reload")
|
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
|
2021-12-14 14:56:36 +00:00
|
|
|
helpText="realm-settings:realm-settings-help:conditions"
|
|
|
|
fieldLabelId="realm-settings:conditions"
|
2021-10-12 15:03:56 +00:00
|
|
|
/>
|
|
|
|
</Text>
|
|
|
|
</FlexItem>
|
|
|
|
<FlexItem align={{ default: "alignRight" }}>
|
|
|
|
<Button
|
|
|
|
id="addCondition"
|
|
|
|
component={(props) => (
|
|
|
|
<Link
|
|
|
|
{...props}
|
2021-10-21 18:40:02 +00:00
|
|
|
to={toNewClientPolicyCondition({
|
|
|
|
realm,
|
2021-10-29 14:19:57 +00:00
|
|
|
policyName: formValues.name!,
|
2021-10-21 18:40:02 +00:00
|
|
|
})}
|
2021-10-12 15:03:56 +00:00
|
|
|
></Link>
|
|
|
|
)}
|
|
|
|
variant="link"
|
|
|
|
className="kc-addCondition"
|
2021-11-09 18:27:25 +00:00
|
|
|
data-testid="addCondition"
|
2021-10-12 15:03:56 +00:00
|
|
|
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
|
2021-10-29 14:19:57 +00:00
|
|
|
aria-labelledby="conditions-list-item"
|
2021-10-21 18:40:02 +00:00
|
|
|
key={`list-item-${idx}`}
|
|
|
|
id={condition.condition}
|
2021-10-29 14:19:57 +00:00
|
|
|
data-testid="conditions-list-item"
|
2021-10-21 18:40:02 +00:00
|
|
|
>
|
|
|
|
<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}
|
2021-11-23 14:59:04 +00:00
|
|
|
data-testid={`${condition.condition}-condition-link`}
|
2021-11-09 18:27:25 +00:00
|
|
|
to={toEditClientPolicyCondition({
|
|
|
|
realm,
|
|
|
|
conditionName: condition.condition!,
|
|
|
|
policyName: policyName,
|
|
|
|
})}
|
2021-10-21 18:40:02 +00:00
|
|
|
className="kc-condition-link"
|
|
|
|
>
|
|
|
|
{condition.condition}
|
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
condition.condition
|
|
|
|
)}
|
|
|
|
{conditionTypes?.map(
|
|
|
|
(type) =>
|
|
|
|
type.id === condition.condition && (
|
|
|
|
<>
|
|
|
|
<HelpItem
|
|
|
|
helpText={type.helpText}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId={condition.condition}
|
2021-10-21 18:40:02 +00:00
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
isInline
|
|
|
|
icon={
|
|
|
|
<TrashIcon
|
|
|
|
className="kc-conditionType-trash-icon"
|
2021-11-23 14:59:04 +00:00
|
|
|
data-testid={`delete-${condition.condition}-condition`}
|
2021-10-21 18:40:02 +00:00
|
|
|
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
|
2021-12-14 14:56:36 +00:00
|
|
|
helpText="realm-settings-help:clientProfiles"
|
|
|
|
fieldLabelId="realm-settings:clientProfiles"
|
2021-10-12 15:03:56 +00:00
|
|
|
/>
|
|
|
|
</Text>
|
|
|
|
</FlexItem>
|
|
|
|
<FlexItem align={{ default: "alignRight" }}>
|
|
|
|
<Button
|
2021-10-29 14:19:57 +00:00
|
|
|
id="addClientProfile"
|
2021-10-12 15:03:56 +00:00
|
|
|
variant="link"
|
|
|
|
className="kc-addClientProfile"
|
2021-11-09 18:27:25 +00:00
|
|
|
data-testid="addClientProfile"
|
2021-10-12 15:03:56 +00:00
|
|
|
icon={<PlusCircleIcon />}
|
2021-10-29 14:19:57 +00:00
|
|
|
onClick={toggleModal}
|
2021-10-12 15:03:56 +00:00
|
|
|
>
|
|
|
|
{t("realm-settings:addClientProfile")}
|
|
|
|
</Button>
|
|
|
|
</FlexItem>
|
|
|
|
</Flex>
|
2021-10-29 14:19:57 +00:00
|
|
|
{policyProfiles.length > 0 ? (
|
|
|
|
<DataList aria-label={t("profiles")} isCompact>
|
|
|
|
{policyProfiles.map((profile, idx) => (
|
|
|
|
<DataListItem
|
|
|
|
aria-labelledby={`${profile}-profile-list-item`}
|
|
|
|
key={profile}
|
|
|
|
id={`${profile}-profile-list-item`}
|
|
|
|
data-testid={"profile-list-item"}
|
|
|
|
>
|
|
|
|
<DataListItemRow data-testid="profile-list-row">
|
|
|
|
<DataListItemCells
|
|
|
|
dataListCells={[
|
|
|
|
<DataListCell key="name" data-testid="profile-name">
|
|
|
|
{profile && (
|
|
|
|
<Link
|
|
|
|
key={profile}
|
|
|
|
data-testid="profile-name-link"
|
2022-03-14 10:15:03 +00:00
|
|
|
to={toClientProfile({
|
|
|
|
realm,
|
|
|
|
profileName: profile,
|
|
|
|
})}
|
2021-10-29 14:19:57 +00:00
|
|
|
className="kc-profile-link"
|
|
|
|
>
|
|
|
|
{profile}
|
|
|
|
</Link>
|
|
|
|
)}
|
|
|
|
{policyProfiles
|
|
|
|
.filter((type) => type === profile)
|
|
|
|
.map((type) => (
|
|
|
|
<>
|
|
|
|
<HelpItem
|
|
|
|
helpText={
|
|
|
|
clientProfiles.find(
|
|
|
|
(profile) => type === profile.name
|
|
|
|
)?.description
|
|
|
|
}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId={profile}
|
2021-10-29 14:19:57 +00:00
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
isInline
|
|
|
|
icon={
|
|
|
|
<TrashIcon
|
|
|
|
className="kc-conditionType-trash-icon"
|
|
|
|
data-testid="deleteClientProfileDropdown"
|
|
|
|
onClick={() => {
|
|
|
|
toggleDeleteProfileDialog();
|
|
|
|
setProfileToDelete({
|
|
|
|
idx: idx,
|
|
|
|
name: type!,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
></Button>
|
|
|
|
</>
|
|
|
|
))}
|
|
|
|
</DataListCell>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</DataListItemRow>
|
|
|
|
</DataListItem>
|
|
|
|
))}
|
|
|
|
</DataList>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<Divider />
|
|
|
|
<Text
|
|
|
|
className="kc-emptyClientProfiles"
|
|
|
|
component={TextVariants.h6}
|
|
|
|
>
|
|
|
|
{t("realm-settings:emptyProfiles")}
|
|
|
|
</Text>
|
|
|
|
</>
|
|
|
|
)}
|
2021-10-12 15:03:56 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</FormAccess>
|
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
2021-10-29 16:11:06 +00:00
|
|
|
}
|