2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2022-03-09 16:44:43 +00:00
|
|
|
import { Link } from "react-router-dom";
|
2021-11-17 08:27:56 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import {
|
|
|
|
Alert,
|
|
|
|
AlertVariant,
|
2021-12-01 07:58:25 +00:00
|
|
|
Button,
|
2021-11-17 08:27:56 +00:00
|
|
|
PageSection,
|
2021-12-01 07:58:25 +00:00
|
|
|
ToolbarItem,
|
2021-11-17 08:27:56 +00:00
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import {
|
|
|
|
ExpandableRowContent,
|
|
|
|
TableComposable,
|
|
|
|
Tbody,
|
|
|
|
Td,
|
|
|
|
Th,
|
|
|
|
Thead,
|
|
|
|
Tr,
|
|
|
|
} from "@patternfly/react-table";
|
|
|
|
|
|
|
|
import type ResourceServerRepresentation from "@keycloak/keycloak-admin-client/lib/defs/resourceServerRepresentation";
|
|
|
|
import type ResourceRepresentation from "@keycloak/keycloak-admin-client/lib/defs/resourceRepresentation";
|
2021-12-01 07:58:25 +00:00
|
|
|
import { KeycloakSpinner } from "../../components/keycloak-spinner/KeycloakSpinner";
|
2021-11-17 08:27:56 +00:00
|
|
|
import { useConfirmDialog } from "../../components/confirm-dialog/ConfirmDialog";
|
|
|
|
import { PaginatingTableToolbar } from "../../components/table-toolbar/PaginatingTableToolbar";
|
|
|
|
import { useAdminClient, useFetch } from "../../context/auth/AdminClient";
|
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
|
|
|
import { DetailCell } from "./DetailCell";
|
2021-12-01 07:58:25 +00:00
|
|
|
import { toCreateResource } from "../routes/NewResource";
|
|
|
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
|
|
|
import { toResourceDetails } from "../routes/Resource";
|
2022-01-04 09:17:43 +00:00
|
|
|
import { MoreLabel } from "./MoreLabel";
|
2022-01-19 10:11:40 +00:00
|
|
|
import { toNewPermission } from "../routes/NewPermission";
|
2022-03-16 13:57:50 +00:00
|
|
|
import { ListEmptyState } from "../../components/list-empty-state/ListEmptyState";
|
|
|
|
import { SearchDropdown, SearchForm } from "./SearchDropdown";
|
2021-11-17 08:27:56 +00:00
|
|
|
|
|
|
|
type ResourcesProps = {
|
|
|
|
clientId: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ExpandableResourceRepresentation = ResourceRepresentation & {
|
|
|
|
isExpanded: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const AuthorizationResources = ({ clientId }: ResourcesProps) => {
|
|
|
|
const { t } = useTranslation("clients");
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-11-17 08:27:56 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-12-01 07:58:25 +00:00
|
|
|
const { realm } = useRealm();
|
|
|
|
|
2021-11-17 08:27:56 +00:00
|
|
|
const [resources, setResources] =
|
|
|
|
useState<ExpandableResourceRepresentation[]>();
|
|
|
|
const [selectedResource, setSelectedResource] =
|
|
|
|
useState<ResourceRepresentation>();
|
|
|
|
const [permissions, setPermission] =
|
|
|
|
useState<ResourceServerRepresentation[]>();
|
|
|
|
|
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
const refresh = () => setKey(key + 1);
|
|
|
|
|
|
|
|
const [max, setMax] = useState(10);
|
|
|
|
const [first, setFirst] = useState(0);
|
2022-03-16 13:57:50 +00:00
|
|
|
const [search, setSearch] = useState<SearchForm>({});
|
2021-11-17 08:27:56 +00:00
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() => {
|
|
|
|
const params = {
|
|
|
|
first,
|
2022-04-21 14:53:30 +00:00
|
|
|
max: max + 1,
|
2021-11-17 08:27:56 +00:00
|
|
|
deep: false,
|
2022-03-16 13:57:50 +00:00
|
|
|
...search,
|
2021-11-17 08:27:56 +00:00
|
|
|
};
|
|
|
|
return adminClient.clients.listResources({
|
|
|
|
...params,
|
|
|
|
id: clientId,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(resources) =>
|
|
|
|
setResources(
|
|
|
|
resources.map((resource) => ({ ...resource, isExpanded: false }))
|
|
|
|
),
|
2022-04-21 14:53:30 +00:00
|
|
|
[key, search, first, max]
|
2021-11-17 08:27:56 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const UriRenderer = ({ row }: { row: ResourceRepresentation }) => (
|
|
|
|
<>
|
2022-01-04 09:17:43 +00:00
|
|
|
{row.uris?.[0]} <MoreLabel array={row.uris} />
|
2021-11-17 08:27:56 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
|
|
|
const fetchPermissions = async (id: string) => {
|
|
|
|
return adminClient.clients.listPermissionsByResource({
|
|
|
|
id: clientId,
|
|
|
|
resourceId: id,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "clients:deleteResource",
|
|
|
|
children: (
|
|
|
|
<>
|
|
|
|
{t("deleteResourceConfirm")}
|
|
|
|
{permissions?.length && (
|
|
|
|
<Alert
|
|
|
|
variant="warning"
|
|
|
|
isInline
|
|
|
|
isPlain
|
|
|
|
title={t("deleteResourceWarning")}
|
|
|
|
className="pf-u-pt-lg"
|
|
|
|
>
|
|
|
|
<p className="pf-u-pt-xs">
|
|
|
|
{permissions.map((permission) => (
|
|
|
|
<strong key={permission.id} className="pf-u-pr-md">
|
|
|
|
{permission.name}
|
|
|
|
</strong>
|
|
|
|
))}
|
|
|
|
</p>
|
|
|
|
</Alert>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
continueButtonLabel: "clients:confirm",
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await adminClient.clients.delResource({
|
|
|
|
id: clientId,
|
|
|
|
resourceId: selectedResource?._id!,
|
|
|
|
});
|
|
|
|
addAlert(t("resourceDeletedSuccess"), AlertVariant.success);
|
|
|
|
refresh();
|
|
|
|
} catch (error) {
|
|
|
|
addError("clients:resourceDeletedError", error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!resources) {
|
2021-12-01 07:58:25 +00:00
|
|
|
return <KeycloakSpinner />;
|
2021-11-17 08:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 13:57:50 +00:00
|
|
|
const noData = resources.length === 0;
|
|
|
|
const searching = Object.keys(search).length !== 0;
|
2021-11-17 08:27:56 +00:00
|
|
|
return (
|
|
|
|
<PageSection variant="light" className="pf-u-p-0">
|
|
|
|
<DeleteConfirm />
|
2022-03-16 13:57:50 +00:00
|
|
|
{(!noData || searching) && (
|
|
|
|
<PaginatingTableToolbar
|
|
|
|
count={resources.length}
|
|
|
|
first={first}
|
|
|
|
max={max}
|
|
|
|
onNextClick={setFirst}
|
|
|
|
onPreviousClick={setFirst}
|
|
|
|
onPerPageSelect={(first, max) => {
|
|
|
|
setFirst(first);
|
|
|
|
setMax(max);
|
|
|
|
}}
|
|
|
|
toolbarItem={
|
|
|
|
<>
|
|
|
|
<ToolbarItem>
|
|
|
|
<SearchDropdown
|
|
|
|
search={search}
|
|
|
|
onSearch={setSearch}
|
|
|
|
isResource
|
2021-12-01 07:58:25 +00:00
|
|
|
/>
|
2022-03-16 13:57:50 +00:00
|
|
|
</ToolbarItem>
|
|
|
|
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button
|
|
|
|
data-testid="createResource"
|
|
|
|
component={(props) => (
|
|
|
|
<Link
|
|
|
|
{...props}
|
|
|
|
to={toCreateResource({ realm, id: clientId })}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{t("createResource")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{!noData && (
|
|
|
|
<TableComposable aria-label={t("resources")} variant="compact">
|
|
|
|
<Thead>
|
|
|
|
<Tr>
|
|
|
|
<Th />
|
|
|
|
<Th>{t("common:name")}</Th>
|
|
|
|
<Th>{t("common:type")}</Th>
|
|
|
|
<Th>{t("owner")}</Th>
|
|
|
|
<Th>{t("uris")}</Th>
|
|
|
|
<Th />
|
|
|
|
<Th />
|
|
|
|
</Tr>
|
|
|
|
</Thead>
|
|
|
|
{resources.map((resource, rowIndex) => (
|
|
|
|
<Tbody key={resource._id} isExpanded={resource.isExpanded}>
|
|
|
|
<Tr>
|
|
|
|
<Td
|
|
|
|
expand={{
|
|
|
|
rowIndex,
|
|
|
|
isExpanded: resource.isExpanded,
|
|
|
|
onToggle: (_, rowIndex) => {
|
|
|
|
const rows = resources.map((resource, index) =>
|
|
|
|
index === rowIndex
|
|
|
|
? {
|
|
|
|
...resource,
|
|
|
|
isExpanded: !resource.isExpanded,
|
|
|
|
}
|
|
|
|
: resource
|
|
|
|
);
|
|
|
|
setResources(rows);
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Td data-testid={`name-column-${resource.name}`}>
|
2022-03-09 16:44:43 +00:00
|
|
|
<Link
|
2022-03-16 13:57:50 +00:00
|
|
|
to={toResourceDetails({
|
2022-03-09 16:44:43 +00:00
|
|
|
realm,
|
|
|
|
id: clientId,
|
2022-03-16 13:57:50 +00:00
|
|
|
resourceId: resource._id!,
|
2022-03-09 16:44:43 +00:00
|
|
|
})}
|
2022-03-16 13:57:50 +00:00
|
|
|
>
|
|
|
|
{resource.name}
|
|
|
|
</Link>
|
|
|
|
</Td>
|
|
|
|
<Td>{resource.type}</Td>
|
|
|
|
<Td>{resource.owner?.name}</Td>
|
|
|
|
<Td>
|
|
|
|
<UriRenderer row={resource} />
|
|
|
|
</Td>
|
|
|
|
<Td width={10}>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
component={(props) => (
|
|
|
|
<Link
|
|
|
|
{...props}
|
|
|
|
to={toNewPermission({
|
|
|
|
realm,
|
|
|
|
id: clientId,
|
|
|
|
permissionType: "resource",
|
|
|
|
selectedId: resource._id,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{t("createPermission")}
|
|
|
|
</Button>
|
|
|
|
</Td>
|
|
|
|
<Td
|
|
|
|
actions={{
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
title: t("common:delete"),
|
|
|
|
onClick: async () => {
|
|
|
|
setSelectedResource(resource);
|
|
|
|
setPermission(
|
|
|
|
await fetchPermissions(resource._id!)
|
|
|
|
);
|
|
|
|
toggleDeleteDialog();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Tr>
|
|
|
|
<Tr
|
|
|
|
key={`child-${resource._id}`}
|
|
|
|
isExpanded={resource.isExpanded}
|
2022-03-09 16:44:43 +00:00
|
|
|
>
|
2022-03-16 13:57:50 +00:00
|
|
|
<Td />
|
|
|
|
<Td colSpan={4}>
|
|
|
|
<ExpandableRowContent>
|
|
|
|
{resource.isExpanded && (
|
|
|
|
<DetailCell
|
|
|
|
clientId={clientId}
|
|
|
|
id={resource._id!}
|
|
|
|
uris={resource.uris}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</ExpandableRowContent>
|
|
|
|
</Td>
|
|
|
|
</Tr>
|
|
|
|
</Tbody>
|
|
|
|
))}
|
|
|
|
</TableComposable>
|
|
|
|
)}
|
|
|
|
</PaginatingTableToolbar>
|
|
|
|
)}
|
|
|
|
{noData && searching && (
|
|
|
|
<ListEmptyState
|
|
|
|
isSearchVariant
|
|
|
|
message={t("common:noSearchResults")}
|
|
|
|
instructions={t("common:noSearchResultsInstructions")}
|
|
|
|
/>
|
|
|
|
)}
|
2021-11-17 08:27:56 +00:00
|
|
|
</PageSection>
|
|
|
|
);
|
|
|
|
};
|