2021-04-06 07:29:11 +00:00
|
|
|
import React, { useState } from "react";
|
2020-09-25 17:11:25 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2021-01-05 19:49:33 +00:00
|
|
|
import { Link, useHistory, useRouteMatch } from "react-router-dom";
|
2021-04-06 07:29:11 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
Dropdown,
|
|
|
|
DropdownItem,
|
|
|
|
KebabToggle,
|
|
|
|
PageSection,
|
|
|
|
ToolbarItem,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { cellWidth } from "@patternfly/react-table";
|
2021-05-04 17:58:18 +00:00
|
|
|
import type ClientScopeRepresentation from "keycloak-admin/lib/defs/clientScopeRepresentation";
|
2020-09-25 17:11:25 +00:00
|
|
|
|
2020-11-12 12:55:52 +00:00
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
2020-12-07 19:23:18 +00:00
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
2021-01-07 18:34:59 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2020-12-11 10:28:38 +00:00
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
2021-04-06 07:29:11 +00:00
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
2021-04-21 13:18:45 +00:00
|
|
|
import { upperCaseFormatter, emptyFormatter } from "../util";
|
2021-04-06 07:29:11 +00:00
|
|
|
import {
|
|
|
|
CellDropdown,
|
|
|
|
ClientScope,
|
|
|
|
AllClientScopes,
|
|
|
|
AllClientScopeType,
|
|
|
|
} from "../components/client-scope/ClientScopeTypes";
|
2021-05-04 17:58:18 +00:00
|
|
|
import type KeycloakAdminClient from "keycloak-admin";
|
2021-05-03 16:44:47 +00:00
|
|
|
import { ChangeTypeDialog } from "./ChangeTypeDialog";
|
|
|
|
|
|
|
|
import "./client-scope.css";
|
2021-04-06 07:29:11 +00:00
|
|
|
|
|
|
|
type ClientScopeDefaultOptionalType = ClientScopeRepresentation & {
|
|
|
|
type: AllClientScopeType;
|
|
|
|
};
|
|
|
|
|
|
|
|
const castAdminClient = (adminClient: KeycloakAdminClient) =>
|
|
|
|
(adminClient.clientScopes as unknown) as {
|
|
|
|
[index: string]: Function;
|
|
|
|
};
|
|
|
|
|
|
|
|
const changeScope = async (
|
|
|
|
adminClient: KeycloakAdminClient,
|
|
|
|
clientScope: ClientScopeDefaultOptionalType,
|
|
|
|
changeTo: AllClientScopeType
|
|
|
|
) => {
|
|
|
|
await removeScope(adminClient, clientScope);
|
|
|
|
await addScope(adminClient, clientScope, changeTo);
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeScope = async (
|
|
|
|
adminClient: KeycloakAdminClient,
|
|
|
|
clientScope: ClientScopeDefaultOptionalType
|
|
|
|
) => {
|
|
|
|
if (clientScope.type !== AllClientScopes.none)
|
|
|
|
await castAdminClient(adminClient)[
|
|
|
|
`delDefault${
|
|
|
|
clientScope.type === ClientScope.optional ? "Optional" : ""
|
|
|
|
}ClientScope`
|
|
|
|
]({
|
|
|
|
id: clientScope.id!,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const addScope = async (
|
|
|
|
adminClient: KeycloakAdminClient,
|
|
|
|
clientScope: ClientScopeDefaultOptionalType,
|
|
|
|
type: AllClientScopeType
|
|
|
|
) => {
|
|
|
|
if (type !== AllClientScopes.none)
|
|
|
|
await castAdminClient(adminClient)[
|
|
|
|
`addDefault${type === ClientScope.optional ? "Optional" : ""}ClientScope`
|
|
|
|
]({
|
|
|
|
id: clientScope.id!,
|
|
|
|
});
|
|
|
|
};
|
2020-09-09 09:07:17 +00:00
|
|
|
|
2020-09-10 18:04:03 +00:00
|
|
|
export const ClientScopesSection = () => {
|
2020-09-25 17:11:25 +00:00
|
|
|
const { t } = useTranslation("client-scopes");
|
|
|
|
const history = useHistory();
|
2021-01-05 19:49:33 +00:00
|
|
|
const { url } = useRouteMatch();
|
2020-09-25 17:11:25 +00:00
|
|
|
|
2020-11-12 12:55:52 +00:00
|
|
|
const adminClient = useAdminClient();
|
2021-01-07 18:34:59 +00:00
|
|
|
const { addAlert } = useAlerts();
|
2020-09-25 17:11:25 +00:00
|
|
|
|
2021-04-06 07:29:11 +00:00
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
const refresh = () => setKey(new Date().getTime());
|
|
|
|
|
|
|
|
const [kebabOpen, setKebabOpen] = useState(false);
|
|
|
|
const [changeTypeOpen, setChangeTypeOpen] = useState(false);
|
|
|
|
const [selectedScopes, setSelectedScopes] = useState<
|
|
|
|
ClientScopeDefaultOptionalType[]
|
|
|
|
>([]);
|
|
|
|
|
|
|
|
const loader = async () => {
|
|
|
|
const defaultScopes = await adminClient.clientScopes.listDefaultClientScopes();
|
|
|
|
const optionalScopes = await adminClient.clientScopes.listDefaultOptionalClientScopes();
|
|
|
|
|
|
|
|
const clientScopes = (await adminClient.clientScopes.find()).map(
|
|
|
|
(scope) => {
|
|
|
|
return {
|
|
|
|
...scope,
|
|
|
|
type: defaultScopes.find(
|
|
|
|
(defaultScope) => defaultScope.name === scope.name
|
|
|
|
)
|
|
|
|
? ClientScope.default
|
|
|
|
: optionalScopes.find(
|
|
|
|
(optionalScope) => optionalScope.name === scope.name
|
|
|
|
)
|
|
|
|
? ClientScope.optional
|
|
|
|
: AllClientScopes.none,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return clientScopes;
|
|
|
|
};
|
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: t("deleteClientScope", {
|
|
|
|
count: selectedScopes.length,
|
|
|
|
name: selectedScopes[0]?.name,
|
|
|
|
}),
|
|
|
|
messageKey: "client-scopes:deleteConfirm",
|
|
|
|
continueButtonLabel: "common:delete",
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
for (const scope of selectedScopes) {
|
|
|
|
await adminClient.clientScopes.del({ id: scope.id! });
|
|
|
|
}
|
|
|
|
addAlert(t("deletedSuccess"), AlertVariant.success);
|
|
|
|
refresh();
|
|
|
|
} catch (error) {
|
|
|
|
addAlert(
|
|
|
|
t("deleteError", {
|
|
|
|
error: error.response?.data?.errorMessage || error,
|
|
|
|
}),
|
|
|
|
AlertVariant.danger
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const TypeSelector = (scope: ClientScopeDefaultOptionalType) => (
|
|
|
|
<>
|
|
|
|
<CellDropdown
|
|
|
|
clientScope={scope}
|
|
|
|
type={scope.type}
|
|
|
|
all
|
|
|
|
onSelect={async (value) => {
|
|
|
|
try {
|
|
|
|
await changeScope(adminClient, scope, value);
|
|
|
|
addAlert(t("clientScopeSuccess"), AlertVariant.success);
|
|
|
|
refresh();
|
|
|
|
} catch (error) {
|
|
|
|
addAlert(t("clientScopeError", { error }), AlertVariant.danger);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
2020-10-07 15:47:03 +00:00
|
|
|
|
2020-12-07 19:23:18 +00:00
|
|
|
const ClientScopeDetailLink = (clientScope: ClientScopeRepresentation) => (
|
|
|
|
<>
|
2021-03-01 09:20:36 +00:00
|
|
|
<Link key={clientScope.id} to={`${url}/${clientScope.id}/settings`}>
|
2020-12-07 19:23:18 +00:00
|
|
|
{clientScope.name}
|
|
|
|
</Link>
|
|
|
|
</>
|
|
|
|
);
|
2020-09-18 08:04:55 +00:00
|
|
|
return (
|
2020-10-02 13:56:46 +00:00
|
|
|
<>
|
2021-04-06 07:29:11 +00:00
|
|
|
<DeleteConfirm />
|
|
|
|
{changeTypeOpen && (
|
|
|
|
<ChangeTypeDialog
|
|
|
|
selectedClientScopes={selectedScopes.length}
|
|
|
|
onConfirm={(type) => {
|
|
|
|
selectedScopes.map(async (scope) => {
|
|
|
|
try {
|
|
|
|
await changeScope(adminClient, scope, type);
|
|
|
|
addAlert(t("clientScopeSuccess"), AlertVariant.success);
|
|
|
|
refresh();
|
|
|
|
} catch (error) {
|
|
|
|
addAlert(t("clientScopeError", { error }), AlertVariant.danger);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
setChangeTypeOpen(false);
|
|
|
|
}}
|
|
|
|
onClose={() => setChangeTypeOpen(false)}
|
|
|
|
/>
|
|
|
|
)}
|
2020-10-02 13:56:46 +00:00
|
|
|
<ViewHeader
|
|
|
|
titleKey="clientScopes"
|
|
|
|
subKey="client-scopes:clientScopeExplain"
|
|
|
|
/>
|
2021-03-31 13:16:58 +00:00
|
|
|
<PageSection variant="light" className="pf-u-p-0">
|
2020-12-11 10:18:29 +00:00
|
|
|
<KeycloakDataTable
|
2021-04-06 07:29:11 +00:00
|
|
|
key={key}
|
2020-12-07 19:23:18 +00:00
|
|
|
loader={loader}
|
|
|
|
ariaLabelKey="client-scopes:clientScopeList"
|
|
|
|
searchPlaceholderKey="client-scopes:searchFor"
|
2021-04-06 07:29:11 +00:00
|
|
|
onSelect={(clientScopes) => setSelectedScopes([...clientScopes])}
|
|
|
|
canSelectAll
|
2020-12-07 19:23:18 +00:00
|
|
|
toolbarItem={
|
2021-04-06 07:29:11 +00:00
|
|
|
<>
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button onClick={() => history.push(`${url}/new`)}>
|
|
|
|
{t("createClientScope")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
<ToolbarItem>
|
|
|
|
<Dropdown
|
|
|
|
toggle={
|
|
|
|
<KebabToggle onToggle={() => setKebabOpen(!kebabOpen)} />
|
|
|
|
}
|
|
|
|
isOpen={kebabOpen}
|
|
|
|
isPlain
|
|
|
|
dropdownItems={[
|
|
|
|
<DropdownItem
|
|
|
|
key="changeType"
|
|
|
|
component="button"
|
|
|
|
isDisabled={selectedScopes.length === 0}
|
|
|
|
onClick={() => {
|
|
|
|
setChangeTypeOpen(true);
|
|
|
|
setKebabOpen(false);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("changeType")}
|
|
|
|
</DropdownItem>,
|
|
|
|
|
|
|
|
<DropdownItem
|
|
|
|
key="action"
|
|
|
|
component="button"
|
|
|
|
isDisabled={selectedScopes.length === 0}
|
|
|
|
onClick={() => {
|
|
|
|
toggleDeleteDialog();
|
|
|
|
setKebabOpen(false);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("common:delete")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</ToolbarItem>
|
|
|
|
</>
|
2020-12-07 19:23:18 +00:00
|
|
|
}
|
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
title: t("common:export"),
|
|
|
|
onRowClick: () => {},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: t("common:delete"),
|
2021-04-06 07:29:11 +00:00
|
|
|
onRowClick: (clientScope) => {
|
|
|
|
setSelectedScopes([clientScope]);
|
|
|
|
toggleDeleteDialog();
|
2021-01-07 18:34:59 +00:00
|
|
|
},
|
2020-12-07 19:23:18 +00:00
|
|
|
},
|
|
|
|
]}
|
|
|
|
columns={[
|
|
|
|
{
|
2021-01-20 06:53:36 +00:00
|
|
|
name: "name",
|
2020-12-07 19:23:18 +00:00
|
|
|
cellRenderer: ClientScopeDetailLink,
|
|
|
|
},
|
2021-04-06 07:29:11 +00:00
|
|
|
{ name: "description", cellFormatters: [emptyFormatter()] },
|
|
|
|
{ name: "type", cellRenderer: TypeSelector },
|
2020-12-07 19:23:18 +00:00
|
|
|
{
|
2021-01-20 06:53:36 +00:00
|
|
|
name: "protocol",
|
2021-02-28 20:02:31 +00:00
|
|
|
displayKey: "client-scopes:protocol",
|
2021-04-21 13:18:45 +00:00
|
|
|
cellFormatters: [upperCaseFormatter()],
|
2021-04-06 11:11:39 +00:00
|
|
|
transforms: [cellWidth(15)],
|
2020-12-07 19:23:18 +00:00
|
|
|
},
|
2021-04-06 07:29:11 +00:00
|
|
|
{
|
|
|
|
name: "attributes['gui.order']",
|
|
|
|
displayKey: "client-scopes:displayOrder",
|
|
|
|
cellFormatters: [emptyFormatter()],
|
2021-04-06 11:11:39 +00:00
|
|
|
transforms: [cellWidth(15)],
|
2021-04-06 07:29:11 +00:00
|
|
|
},
|
2020-12-07 19:23:18 +00:00
|
|
|
]}
|
|
|
|
/>
|
2020-10-02 13:56:46 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
2020-09-18 08:04:55 +00:00
|
|
|
);
|
2020-09-09 09:07:17 +00:00
|
|
|
};
|