2023-05-03 13:51:02 +00:00
|
|
|
import type { RoleMappingPayload } from "@keycloak/keycloak-admin-client/lib/defs/roleRepresentation";
|
2021-12-07 12:56:25 +00:00
|
|
|
import { AlertVariant } from "@patternfly/react-core";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2021-12-07 12:56:25 +00:00
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../admin-client";
|
2021-12-07 12:56:25 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2022-08-15 08:32:37 +00:00
|
|
|
import { RoleMapping, Row } from "../components/role-mapping/RoleMapping";
|
2021-12-07 12:56:25 +00:00
|
|
|
|
|
|
|
type GroupRoleMappingProps = {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const GroupRoleMapping = ({ id, name }: GroupRoleMappingProps) => {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2021-12-07 12:56:25 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
|
|
|
|
const assignRoles = async (rows: Row[]) => {
|
|
|
|
try {
|
|
|
|
const realmRoles = rows
|
|
|
|
.filter((row) => row.client === undefined)
|
|
|
|
.map((row) => row.role as RoleMappingPayload)
|
|
|
|
.flat();
|
|
|
|
await adminClient.groups.addRealmRoleMappings({
|
|
|
|
id,
|
|
|
|
roles: realmRoles,
|
|
|
|
});
|
|
|
|
await Promise.all(
|
|
|
|
rows
|
|
|
|
.filter((row) => row.client !== undefined)
|
|
|
|
.map((row) =>
|
|
|
|
adminClient.groups.addClientRoleMappings({
|
|
|
|
id,
|
|
|
|
clientUniqueId: row.client!.id!,
|
|
|
|
roles: [row.role as RoleMappingPayload],
|
2023-07-11 14:03:21 +00:00
|
|
|
}),
|
|
|
|
),
|
2021-12-07 12:56:25 +00:00
|
|
|
);
|
|
|
|
addAlert(t("roleMappingUpdatedSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
2023-09-13 14:05:17 +00:00
|
|
|
addError("roleMappingUpdatedError", error);
|
2021-12-07 12:56:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-15 08:32:37 +00:00
|
|
|
return <RoleMapping name={name} id={id} type="groups" save={assignRoles} />;
|
2021-12-07 12:56:25 +00:00
|
|
|
};
|