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-06-15 06:14:38 +00:00
|
|
|
import type UserRepresentation from "keycloak-admin/lib/defs/userRepresentation";
|
2021-05-04 17:58:18 +00:00
|
|
|
import type { RoleMappingPayload } from "keycloak-admin/lib/defs/roleRepresentation";
|
2021-06-15 06:14:38 +00:00
|
|
|
import type ClientRepresentation from "keycloak-admin/lib/defs/clientRepresentation";
|
|
|
|
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-04-01 14:14:19 +00:00
|
|
|
const { addAlert } = useAlerts();
|
|
|
|
|
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!;
|
|
|
|
|
|
|
|
const assignedRoles = await adminClient.users.listRealmRoleMappings({ id });
|
2020-12-07 18:37:36 +00:00
|
|
|
const effectiveRoles = await adminClient.users.listCompositeRealmRoleMappings(
|
2021-06-15 06:14:38 +00:00
|
|
|
{ id }
|
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 06:14:38 +00:00
|
|
|
const clientAssignedRoles = await adminClient.users.listClientRoleMappings(
|
|
|
|
{
|
|
|
|
id,
|
2020-12-07 18:37:36 +00:00
|
|
|
clientUniqueId: client.id!,
|
2021-06-15 06:14:38 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
const clientEffectiveRoles = await adminClient.users.listCompositeClientRoleMappings(
|
|
|
|
{
|
|
|
|
id,
|
|
|
|
clientUniqueId: client.id!,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
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) {
|
|
|
|
addAlert(
|
|
|
|
t("roleMappingUpdatedError", {
|
|
|
|
error: error.response?.data?.errorMessage || error,
|
|
|
|
}),
|
|
|
|
AlertVariant.danger
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return (
|
2021-06-15 06:14:38 +00:00
|
|
|
<>
|
|
|
|
{serviceAccount && (
|
|
|
|
<RoleMapping
|
|
|
|
name={client.clientId!}
|
|
|
|
id={serviceAccount.id!}
|
|
|
|
type="service-account"
|
|
|
|
loader={loader}
|
|
|
|
save={assignRoles}
|
|
|
|
onHideRolesToggle={() => setHide(!hide)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
2021-03-19 12:43:32 +00:00
|
|
|
);
|
2020-12-07 18:37:36 +00:00
|
|
|
};
|