keycloak-scim/src/clients/service-account/ServiceAccount.tsx

111 lines
3.5 KiB
TypeScript
Raw Normal View History

import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { AlertVariant } from "@patternfly/react-core";
import type UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation";
import type { RoleMappingPayload } from "@keycloak/keycloak-admin-client/lib/defs/roleRepresentation";
import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation";
import { useAdminClient, useFetch } from "../../context/auth/AdminClient";
import { useAlerts } from "../../components/alert/Alerts";
import {
mapRoles,
RoleMapping,
Row,
} from "../../components/role-mapping/RoleMapping";
type ServiceAccountProps = {
client: ClientRepresentation;
};
export const ServiceAccount = ({ client }: ServiceAccountProps) => {
const { t } = useTranslation("clients");
const adminClient = useAdminClient();
const { addAlert, addError } = useAlerts();
const [hide, setHide] = useState(false);
const [serviceAccount, setServiceAccount] = useState<UserRepresentation>();
useFetch(
() =>
adminClient.clients.getServiceAccountUser({
id: client.id!,
}),
(serviceAccount) => setServiceAccount(serviceAccount),
[]
);
const loader = async () => {
const serviceAccount = await adminClient.clients.getServiceAccountUser({
id: client.id!,
});
const id = serviceAccount.id!;
2021-06-15 11:12:32 +00:00
const assignedRoles = (
await adminClient.users.listRealmRoleMappings({ id })
).map((role) => ({ role }));
const effectiveRoles = (
await adminClient.users.listCompositeRealmRoleMappings({ id })
).map((role) => ({ role }));
const clients = await adminClient.clients.find();
const clientRoles = (
await Promise.all(
clients.map(async (client) => {
2021-06-15 11:12:32 +00:00
const clientAssignedRoles = (
await adminClient.users.listClientRoleMappings({
id,
clientUniqueId: client.id!,
2021-06-15 11:12:32 +00:00
})
).map((role) => ({ role, client }));
const clientEffectiveRoles = (
await adminClient.users.listCompositeClientRoleMappings({
id,
clientUniqueId: client.id!,
2021-06-15 11:12:32 +00:00
})
).map((role) => ({ role, client }));
return mapRoles(clientAssignedRoles, clientEffectiveRoles, hide);
})
)
).flat();
return [...mapRoles(assignedRoles, effectiveRoles, hide), ...clientRoles];
};
const assignRoles = async (rows: Row[]) => {
try {
const realmRoles = rows
.filter((row) => row.client === undefined)
.map((row) => row.role as RoleMappingPayload)
.flat();
adminClient.users.addRealmRoleMappings({
id: serviceAccount?.id!,
roles: realmRoles,
});
await Promise.all(
rows
.filter((row) => row.client !== undefined)
.map((row) =>
adminClient.users.addClientRoleMappings({
id: serviceAccount?.id!,
clientUniqueId: row.client!.id!,
roles: [row.role as RoleMappingPayload],
})
)
);
addAlert(t("roleMappingUpdatedSuccess"), AlertVariant.success);
} catch (error) {
addError("clients:roleMappingUpdatedError", error);
}
};
2021-08-26 12:15:28 +00:00
return serviceAccount ? (
<RoleMapping
name={client.clientId!}
id={serviceAccount.id!}
type="service-account"
loader={loader}
save={assignRoles}
onHideRolesToggle={() => setHide(!hide)}
/>
) : null;
};