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-16 09:32:23 +00:00
|
|
|
import { Link, useHistory } from "react-router-dom";
|
|
|
|
import { ScrollForm } from "../components/scroll-form/ScrollForm";
|
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
|
|
|
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";
|
2022-02-16 11:39:08 +00:00
|
|
|
import "./realm-settings-section.css";
|
2022-03-16 09:32:23 +00:00
|
|
|
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";
|
|
|
|
import type { KeyValueType } from "../components/attribute-form/attribute-convert";
|
|
|
|
import type ClientScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientScopeRepresentation";
|
2022-02-16 11:39:08 +00:00
|
|
|
|
2022-03-16 09:32:23 +00:00
|
|
|
type UserProfileAttributeType = UserProfileAttribute &
|
|
|
|
AttributeRequired &
|
|
|
|
Permission;
|
|
|
|
|
|
|
|
type AttributeRequired = {
|
|
|
|
roles: string[];
|
|
|
|
scopeRequired: string[];
|
|
|
|
enabledWhen: boolean;
|
|
|
|
requiredWhen: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
const { realm } = useRealm();
|
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"
|
|
|
|
>
|
|
|
|
{t("common:create")}
|
|
|
|
</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() {
|
|
|
|
const { realm: realmName } = useRealm();
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
const form = useForm<UserProfileConfig>();
|
|
|
|
const { t } = useTranslation("realm-settings");
|
|
|
|
const history = useHistory();
|
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
const [config, setConfig] = useState<UserProfileConfig | null>(null);
|
|
|
|
const [clientScopes, setClientScopes] =
|
|
|
|
useState<ClientScopeRepresentation[]>();
|
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() =>
|
|
|
|
Promise.all([
|
|
|
|
adminClient.users.getProfile({ realm: realmName }),
|
|
|
|
adminClient.clientScopes.find(),
|
|
|
|
]),
|
|
|
|
([config, clientScopes]) => {
|
|
|
|
setConfig(config);
|
|
|
|
setClientScopes(clientScopes);
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const save = async (profileConfig: UserProfileAttributeType) => {
|
|
|
|
const scopeNames = clientScopes?.map((clientScope) => clientScope.name);
|
|
|
|
|
|
|
|
const selector = {
|
|
|
|
scopes: profileConfig.enabledWhen
|
|
|
|
? scopeNames
|
|
|
|
: profileConfig.selector?.scopes,
|
|
|
|
};
|
|
|
|
|
|
|
|
const required = {
|
|
|
|
roles: profileConfig.roles,
|
|
|
|
scopes: profileConfig.requiredWhen
|
|
|
|
? scopeNames
|
|
|
|
: profileConfig.scopeRequired,
|
|
|
|
};
|
|
|
|
|
2022-03-17 11:05:10 +00:00
|
|
|
const validations = profileConfig.validations?.reduce(
|
|
|
|
(prevValidations: any, currentValidations: any) => {
|
|
|
|
prevValidations[currentValidations.name] =
|
|
|
|
currentValidations.config.length === 0
|
|
|
|
? {}
|
|
|
|
: currentValidations.config;
|
|
|
|
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 }),
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
|
|
|
const newAttribute = [
|
|
|
|
{
|
|
|
|
name: profileConfig.name,
|
|
|
|
displayName: profileConfig.displayName,
|
|
|
|
required,
|
|
|
|
validations,
|
|
|
|
selector,
|
|
|
|
permissions: profileConfig.permissions,
|
|
|
|
annotations,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const newAttributesList = config?.attributes!.concat(
|
|
|
|
newAttribute as UserProfileAttribute
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.users.updateProfile({
|
|
|
|
attributes: newAttributesList,
|
|
|
|
realm: realmName,
|
|
|
|
});
|
|
|
|
|
|
|
|
history.push(toUserProfile({ realm: realmName, tab: "attributes" }));
|
|
|
|
|
|
|
|
addAlert(
|
|
|
|
t("realm-settings:createAttributeSuccess"),
|
|
|
|
AlertVariant.success
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
addError("realm-settings:createAttributeError", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormProvider {...form}>
|
|
|
|
<ViewHeader
|
|
|
|
titleKey={t("createAttribute")}
|
|
|
|
subKey={t("createAttributeSubTitle")}
|
|
|
|
/>
|
|
|
|
<PageSection variant="light">
|
|
|
|
<CreateAttributeFormContent save={() => form.handleSubmit(save)()} />
|
|
|
|
</PageSection>
|
|
|
|
</FormProvider>
|
2022-02-16 11:39:08 +00:00
|
|
|
);
|
|
|
|
}
|