2022-12-07 14:23:12 +00:00
|
|
|
import type UserProfileConfig from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig";
|
|
|
|
import type { UserProfileAttribute } from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig";
|
2022-03-16 09:32:23 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
Form,
|
|
|
|
PageSection,
|
|
|
|
} from "@patternfly/react-core";
|
2022-12-07 14:23:12 +00:00
|
|
|
import { flatten } from "flat";
|
|
|
|
import { useState } from "react";
|
2022-03-16 09:32:23 +00:00
|
|
|
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 { Link, useNavigate } from "react-router-dom-v5-compat";
|
2022-12-07 14:23:12 +00:00
|
|
|
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2022-03-16 09:32:23 +00:00
|
|
|
import { ScrollForm } from "../components/scroll-form/ScrollForm";
|
2022-12-07 14:23:12 +00:00
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
|
|
|
import { convertToFormValues } from "../util";
|
|
|
|
import { useParams } from "../utils/useParams";
|
|
|
|
import type { AttributeParams } from "./routes/Attribute";
|
|
|
|
import { toUserProfile } from "./routes/UserProfile";
|
|
|
|
import { AttributeAnnotations } from "./user-profile/attribute/AttributeAnnotations";
|
2022-03-16 09:32:23 +00:00
|
|
|
import { AttributeGeneralSettings } from "./user-profile/attribute/AttributeGeneralSettings";
|
|
|
|
import { AttributePermission } from "./user-profile/attribute/AttributePermission";
|
|
|
|
import { AttributeValidations } from "./user-profile/attribute/AttributeValidations";
|
|
|
|
import { UserProfileProvider } from "./user-profile/UserProfileContext";
|
2022-03-31 13:28:48 +00:00
|
|
|
|
|
|
|
import "./realm-settings-section.css";
|
2022-02-16 11:39:08 +00:00
|
|
|
|
2022-11-16 14:19:09 +00:00
|
|
|
type IndexedAnnotations = {
|
|
|
|
key: string;
|
|
|
|
value: unknown;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type IndexedValidations = {
|
|
|
|
key: string;
|
|
|
|
value?: Record<string, unknown>[];
|
|
|
|
};
|
|
|
|
|
|
|
|
type UserProfileAttributeType = Omit<
|
|
|
|
UserProfileAttribute,
|
|
|
|
"validations" | "annotations"
|
|
|
|
> &
|
|
|
|
Attribute &
|
|
|
|
Permission & {
|
|
|
|
validations: IndexedValidations[];
|
|
|
|
annotations: IndexedAnnotations[];
|
|
|
|
};
|
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;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2022-11-16 14:19:09 +00:00
|
|
|
export const USERNAME_EMAIL = ["username", "email"];
|
|
|
|
|
2022-03-16 09:32:23 +00:00
|
|
|
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;
|
|
|
|
|
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));
|
2022-11-16 14:19:09 +00:00
|
|
|
form.setValue(
|
|
|
|
"annotations",
|
|
|
|
Object.entries(annotations || {}).map(([key, value]) => ({
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
form.setValue(
|
|
|
|
"validations",
|
|
|
|
Object.entries(validations || {}).map(([key, value]) => ({
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
}))
|
|
|
|
);
|
2022-03-31 13:28:48 +00:00
|
|
|
form.setValue("isRequired", required !== undefined);
|
2022-03-16 09:32:23 +00:00
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const save = async (profileConfig: UserProfileAttributeType) => {
|
2022-11-16 14:19:09 +00:00
|
|
|
const validations = profileConfig.validations.reduce(
|
|
|
|
(prevValidations, currentValidations) => {
|
|
|
|
prevValidations[currentValidations.key] =
|
|
|
|
currentValidations.value?.length === 0
|
|
|
|
? {}
|
|
|
|
: currentValidations.value;
|
|
|
|
return prevValidations;
|
|
|
|
},
|
|
|
|
{} as Record<string, unknown>
|
|
|
|
);
|
|
|
|
|
|
|
|
const annotations = profileConfig.annotations.reduce(
|
2022-03-16 09:32:23 +00:00
|
|
|
(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!,
|
|
|
|
selector: profileConfig.selector,
|
|
|
|
permissions: profileConfig.permissions!,
|
|
|
|
annotations,
|
2022-11-16 14:19:09 +00:00
|
|
|
validations,
|
2022-03-31 13:28:48 +00:00
|
|
|
},
|
|
|
|
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 : {},
|
|
|
|
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
|
|
|
);
|
|
|
|
}
|