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-08-10 13:07:59 +00:00
|
|
|
import { Link } 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";
|
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-07-22 11:17:00 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
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,
|
2021-05-21 16:34:05 +00:00
|
|
|
ClientScopeDefaultOptionalType,
|
|
|
|
changeScope,
|
|
|
|
removeScope,
|
2021-04-06 07:29:11 +00:00
|
|
|
} from "../components/client-scope/ClientScopeTypes";
|
2021-05-03 16:44:47 +00:00
|
|
|
import { ChangeTypeDialog } from "./ChangeTypeDialog";
|
2021-07-22 11:17:00 +00:00
|
|
|
import { toNewClientScope } from "./routes/NewClientScope";
|
2021-05-03 16:44:47 +00:00
|
|
|
|
|
|
|
import "./client-scope.css";
|
2021-08-10 13:07:59 +00:00
|
|
|
import { toClientScope } from "./routes/ClientScope";
|
2021-08-20 10:37:40 +00:00
|
|
|
import { useWhoAmI } from "../context/whoami/WhoAmI";
|
2021-04-06 07:29:11 +00:00
|
|
|
|
2020-09-10 18:04:03 +00:00
|
|
|
export const ClientScopesSection = () => {
|
2021-07-22 11:17:00 +00:00
|
|
|
const { realm } = useRealm();
|
2021-08-20 10:37:40 +00:00
|
|
|
const { whoAmI } = useWhoAmI();
|
2020-09-25 17:11:25 +00:00
|
|
|
const { t } = useTranslation("client-scopes");
|
|
|
|
|
2020-11-12 12:55:52 +00:00
|
|
|
const adminClient = useAdminClient();
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = 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 () => {
|
2021-07-05 11:24:10 +00:00
|
|
|
const defaultScopes =
|
|
|
|
await adminClient.clientScopes.listDefaultClientScopes();
|
|
|
|
const optionalScopes =
|
|
|
|
await adminClient.clientScopes.listDefaultOptionalClientScopes();
|
2021-08-20 10:37:40 +00:00
|
|
|
const clientScopes = await adminClient.clientScopes.find();
|
2021-04-06 07:29:11 +00:00
|
|
|
|
2021-08-20 10:37:40 +00:00
|
|
|
return clientScopes
|
|
|
|
.map((scope) => {
|
2021-04-06 07:29:11 +00:00
|
|
|
return {
|
|
|
|
...scope,
|
|
|
|
type: defaultScopes.find(
|
|
|
|
(defaultScope) => defaultScope.name === scope.name
|
|
|
|
)
|
|
|
|
? ClientScope.default
|
|
|
|
: optionalScopes.find(
|
|
|
|
(optionalScope) => optionalScope.name === scope.name
|
|
|
|
)
|
|
|
|
? ClientScope.optional
|
|
|
|
: AllClientScopes.none,
|
|
|
|
};
|
2021-08-20 10:37:40 +00:00
|
|
|
})
|
|
|
|
.sort((a, b) => a.name!.localeCompare(b.name!, whoAmI.getLocale()));
|
2021-04-06 07:29:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
2021-05-27 11:41:29 +00:00
|
|
|
try {
|
|
|
|
await removeScope(adminClient, scope);
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(
|
|
|
|
"could not remove scope",
|
|
|
|
error.response?.data?.errorMessage || error
|
|
|
|
);
|
|
|
|
}
|
2021-04-06 07:29:11 +00:00
|
|
|
await adminClient.clientScopes.del({ id: scope.id! });
|
|
|
|
}
|
|
|
|
addAlert(t("deletedSuccess"), AlertVariant.success);
|
|
|
|
refresh();
|
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("client-scopes:deleteError", error);
|
2021-04-06 07:29:11 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const TypeSelector = (scope: ClientScopeDefaultOptionalType) => (
|
2021-08-26 12:15:28 +00:00
|
|
|
<CellDropdown
|
|
|
|
clientScope={scope}
|
|
|
|
type={scope.type}
|
|
|
|
all
|
|
|
|
onSelect={async (value) => {
|
|
|
|
try {
|
|
|
|
await changeScope(adminClient, scope, value);
|
|
|
|
addAlert(t("clientScopeSuccess"), AlertVariant.success);
|
|
|
|
refresh();
|
|
|
|
} catch (error) {
|
|
|
|
addError("client-scopes:clientScopeError", error);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
2021-04-06 07:29:11 +00:00
|
|
|
);
|
2020-10-07 15:47:03 +00:00
|
|
|
|
2021-08-10 13:07:59 +00:00
|
|
|
const ClientScopeDetailLink = ({
|
|
|
|
id,
|
|
|
|
type,
|
|
|
|
name,
|
|
|
|
}: ClientScopeDefaultOptionalType) => (
|
2021-08-26 12:15:28 +00:00
|
|
|
<Link
|
|
|
|
key={id}
|
|
|
|
to={toClientScope({ realm, id: id!, type, tab: "settings" })}
|
|
|
|
>
|
|
|
|
{name}
|
|
|
|
</Link>
|
2020-12-07 19:23:18 +00:00
|
|
|
);
|
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) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("client-scopes:clientScopeError", error);
|
2021-04-06 07:29:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
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>
|
2021-07-22 11:17:00 +00:00
|
|
|
{/* @ts-ignore */}
|
|
|
|
<Button component={Link} to={toNewClientScope({ realm })}>
|
2021-04-06 07:29:11 +00:00
|
|
|
{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"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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-08-20 10:37:40 +00:00
|
|
|
{
|
|
|
|
name: "type",
|
|
|
|
displayKey: "client-scopes:assignedType",
|
|
|
|
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
|
|
|
},
|
2021-08-20 10:37:40 +00:00
|
|
|
{ name: "description", cellFormatters: [emptyFormatter()] },
|
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
|
|
|
};
|