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) => {
|
|
|
|
const { t } = useTranslation("clients");
|
|
|
|
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],
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
addAlert(t("roleMappingUpdatedSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
|
|
|
addError("clients:roleMappingUpdatedError", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|