UNPKG

@keycloakify/keycloak-admin-ui

Version:
52 lines 3.06 kB
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import { useAlerts, useFetch } from "../../ui-shared"; import { AlertVariant, PageSection } from "@patternfly/react-core"; import { InfoCircleIcon } from "@patternfly/react-icons"; import { useState } from "react"; import { Trans, useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; import { useAdminClient } from "../../admin-client"; import { KeycloakSpinner } from "../../ui-shared"; import { RoleMapping } from "../../components/role-mapping/RoleMapping"; import { useAccess } from "../../context/access/Access"; import { useRealm } from "../../context/realm-context/RealmContext"; import { toUser } from "../../user/routes/User"; import "./service-account.css"; export const ServiceAccount = ({ client }) => { var _a; const { adminClient } = useAdminClient(); const { t } = useTranslation(); const { addAlert, addError } = useAlerts(); const { realm } = useRealm(); const [serviceAccount, setServiceAccount] = useState(); const { hasAccess } = useAccess(); const hasManageClients = hasAccess("manage-clients"); useFetch(() => adminClient.clients.getServiceAccountUser({ id: client.id, }), (serviceAccount) => setServiceAccount(serviceAccount), []); const assignRoles = async (rows) => { try { const realmRoles = rows .filter((row) => row.client === undefined) .map((row) => row.role) .flat(); await adminClient.users.addRealmRoleMappings({ id: serviceAccount === null || serviceAccount === void 0 ? void 0 : serviceAccount.id, roles: realmRoles, }); await Promise.all(rows .filter((row) => row.client !== undefined) .map((row) => adminClient.users.addClientRoleMappings({ id: serviceAccount === null || serviceAccount === void 0 ? void 0 : serviceAccount.id, clientUniqueId: row.client.id, roles: [row.role], }))); addAlert(t("roleMappingUpdatedSuccess"), AlertVariant.success); } catch (error) { addError("roleMappingUpdatedError", error); } }; return serviceAccount ? (_jsxs(_Fragment, { children: [_jsxs(PageSection, { className: "pf-v5-u-pb-0", children: [_jsx(InfoCircleIcon, { className: "pf-v5-c-alert__icon keycloak--service-account--info-text" }), _jsx("span", { className: "pf-v5-u-pl-sm", children: _jsxs(Trans, { i18nKey: "manageServiceAccountUser", children: ["", _jsx(Link, { to: toUser({ realm, id: serviceAccount.id, tab: "settings" }), children: { link: serviceAccount.username } })] }) })] }), _jsx(RoleMapping, { name: client.clientId, id: serviceAccount.id, type: "users", isManager: hasManageClients || ((_a = client.access) === null || _a === void 0 ? void 0 : _a.configure), save: assignRoles })] })) : (_jsx(KeycloakSpinner, {})); }; //# sourceMappingURL=ServiceAccount.js.map