2021-06-15 06:14:38 +00:00
|
|
|
import React, { useState } from "react";
|
2020-12-07 18:37:36 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2021-04-20 12:10:00 +00:00
|
|
|
import { AlertVariant } from "@patternfly/react-core";
|
2020-12-07 18:37:36 +00:00
|
|
|
|
2021-08-26 08:39:35 +00:00
|
|
|
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";
|
2021-06-15 06:14:38 +00:00
|
|
|
import { useAdminClient, useFetch } from "../../context/auth/AdminClient";
|
2021-04-01 14:14:19 +00:00
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
2021-04-20 12:10:00 +00:00
|
|
|
import {
|
2021-06-15 06:14:38 +00:00
|
|
|
mapRoles,
|
2021-04-20 12:10:00 +00:00
|
|
|
RoleMapping,
|
|
|
|
Row,
|
|
|
|
} from "../../components/role-mapping/RoleMapping";
|
2020-12-07 18:37:36 +00:00
|
|
|
|
|
|
|
type ServiceAccountProps = {
|
2021-06-15 06:14:38 +00:00
|
|
|
client: ClientRepresentation;
|
2020-12-07 18:37:36 +00:00
|
|
|
};
|
|
|
|
|
2021-06-15 06:14:38 +00:00
|
|
|
export const ServiceAccount = ({ client }: ServiceAccountProps) => {
|
2020-12-07 18:37:36 +00:00
|
|
|
const { t } = useTranslation("clients");
|
|
|
|
const adminClient = useAdminClient();
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-04-01 14:14:19 +00:00
|
|
|
|
2020-12-07 18:37:36 +00:00
|
|
|
const [hide, setHide] = useState(false);
|
2021-06-15 06:14:38 +00:00
|
|
|
const [serviceAccount, setServiceAccount] = useState<UserRepresentation>();
|
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() =>
|
|
|
|
adminClient.clients.getServiceAccountUser({
|
|
|
|
id: client.id!,
|
|
|
|
}),
|
|
|
|
(serviceAccount) => setServiceAccount(serviceAccount),
|
|
|
|
[]
|
|
|
|
);
|
2020-12-07 18:37:36 +00:00
|
|
|
|
|
|
|
const loader = async () => {
|
|
|
|
const serviceAccount = await adminClient.clients.getServiceAccountUser({
|
2021-06-15 06:14:38 +00:00
|
|
|
id: client.id!,
|
2020-12-07 18:37:36 +00:00
|
|
|
});
|
2021-06-15 06:14:38 +00:00
|
|
|
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 }));
|
2020-12-07 18:37:36 +00:00
|
|
|
|
|
|
|
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({
|
2021-06-15 06:14:38 +00:00
|
|
|
id,
|
2020-12-07 18:37:36 +00:00
|
|
|
clientUniqueId: client.id!,
|
2021-06-15 11:12:32 +00:00
|
|
|
})
|
|
|
|
).map((role) => ({ role, client }));
|
|
|
|
const clientEffectiveRoles = (
|
|
|
|
await adminClient.users.listCompositeClientRoleMappings({
|
2021-06-15 06:14:38 +00:00
|
|
|
id,
|
|
|
|
clientUniqueId: client.id!,
|
2021-06-15 11:12:32 +00:00
|
|
|
})
|
|
|
|
).map((role) => ({ role, client }));
|
2021-06-15 06:14:38 +00:00
|
|
|
return mapRoles(clientAssignedRoles, clientEffectiveRoles, hide);
|
2020-12-07 18:37:36 +00:00
|
|
|
})
|
|
|
|
)
|
2021-06-15 06:14:38 +00:00
|
|
|
).flat();
|
2020-12-07 18:37:36 +00:00
|
|
|
|
2021-06-15 06:14:38 +00:00
|
|
|
return [...mapRoles(assignedRoles, effectiveRoles, hide), ...clientRoles];
|
2020-12-07 18:37:36 +00:00
|
|
|
};
|
|
|
|
|
2021-04-01 14:14:19 +00:00
|
|
|
const assignRoles = async (rows: Row[]) => {
|
|
|
|
try {
|
|
|
|
const realmRoles = rows
|
|
|
|
.filter((row) => row.client === undefined)
|
|
|
|
.map((row) => row.role as RoleMappingPayload)
|
|
|
|
.flat();
|
|
|
|
adminClient.users.addRealmRoleMappings({
|
2021-06-15 06:14:38 +00:00
|
|
|
id: serviceAccount?.id!,
|
2021-04-01 14:14:19 +00:00
|
|
|
roles: realmRoles,
|
|
|
|
});
|
|
|
|
await Promise.all(
|
|
|
|
rows
|
|
|
|
.filter((row) => row.client !== undefined)
|
|
|
|
.map((row) =>
|
|
|
|
adminClient.users.addClientRoleMappings({
|
2021-06-15 06:14:38 +00:00
|
|
|
id: serviceAccount?.id!,
|
2021-04-01 14:14:19 +00:00
|
|
|
clientUniqueId: row.client!.id!,
|
|
|
|
roles: [row.role as RoleMappingPayload],
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
addAlert(t("roleMappingUpdatedSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("clients:roleMappingUpdatedError", error);
|
2021-04-01 14:14:19 +00:00
|
|
|
}
|
|
|
|
};
|
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;
|
2020-12-07 18:37:36 +00:00
|
|
|
};
|