2023-01-04 11:19:09 +00:00
|
|
|
import ClientScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientScopeRepresentation";
|
|
|
|
import type ProtocolMapperRepresentation from "@keycloak/keycloak-admin-client/lib/defs/protocolMapperRepresentation";
|
|
|
|
import type { RoleMappingPayload } from "@keycloak/keycloak-admin-client/lib/defs/roleRepresentation";
|
|
|
|
import type { ProtocolMapperTypeRepresentation } from "@keycloak/keycloak-admin-client/lib/defs/serverInfoRepesentation";
|
|
|
|
import {
|
2023-02-07 11:31:20 +00:00
|
|
|
Alert,
|
2023-01-04 11:19:09 +00:00
|
|
|
AlertVariant,
|
|
|
|
ButtonVariant,
|
|
|
|
PageSection,
|
|
|
|
Tab,
|
|
|
|
TabTitleText,
|
|
|
|
} from "@patternfly/react-core";
|
2024-04-05 14:37:05 +00:00
|
|
|
import { DropdownItem } from "@patternfly/react-core/deprecated";
|
2023-01-04 11:19:09 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2023-01-18 12:09:49 +00:00
|
|
|
import { useNavigate } from "react-router-dom";
|
2024-04-19 18:10:34 +00:00
|
|
|
import { useHelp } from "@keycloak/keycloak-ui-shared";
|
2023-01-04 11:19:09 +00:00
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../admin-client";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2023-01-04 11:19:09 +00:00
|
|
|
import {
|
|
|
|
AllClientScopes,
|
|
|
|
ClientScope,
|
|
|
|
ClientScopeDefaultOptionalType,
|
2023-05-03 13:51:02 +00:00
|
|
|
changeScope,
|
2023-01-04 11:19:09 +00:00
|
|
|
} from "../components/client-scope/ClientScopeTypes";
|
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
|
|
|
import { KeycloakSpinner } from "../components/keycloak-spinner/KeycloakSpinner";
|
|
|
|
import { RoleMapping, Row } from "../components/role-mapping/RoleMapping";
|
|
|
|
import {
|
|
|
|
RoutableTabs,
|
|
|
|
useRoutableTab,
|
|
|
|
} from "../components/routable-tabs/RoutableTabs";
|
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
|
|
|
import { convertFormValuesToObject } from "../util";
|
2023-05-03 15:40:27 +00:00
|
|
|
import { useFetch } from "../utils/useFetch";
|
2023-01-04 11:19:09 +00:00
|
|
|
import { useParams } from "../utils/useParams";
|
|
|
|
import { MapperList } from "./details/MapperList";
|
|
|
|
import { ScopeForm } from "./details/ScopeForm";
|
|
|
|
import {
|
|
|
|
ClientScopeParams,
|
|
|
|
ClientScopeTab,
|
|
|
|
toClientScope,
|
|
|
|
} from "./routes/ClientScope";
|
|
|
|
import { toMapper } from "./routes/Mapper";
|
2023-05-30 12:21:37 +00:00
|
|
|
import { toClientScopes } from "./routes/ClientScopes";
|
2023-01-04 11:19:09 +00:00
|
|
|
|
|
|
|
export default function EditClientScope() {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2023-01-04 11:19:09 +00:00
|
|
|
const navigate = useNavigate();
|
|
|
|
const { realm } = useRealm();
|
|
|
|
const { id } = useParams<ClientScopeParams>();
|
|
|
|
const { addAlert, addError } = useAlerts();
|
2023-02-07 11:31:20 +00:00
|
|
|
const { enabled } = useHelp();
|
2023-01-04 11:19:09 +00:00
|
|
|
const [clientScope, setClientScope] =
|
|
|
|
useState<ClientScopeDefaultOptionalType>();
|
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
const refresh = () => setKey(key + 1);
|
|
|
|
|
|
|
|
useFetch(
|
|
|
|
async () => {
|
|
|
|
const clientScope = await adminClient.clientScopes.findOne({ id });
|
|
|
|
|
|
|
|
if (!clientScope) {
|
2023-09-14 09:01:15 +00:00
|
|
|
throw new Error(t("notFound"));
|
2023-01-04 11:19:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...clientScope,
|
|
|
|
type: await determineScopeType(clientScope),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
(clientScope) => {
|
|
|
|
setClientScope(clientScope);
|
|
|
|
},
|
2023-07-11 14:03:21 +00:00
|
|
|
[key, id],
|
2023-01-04 11:19:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
async function determineScopeType(clientScope: ClientScopeRepresentation) {
|
|
|
|
const defaultScopes =
|
|
|
|
await adminClient.clientScopes.listDefaultClientScopes();
|
|
|
|
const hasDefaultScope = defaultScopes.find(
|
2023-07-11 14:03:21 +00:00
|
|
|
(defaultScope) => defaultScope.name === clientScope.name,
|
2023-01-04 11:19:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (hasDefaultScope) {
|
|
|
|
return ClientScope.default;
|
|
|
|
}
|
|
|
|
|
|
|
|
const optionalScopes =
|
|
|
|
await adminClient.clientScopes.listDefaultOptionalClientScopes();
|
|
|
|
const hasOptionalScope = optionalScopes.find(
|
2023-07-11 14:03:21 +00:00
|
|
|
(optionalScope) => optionalScope.name === clientScope.name,
|
2023-01-04 11:19:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return hasOptionalScope ? ClientScope.optional : AllClientScopes.none;
|
|
|
|
}
|
|
|
|
|
|
|
|
const useTab = (tab: ClientScopeTab) =>
|
|
|
|
useRoutableTab(
|
|
|
|
toClientScope({
|
|
|
|
realm,
|
|
|
|
id,
|
|
|
|
tab,
|
2023-07-11 14:03:21 +00:00
|
|
|
}),
|
2023-01-04 11:19:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const settingsTab = useTab("settings");
|
|
|
|
const mappersTab = useTab("mappers");
|
|
|
|
const scopeTab = useTab("scope");
|
|
|
|
|
|
|
|
const onSubmit = async (formData: ClientScopeDefaultOptionalType) => {
|
|
|
|
const clientScope = convertFormValuesToObject({
|
|
|
|
...formData,
|
|
|
|
name: formData.name?.trim().replace(/ /g, "_"),
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientScopes.update({ id }, clientScope);
|
2023-05-03 13:51:02 +00:00
|
|
|
await changeScope({ ...clientScope, id }, clientScope.type);
|
2023-01-04 11:19:09 +00:00
|
|
|
|
2023-09-08 13:17:17 +00:00
|
|
|
addAlert(t("updateSuccessClientScope"), AlertVariant.success);
|
2023-01-04 11:19:09 +00:00
|
|
|
} catch (error) {
|
2023-09-08 13:17:17 +00:00
|
|
|
addError("updateErrorClientScope", error);
|
2023-01-04 11:19:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: t("deleteClientScope", {
|
|
|
|
count: 1,
|
|
|
|
name: clientScope?.name,
|
|
|
|
}),
|
2023-09-08 13:17:17 +00:00
|
|
|
messageKey: "deleteConfirmClientScopes",
|
2023-09-14 09:01:15 +00:00
|
|
|
continueButtonLabel: "delete",
|
2023-01-04 11:19:09 +00:00
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await adminClient.clientScopes.del({ id });
|
2023-09-08 13:17:17 +00:00
|
|
|
addAlert(t("deletedSuccessClientScope"), AlertVariant.success);
|
2023-05-30 12:21:37 +00:00
|
|
|
navigate(toClientScopes({ realm }));
|
2023-01-04 11:19:09 +00:00
|
|
|
} catch (error) {
|
2023-09-08 13:17:17 +00:00
|
|
|
addError("deleteErrorClientScope", error);
|
2023-01-04 11:19:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const assignRoles = async (rows: Row[]) => {
|
|
|
|
try {
|
|
|
|
const realmRoles = rows
|
|
|
|
.filter((row) => row.client === undefined)
|
|
|
|
.map((row) => row.role as RoleMappingPayload)
|
|
|
|
.flat();
|
|
|
|
await adminClient.clientScopes.addRealmScopeMappings(
|
|
|
|
{
|
|
|
|
id,
|
|
|
|
},
|
2023-07-11 14:03:21 +00:00
|
|
|
realmRoles,
|
2023-01-04 11:19:09 +00:00
|
|
|
);
|
|
|
|
await Promise.all(
|
|
|
|
rows
|
|
|
|
.filter((row) => row.client !== undefined)
|
|
|
|
.map((row) =>
|
|
|
|
adminClient.clientScopes.addClientScopeMappings(
|
|
|
|
{
|
|
|
|
id,
|
|
|
|
client: row.client!.id!,
|
|
|
|
},
|
2023-07-11 14:03:21 +00:00
|
|
|
[row.role as RoleMappingPayload],
|
|
|
|
),
|
|
|
|
),
|
2023-01-04 11:19:09 +00:00
|
|
|
);
|
|
|
|
addAlert(t("roleMappingUpdatedSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
2023-09-13 14:05:17 +00:00
|
|
|
addError("roleMappingUpdatedError", error);
|
2023-01-04 11:19:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const addMappers = async (
|
2023-07-11 14:03:21 +00:00
|
|
|
mappers: ProtocolMapperTypeRepresentation | ProtocolMapperRepresentation[],
|
2023-01-04 11:19:09 +00:00
|
|
|
): Promise<void> => {
|
|
|
|
if (!Array.isArray(mappers)) {
|
|
|
|
const mapper = mappers as ProtocolMapperTypeRepresentation;
|
|
|
|
navigate(
|
|
|
|
toMapper({
|
|
|
|
realm,
|
|
|
|
id: clientScope!.id!,
|
|
|
|
mapperId: mapper.id!,
|
2023-07-11 14:03:21 +00:00
|
|
|
}),
|
2023-01-04 11:19:09 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
await adminClient.clientScopes.addMultipleProtocolMappers(
|
|
|
|
{ id: clientScope!.id! },
|
2023-07-11 14:03:21 +00:00
|
|
|
mappers as ProtocolMapperRepresentation[],
|
2023-01-04 11:19:09 +00:00
|
|
|
);
|
|
|
|
refresh();
|
2023-09-14 09:01:15 +00:00
|
|
|
addAlert(t("mappingCreatedSuccess"), AlertVariant.success);
|
2023-01-04 11:19:09 +00:00
|
|
|
} catch (error) {
|
2023-09-14 09:01:15 +00:00
|
|
|
addError("mappingCreatedError", error);
|
2023-01-04 11:19:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onDelete = async (mapper: ProtocolMapperRepresentation) => {
|
|
|
|
try {
|
|
|
|
await adminClient.clientScopes.delProtocolMapper({
|
|
|
|
id: clientScope!.id!,
|
|
|
|
mapperId: mapper.id!,
|
|
|
|
});
|
2023-09-14 09:01:15 +00:00
|
|
|
addAlert(t("mappingDeletedSuccess"), AlertVariant.success);
|
2023-01-04 11:19:09 +00:00
|
|
|
refresh();
|
|
|
|
} catch (error) {
|
2023-09-14 09:01:15 +00:00
|
|
|
addError("mappingDeletedError", error);
|
2023-01-04 11:19:09 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!clientScope) {
|
|
|
|
return <KeycloakSpinner />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DeleteConfirm />
|
|
|
|
<ViewHeader
|
|
|
|
titleKey={clientScope.name!}
|
|
|
|
dropdownItems={[
|
|
|
|
<DropdownItem key="delete" onClick={toggleDeleteDialog}>
|
2023-09-14 09:01:15 +00:00
|
|
|
{t("delete")}
|
2023-01-04 11:19:09 +00:00
|
|
|
</DropdownItem>,
|
|
|
|
]}
|
|
|
|
badges={[{ text: clientScope.protocol }]}
|
|
|
|
divider={false}
|
|
|
|
/>
|
|
|
|
|
2024-04-05 14:37:05 +00:00
|
|
|
<PageSection variant="light" className="pf-v5-u-p-0">
|
2023-01-04 11:19:09 +00:00
|
|
|
<RoutableTabs isBox>
|
|
|
|
<Tab
|
|
|
|
id="settings"
|
|
|
|
data-testid="settings"
|
2023-09-14 09:01:15 +00:00
|
|
|
title={<TabTitleText>{t("settings")}</TabTitleText>}
|
2023-01-04 11:19:09 +00:00
|
|
|
{...settingsTab}
|
|
|
|
>
|
|
|
|
<PageSection variant="light">
|
|
|
|
<ScopeForm save={onSubmit} clientScope={clientScope} />
|
|
|
|
</PageSection>
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
id="mappers"
|
|
|
|
data-testid="mappers"
|
2023-09-14 09:01:15 +00:00
|
|
|
title={<TabTitleText>{t("mappers")}</TabTitleText>}
|
2023-01-04 11:19:09 +00:00
|
|
|
{...mappersTab}
|
|
|
|
>
|
|
|
|
<MapperList
|
|
|
|
model={clientScope}
|
|
|
|
onAdd={addMappers}
|
|
|
|
onDelete={onDelete}
|
|
|
|
detailLink={(id) =>
|
|
|
|
toMapper({ realm, id: clientScope.id!, mapperId: id! })
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
id="scope"
|
|
|
|
data-testid="scopeTab"
|
|
|
|
title={<TabTitleText>{t("scope")}</TabTitleText>}
|
|
|
|
{...scopeTab}
|
|
|
|
>
|
2023-02-07 11:31:20 +00:00
|
|
|
{enabled && (
|
|
|
|
<PageSection>
|
|
|
|
<Alert
|
|
|
|
variant="info"
|
|
|
|
isInline
|
2023-09-13 14:05:17 +00:00
|
|
|
title={t("clientScopesRolesScope")}
|
2023-03-30 14:40:52 +00:00
|
|
|
component="h2"
|
2023-02-07 11:31:20 +00:00
|
|
|
/>
|
|
|
|
</PageSection>
|
|
|
|
)}
|
2023-01-04 11:19:09 +00:00
|
|
|
<RoleMapping
|
|
|
|
id={clientScope.id!}
|
|
|
|
name={clientScope.name!}
|
|
|
|
type="clientScopes"
|
|
|
|
save={assignRoles}
|
|
|
|
/>
|
|
|
|
</Tab>
|
|
|
|
</RoutableTabs>
|
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|