2023-05-03 13:51:02 +00:00
|
|
|
import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
|
|
|
|
import type RoleRepresentation from "@keycloak/keycloak-admin-client/lib/defs/roleRepresentation";
|
|
|
|
import { AlertVariant, Tab, Tabs, TabTitleText } from "@patternfly/react-core";
|
2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2021-11-16 11:10:10 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../admin-client";
|
2022-09-16 08:58:43 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { KeycloakSpinner } from "../components/keycloak-spinner/KeycloakSpinner";
|
2022-09-16 08:58:43 +00:00
|
|
|
import { RoleMapping } from "../components/role-mapping/RoleMapping";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2023-05-03 15:40:27 +00:00
|
|
|
import { useFetch } from "../utils/useFetch";
|
2022-02-09 11:46:58 +00:00
|
|
|
import { DefaultsGroupsTab } from "./DefaultGroupsTab";
|
2021-11-16 11:10:10 +00:00
|
|
|
|
|
|
|
export const UserRegistration = () => {
|
|
|
|
const { t } = useTranslation("realm-settings");
|
|
|
|
const [realm, setRealm] = useState<RealmRepresentation>();
|
|
|
|
const [activeTab, setActiveTab] = useState(10);
|
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
|
2022-09-16 08:58:43 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-11-16 11:10:10 +00:00
|
|
|
const { realm: realmName } = useRealm();
|
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() => adminClient.realms.findOne({ realm: realmName }),
|
|
|
|
setRealm,
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!realm) {
|
|
|
|
return <KeycloakSpinner />;
|
|
|
|
}
|
|
|
|
|
2022-09-16 08:58:43 +00:00
|
|
|
const addComposites = async (composites: RoleRepresentation[]) => {
|
|
|
|
const compositeArray = composites;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.roles.createComposite(
|
|
|
|
{ roleId: realm.defaultRole!.id!, realm: realmName },
|
|
|
|
compositeArray
|
|
|
|
);
|
|
|
|
setKey(key + 1);
|
|
|
|
addAlert(t("roles:addAssociatedRolesSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
|
|
|
addError("roles:addAssociatedRolesError", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-16 11:10:10 +00:00
|
|
|
return (
|
|
|
|
<Tabs
|
|
|
|
activeKey={activeTab}
|
|
|
|
onSelect={(_, key) => setActiveTab(key as number)}
|
|
|
|
>
|
|
|
|
<Tab
|
2022-02-09 11:46:58 +00:00
|
|
|
key={key}
|
2021-11-16 11:10:10 +00:00
|
|
|
id="roles"
|
|
|
|
eventKey={10}
|
|
|
|
title={<TabTitleText>{t("defaultRoles")}</TabTitleText>}
|
|
|
|
>
|
2022-09-16 08:58:43 +00:00
|
|
|
<RoleMapping
|
|
|
|
name={realm.defaultRole!.name!}
|
|
|
|
id={realm.defaultRole!.id!}
|
|
|
|
type="roles"
|
|
|
|
isManager
|
|
|
|
save={(rows) => addComposites(rows.map((r) => r.role))}
|
2021-11-16 11:10:10 +00:00
|
|
|
/>
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
id="groups"
|
|
|
|
eventKey={20}
|
|
|
|
title={<TabTitleText>{t("defaultGroups")}</TabTitleText>}
|
|
|
|
>
|
2022-02-09 11:46:58 +00:00
|
|
|
<DefaultsGroupsTab />
|
2021-11-16 11:10:10 +00:00
|
|
|
</Tab>
|
|
|
|
</Tabs>
|
|
|
|
);
|
|
|
|
};
|