2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2022-03-16 09:32:23 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
Form,
|
|
|
|
PageSection,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { FormProvider, useForm, useFormContext } from "react-hook-form";
|
2022-02-16 11:39:08 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-08-22 15:51:19 +00:00
|
|
|
import { useParams } from "react-router-dom";
|
|
|
|
import { Link, useNavigate } from "react-router-dom-v5-compat";
|
2022-03-16 09:32:23 +00:00
|
|
|
import { ScrollForm } from "../components/scroll-form/ScrollForm";
|
|
|
|
import type UserProfileConfig from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig";
|
|
|
|
import { AttributeGeneralSettings } from "./user-profile/attribute/AttributeGeneralSettings";
|
|
|
|
import { AttributePermission } from "./user-profile/attribute/AttributePermission";
|
|
|
|
import { AttributeValidations } from "./user-profile/attribute/AttributeValidations";
|
|
|
|
import { toUserProfile } from "./routes/UserProfile";
|
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
|
|
|
import { AttributeAnnotations } from "./user-profile/attribute/AttributeAnnotations";
|
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { UserProfileProvider } from "./user-profile/UserProfileContext";
|
|
|
|
import type { UserProfileAttribute } from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig";
|
2022-03-31 13:28:48 +00:00
|
|
|
import type { AttributeParams } from "./routes/Attribute";
|
2022-04-20 17:11:46 +00:00
|
|
|
import type { KeyValueType } from "../components/key-value-form/key-value-convert";
|
2022-03-31 13:28:48 +00:00
|
|
|
import { convertToFormValues } from "../util";
|
|
|
|
import { flatten } from "flat";
|
|
|
|
|
|
|
|
import "./realm-settings-section.css";
|
2022-02-16 11:39:08 +00:00
|
|
|
|
2022-03-31 13:28:48 +00:00
|
|
|
type UserProfileAttributeType = UserProfileAttribute & Attribute & Permission;
|
2022-03-16 09:32:23 +00:00
|
|
|
|
2022-03-31 13:28:48 +00:00
|
|
|
type Attribute = {
|
2022-03-16 09:32:23 +00:00
|
|
|
roles: string[];
|
2022-03-31 13:28:48 +00:00
|
|
|
scopes: string[];
|
|
|
|
isRequired: boolean;
|
2022-03-16 09:32:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type Permission = {
|
|
|
|
view: PermissionView[];
|
|
|
|
edit: PermissionEdit[];
|
|
|
|
};
|
|
|
|
|
|
|
|
type PermissionView = [
|
|
|
|
{
|
|
|
|
adminView: boolean;
|
|
|
|
userView: boolean;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
type PermissionEdit = [
|
|
|
|
{
|
|
|
|
adminEdit: boolean;
|
|
|
|
userEdit: boolean;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const CreateAttributeFormContent = ({
|
|
|
|
save,
|
|
|
|
}: {
|
|
|
|
save: (profileConfig: UserProfileConfig) => void;
|
|
|
|
}) => {
|
2022-02-16 11:39:08 +00:00
|
|
|
const { t } = useTranslation("realm-settings");
|
2022-03-16 09:32:23 +00:00
|
|
|
const form = useFormContext();
|
2022-03-31 13:28:48 +00:00
|
|
|
const { realm, attributeName } = useParams<AttributeParams>();
|
|
|
|
const editMode = attributeName ? true : false;
|
2022-02-16 11:39:08 +00:00
|
|
|
|
|
|
|
return (
|
2022-03-16 09:32:23 +00:00
|
|
|
<UserProfileProvider>
|
|
|
|
<ScrollForm
|
|
|
|
sections={[
|
2022-05-30 11:07:33 +00:00
|
|
|
{ title: t("generalSettings"), panel: <AttributeGeneralSettings /> },
|
|
|
|
{ title: t("permission"), panel: <AttributePermission /> },
|
|
|
|
{ title: t("validations"), panel: <AttributeValidations /> },
|
|
|
|
{ title: t("annotations"), panel: <AttributeAnnotations /> },
|
2022-03-16 09:32:23 +00:00
|
|
|
]}
|
2022-05-30 11:07:33 +00:00
|
|
|
/>
|
2022-03-16 09:32:23 +00:00
|
|
|
<Form onSubmit={form.handleSubmit(save)}>
|
|
|
|
<ActionGroup className="keycloak__form_actions">
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
|
|
|
data-testid="attribute-create"
|
|
|
|
>
|
2022-03-31 13:28:48 +00:00
|
|
|
{editMode ? t("common:save") : t("common:create")}
|
2022-03-16 09:32:23 +00:00
|
|
|
</Button>
|
|
|
|
<Link
|
|
|
|
to={toUserProfile({ realm, tab: "attributes" })}
|
|
|
|
data-testid="attribute-cancel"
|
|
|
|
className="kc-attributeCancel"
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Link>
|
|
|
|
</ActionGroup>
|
|
|
|
</Form>
|
|
|
|
</UserProfileProvider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function NewAttributeSettings() {
|
2022-03-31 13:28:48 +00:00
|
|
|
const { realm, attributeName } = useParams<AttributeParams>();
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2022-03-31 13:28:48 +00:00
|
|
|
const form = useForm<UserProfileConfig>({ shouldUnregister: false });
|
2022-03-16 09:32:23 +00:00
|
|
|
const { t } = useTranslation("realm-settings");
|
2022-08-16 13:09:14 +00:00
|
|
|
const navigate = useNavigate();
|
2022-03-16 09:32:23 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
const [config, setConfig] = useState<UserProfileConfig | null>(null);
|
2022-03-31 13:28:48 +00:00
|
|
|
const editMode = attributeName ? true : false;
|
|
|
|
|
|
|
|
const convert = (obj: Record<string, unknown>[] | undefined) =>
|
|
|
|
Object.entries(obj || []).map(([key, value]) => ({
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
}));
|
2022-03-16 09:32:23 +00:00
|
|
|
|
|
|
|
useFetch(
|
2022-06-13 11:10:11 +00:00
|
|
|
() => adminClient.users.getProfile(),
|
2022-03-31 13:28:48 +00:00
|
|
|
(config) => {
|
2022-03-16 09:32:23 +00:00
|
|
|
setConfig(config);
|
2022-03-31 13:28:48 +00:00
|
|
|
const {
|
|
|
|
annotations,
|
|
|
|
validations,
|
|
|
|
permissions,
|
|
|
|
selector,
|
|
|
|
required,
|
|
|
|
...values
|
2022-03-31 14:23:28 +00:00
|
|
|
} =
|
|
|
|
config.attributes!.find(
|
|
|
|
(attribute) => attribute.name === attributeName
|
|
|
|
) || {};
|
2022-03-31 13:28:48 +00:00
|
|
|
convertToFormValues(values, form.setValue);
|
|
|
|
Object.entries(
|
2022-08-29 13:46:28 +00:00
|
|
|
flatten<any, any>({ permissions, selector, required }, { safe: true })
|
2022-03-31 13:28:48 +00:00
|
|
|
).map(([key, value]) => form.setValue(key, value));
|
|
|
|
form.setValue("annotations", convert(annotations));
|
|
|
|
form.setValue("validations", convert(validations));
|
|
|
|
form.setValue("isRequired", required !== undefined);
|
2022-03-16 09:32:23 +00:00
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const save = async (profileConfig: UserProfileAttributeType) => {
|
2022-03-17 11:05:10 +00:00
|
|
|
const validations = profileConfig.validations?.reduce(
|
|
|
|
(prevValidations: any, currentValidations: any) => {
|
2022-03-31 13:28:48 +00:00
|
|
|
prevValidations[currentValidations.key] =
|
2022-05-17 10:19:48 +00:00
|
|
|
currentValidations.value?.length === 0
|
|
|
|
? {}
|
|
|
|
: currentValidations.value;
|
2022-03-17 11:05:10 +00:00
|
|
|
return prevValidations;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
2022-03-16 09:32:23 +00:00
|
|
|
|
|
|
|
const annotations = (profileConfig.annotations! as KeyValueType[]).reduce(
|
|
|
|
(obj, item) => Object.assign(obj, { [item.key]: item.value }),
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
2022-03-31 13:28:48 +00:00
|
|
|
const patchAttributes = () =>
|
|
|
|
config?.attributes!.map((attribute) => {
|
|
|
|
if (attribute.name !== attributeName) {
|
|
|
|
return attribute;
|
|
|
|
}
|
|
|
|
|
2022-06-13 09:42:17 +00:00
|
|
|
delete attribute.required;
|
2022-03-31 13:28:48 +00:00
|
|
|
return Object.assign(
|
|
|
|
{
|
|
|
|
...attribute,
|
|
|
|
name: attributeName,
|
|
|
|
displayName: profileConfig.displayName!,
|
|
|
|
validations,
|
|
|
|
selector: profileConfig.selector,
|
|
|
|
permissions: profileConfig.permissions!,
|
|
|
|
annotations,
|
|
|
|
},
|
|
|
|
profileConfig.isRequired
|
|
|
|
? { required: profileConfig.required }
|
2022-06-13 11:10:11 +00:00
|
|
|
: undefined,
|
|
|
|
profileConfig.group ? { group: profileConfig.group } : undefined
|
2022-03-31 13:28:48 +00:00
|
|
|
);
|
|
|
|
});
|
2022-03-16 09:32:23 +00:00
|
|
|
|
2022-03-31 13:28:48 +00:00
|
|
|
const addAttribute = () =>
|
|
|
|
config?.attributes!.concat([
|
|
|
|
Object.assign(
|
|
|
|
{
|
|
|
|
name: profileConfig.name,
|
|
|
|
displayName: profileConfig.displayName!,
|
|
|
|
required: profileConfig.isRequired ? profileConfig.required : {},
|
|
|
|
validations,
|
|
|
|
selector: profileConfig.selector,
|
|
|
|
permissions: profileConfig.permissions!,
|
|
|
|
annotations,
|
|
|
|
},
|
|
|
|
profileConfig.isRequired
|
|
|
|
? { required: profileConfig.required }
|
2022-06-13 11:10:11 +00:00
|
|
|
: undefined,
|
|
|
|
profileConfig.group ? { group: profileConfig.group } : undefined
|
2022-03-31 13:28:48 +00:00
|
|
|
),
|
|
|
|
] as UserProfileAttribute);
|
|
|
|
|
|
|
|
const updatedAttributes = editMode ? patchAttributes() : addAttribute();
|
2022-03-16 09:32:23 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.users.updateProfile({
|
2022-06-13 11:10:11 +00:00
|
|
|
...config,
|
2022-03-31 13:28:48 +00:00
|
|
|
attributes: updatedAttributes as UserProfileAttribute[],
|
|
|
|
realm,
|
2022-03-16 09:32:23 +00:00
|
|
|
});
|
|
|
|
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toUserProfile({ realm, tab: "attributes" }));
|
2022-03-16 09:32:23 +00:00
|
|
|
|
|
|
|
addAlert(
|
|
|
|
t("realm-settings:createAttributeSuccess"),
|
|
|
|
AlertVariant.success
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
addError("realm-settings:createAttributeError", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormProvider {...form}>
|
|
|
|
<ViewHeader
|
2022-03-31 13:28:48 +00:00
|
|
|
titleKey={editMode ? attributeName : t("createAttribute")}
|
|
|
|
subKey={editMode ? "" : t("createAttributeSubTitle")}
|
2022-03-16 09:32:23 +00:00
|
|
|
/>
|
|
|
|
<PageSection variant="light">
|
|
|
|
<CreateAttributeFormContent save={() => form.handleSubmit(save)()} />
|
|
|
|
</PageSection>
|
|
|
|
</FormProvider>
|
2022-02-16 11:39:08 +00:00
|
|
|
);
|
|
|
|
}
|