2021-12-07 12:56:25 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { AlertVariant } from "@patternfly/react-core";
|
|
|
|
|
|
|
|
import type { RoleMappingPayload } from "@keycloak/keycloak-admin-client/lib/defs/roleRepresentation";
|
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
|
|
|
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");
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
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],
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
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
|
|
|
};
|