2022-03-16 09:32:23 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
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-03-31 13:28:48 +00:00
|
|
|
import { Link, useHistory, useParams } from "react-router-dom";
|
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={[
|
|
|
|
t("generalSettings"),
|
|
|
|
t("permission"),
|
|
|
|
t("validations"),
|
|
|
|
t("annotations"),
|
|
|
|
]}
|
2022-02-16 11:39:08 +00:00
|
|
|
>
|
2022-03-16 09:32:23 +00:00
|
|
|
<AttributeGeneralSettings />
|
|
|
|
<AttributePermission />
|
|
|
|
<AttributeValidations />
|
|
|
|
<AttributeAnnotations />
|
|
|
|
</ScrollForm>
|
|
|
|
<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-03-16 09:32:23 +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");
|
|
|
|
const history = useHistory();
|
|
|
|
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-03-31 13:28:48 +00:00
|
|
|
() => adminClient.users.getProfile({ realm }),
|
|
|
|
(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(
|
|
|
|
flatten({ permissions, selector, required }, { safe: true })
|
|
|
|
).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] =
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.assign(
|
|
|
|
{
|
|
|
|
...attribute,
|
|
|
|
name: attributeName,
|
|
|
|
displayName: profileConfig.displayName!,
|
|
|
|
validations,
|
|
|
|
selector: profileConfig.selector,
|
|
|
|
permissions: profileConfig.permissions!,
|
|
|
|
annotations,
|
|
|
|
},
|
|
|
|
profileConfig.isRequired
|
|
|
|
? { required: profileConfig.required }
|
|
|
|
: undefined
|
|
|
|
);
|
|
|
|
});
|
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 }
|
|
|
|
: undefined
|
|
|
|
),
|
|
|
|
] as UserProfileAttribute);
|
|
|
|
|
|
|
|
const updatedAttributes = editMode ? patchAttributes() : addAttribute();
|
2022-03-16 09:32:23 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.users.updateProfile({
|
2022-03-31 13:28:48 +00:00
|
|
|
attributes: updatedAttributes as UserProfileAttribute[],
|
|
|
|
realm,
|
2022-03-16 09:32:23 +00:00
|
|
|
});
|
|
|
|
|
2022-03-31 13:28:48 +00:00
|
|
|
history.push(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
|
|
|
);
|
|
|
|
}
|