2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2022-08-16 13:44:53 +00:00
|
|
|
import { useLocation, useNavigate } from "react-router-dom-v5-compat";
|
2020-09-15 19:54:52 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2020-09-28 15:58:03 +00:00
|
|
|
import {
|
|
|
|
DropdownItem,
|
|
|
|
PageSection,
|
|
|
|
PageSectionVariants,
|
2021-03-16 12:37:57 +00:00
|
|
|
Tab,
|
|
|
|
TabTitleText,
|
|
|
|
Tabs,
|
2022-10-10 15:03:27 +00:00
|
|
|
Drawer,
|
|
|
|
DrawerContent,
|
|
|
|
DrawerContentBody,
|
|
|
|
DrawerPanelContent,
|
|
|
|
DrawerHead,
|
2020-09-28 15:58:03 +00:00
|
|
|
} from "@patternfly/react-core";
|
2021-08-26 08:39:35 +00:00
|
|
|
import type GroupRepresentation from "@keycloak/keycloak-admin-client/lib/defs/groupRepresentation";
|
2020-09-09 09:07:17 +00:00
|
|
|
|
2020-12-14 08:57:05 +00:00
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
2021-04-29 06:28:59 +00:00
|
|
|
import { useFetch, useAdminClient } from "../context/auth/AdminClient";
|
2021-01-12 14:00:44 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2020-12-14 08:57:05 +00:00
|
|
|
|
2021-03-16 12:37:57 +00:00
|
|
|
import { useSubGroups } from "./SubGroupsContext";
|
|
|
|
import { GroupTable } from "./GroupTable";
|
|
|
|
import { getId, getLastId } from "./groupIdUtils";
|
|
|
|
import { Members } from "./Members";
|
|
|
|
import { GroupAttributes } from "./GroupAttributes";
|
2021-03-24 14:07:49 +00:00
|
|
|
import { GroupsModal } from "./GroupsModal";
|
2021-11-09 17:53:06 +00:00
|
|
|
import { toGroups } from "./routes/Groups";
|
2021-12-07 12:56:25 +00:00
|
|
|
import { GroupRoleMapping } from "./GroupRoleMapping";
|
2022-02-21 14:58:28 +00:00
|
|
|
import helpUrls from "../help-urls";
|
2022-04-28 13:18:14 +00:00
|
|
|
import { PermissionsTab } from "../components/permission-tab/PermissionTab";
|
2022-05-09 10:44:37 +00:00
|
|
|
import { useAccess } from "../context/access/Access";
|
2022-08-15 12:29:41 +00:00
|
|
|
import { GroupTree } from "./components/GroupTree";
|
2022-09-01 09:21:42 +00:00
|
|
|
import { DeleteGroup } from "./components/DeleteGroup";
|
|
|
|
import useToggle from "../utils/useToggle";
|
2022-10-10 15:03:27 +00:00
|
|
|
import { GroupBreadCrumbs } from "../components/bread-crumb/GroupBreadCrumbs";
|
2021-01-12 14:00:44 +00:00
|
|
|
|
2021-03-16 12:37:57 +00:00
|
|
|
import "./GroupsSection.css";
|
2021-01-12 14:00:44 +00:00
|
|
|
|
2021-10-29 16:11:06 +00:00
|
|
|
export default function GroupsSection() {
|
2020-09-15 19:54:52 +00:00
|
|
|
const { t } = useTranslation("groups");
|
2021-03-16 12:37:57 +00:00
|
|
|
const [activeTab, setActiveTab] = useState(0);
|
|
|
|
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-03-24 14:07:49 +00:00
|
|
|
const { subGroups, setSubGroups, currentGroup } = useSubGroups();
|
2021-01-12 14:00:44 +00:00
|
|
|
const { realm } = useRealm();
|
|
|
|
|
2021-03-24 14:07:49 +00:00
|
|
|
const [rename, setRename] = useState<string>();
|
2022-09-01 09:21:42 +00:00
|
|
|
const [deleteOpen, toggleDeleteOpen] = useToggle();
|
2021-03-24 14:07:49 +00:00
|
|
|
|
2022-08-16 13:09:14 +00:00
|
|
|
const navigate = useNavigate();
|
2021-03-19 18:37:21 +00:00
|
|
|
const location = useLocation();
|
2021-01-12 14:00:44 +00:00
|
|
|
const id = getLastId(location.pathname);
|
|
|
|
|
2022-10-10 15:03:27 +00:00
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
const refresh = () => setKey(key + 1);
|
|
|
|
|
2022-05-09 10:44:37 +00:00
|
|
|
const { hasAccess } = useAccess();
|
|
|
|
const canViewPermissions = hasAccess(
|
|
|
|
"manage-authorization",
|
|
|
|
"manage-users",
|
|
|
|
"manage-clients"
|
|
|
|
);
|
|
|
|
const canManageGroup =
|
|
|
|
hasAccess("manage-users") || currentGroup()?.access?.manage;
|
|
|
|
const canManageRoles = hasAccess("manage-users");
|
|
|
|
|
2021-04-29 06:28:59 +00:00
|
|
|
useFetch(
|
|
|
|
async () => {
|
|
|
|
const ids = getId(location.pathname);
|
|
|
|
const isNavigationStateInValid = ids && ids.length > subGroups.length;
|
2021-05-18 12:25:46 +00:00
|
|
|
|
2021-04-29 06:28:59 +00:00
|
|
|
if (isNavigationStateInValid) {
|
|
|
|
const groups: GroupRepresentation[] = [];
|
|
|
|
for (const i of ids!) {
|
2022-06-01 09:12:16 +00:00
|
|
|
const group =
|
|
|
|
i !== "search"
|
|
|
|
? await adminClient.groups.findOne({ id: i })
|
|
|
|
: { name: t("searchGroups"), id: "search" };
|
2021-09-30 08:58:48 +00:00
|
|
|
if (group) {
|
|
|
|
groups.push(group);
|
|
|
|
} else {
|
|
|
|
throw new Error(t("common:notFound"));
|
|
|
|
}
|
2021-04-29 06:28:59 +00:00
|
|
|
}
|
|
|
|
return groups;
|
|
|
|
}
|
2021-05-18 08:34:16 +00:00
|
|
|
return [];
|
|
|
|
},
|
|
|
|
(groups: GroupRepresentation[]) => {
|
|
|
|
if (groups.length) setSubGroups(groups);
|
2021-04-29 06:28:59 +00:00
|
|
|
},
|
2021-03-16 12:37:57 +00:00
|
|
|
[id]
|
2020-12-14 08:57:05 +00:00
|
|
|
);
|
|
|
|
|
2020-09-15 19:54:52 +00:00
|
|
|
return (
|
2020-10-14 20:47:29 +00:00
|
|
|
<>
|
2022-09-01 09:21:42 +00:00
|
|
|
<DeleteGroup
|
|
|
|
show={deleteOpen}
|
|
|
|
toggleDialog={toggleDeleteOpen}
|
|
|
|
selectedRows={[currentGroup()!]}
|
2022-10-10 15:03:27 +00:00
|
|
|
refresh={() => {
|
|
|
|
navigate(toGroups({ realm }));
|
|
|
|
refresh();
|
|
|
|
}}
|
2022-09-01 09:21:42 +00:00
|
|
|
/>
|
2021-03-24 14:07:49 +00:00
|
|
|
{rename && (
|
|
|
|
<GroupsModal
|
|
|
|
id={id}
|
|
|
|
rename={rename}
|
2022-10-10 15:03:27 +00:00
|
|
|
refresh={(group) => {
|
|
|
|
refresh();
|
|
|
|
setSubGroups([...subGroups.slice(0, subGroups.length - 1), group!]);
|
|
|
|
}}
|
2021-03-24 14:07:49 +00:00
|
|
|
handleModalToggle={() => setRename(undefined)}
|
|
|
|
/>
|
|
|
|
)}
|
2021-03-31 13:16:58 +00:00
|
|
|
<PageSection variant={PageSectionVariants.light} className="pf-u-p-0">
|
2022-10-10 15:03:27 +00:00
|
|
|
<Drawer isInline isExpanded key={key}>
|
|
|
|
<DrawerContent
|
|
|
|
panelContent={
|
|
|
|
<DrawerPanelContent isResizable defaultSize="80%" minSize="500px">
|
|
|
|
<DrawerHead>
|
|
|
|
<GroupBreadCrumbs />
|
|
|
|
<ViewHeader
|
|
|
|
titleKey={!id ? "groups:groups" : currentGroup()?.name!}
|
|
|
|
subKey={!id ? "groups:groupsDescription" : ""}
|
|
|
|
helpUrl={!id ? helpUrls.groupsUrl : ""}
|
|
|
|
divider={!id}
|
|
|
|
dropdownItems={
|
|
|
|
id && canManageGroup
|
|
|
|
? [
|
|
|
|
<DropdownItem
|
|
|
|
data-testid="renameGroupAction"
|
|
|
|
key="renameGroup"
|
|
|
|
onClick={() => setRename(currentGroup()?.name)}
|
|
|
|
>
|
|
|
|
{t("renameGroup")}
|
|
|
|
</DropdownItem>,
|
|
|
|
<DropdownItem
|
|
|
|
data-testid="deleteGroup"
|
|
|
|
key="deleteGroup"
|
|
|
|
onClick={toggleDeleteOpen}
|
|
|
|
>
|
|
|
|
{t("deleteGroup")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
{subGroups.length > 0 && (
|
|
|
|
<Tabs
|
|
|
|
inset={{
|
|
|
|
default: "insetNone",
|
|
|
|
md: "insetSm",
|
|
|
|
xl: "insetLg",
|
|
|
|
"2xl": "inset2xl",
|
|
|
|
}}
|
|
|
|
activeKey={activeTab}
|
|
|
|
onSelect={(_, key) => setActiveTab(key as number)}
|
|
|
|
isBox
|
|
|
|
>
|
|
|
|
<Tab
|
|
|
|
data-testid="groups"
|
|
|
|
eventKey={0}
|
|
|
|
title={<TabTitleText>{t("childGroups")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<GroupTable refresh={refresh} />
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
data-testid="members"
|
|
|
|
eventKey={1}
|
|
|
|
title={<TabTitleText>{t("members")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<Members />
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
data-testid="attributes"
|
|
|
|
eventKey={2}
|
|
|
|
title={
|
|
|
|
<TabTitleText>{t("common:attributes")}</TabTitleText>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<GroupAttributes />
|
|
|
|
</Tab>
|
|
|
|
{canManageRoles && (
|
|
|
|
<Tab
|
|
|
|
eventKey={3}
|
|
|
|
data-testid="role-mapping-tab"
|
|
|
|
title={
|
|
|
|
<TabTitleText>{t("roleMapping")}</TabTitleText>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<GroupRoleMapping
|
|
|
|
id={id!}
|
|
|
|
name={currentGroup()?.name!}
|
|
|
|
/>
|
|
|
|
</Tab>
|
|
|
|
)}
|
|
|
|
{canViewPermissions && (
|
|
|
|
<Tab
|
|
|
|
eventKey={4}
|
|
|
|
data-testid="permissionsTab"
|
|
|
|
title={
|
|
|
|
<TabTitleText>
|
|
|
|
{t("common:permissions")}
|
|
|
|
</TabTitleText>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<PermissionsTab id={id} type="groups" />
|
|
|
|
</Tab>
|
|
|
|
)}
|
|
|
|
</Tabs>
|
|
|
|
)}
|
|
|
|
{subGroups.length === 0 && <GroupTable refresh={refresh} />}
|
|
|
|
</DrawerHead>
|
|
|
|
</DrawerPanelContent>
|
|
|
|
}
|
2021-03-16 12:37:57 +00:00
|
|
|
>
|
2022-10-10 15:03:27 +00:00
|
|
|
<DrawerContentBody>
|
|
|
|
<GroupTree refresh={refresh} />
|
|
|
|
</DrawerContentBody>
|
|
|
|
</DrawerContent>
|
|
|
|
</Drawer>
|
2020-09-18 08:04:55 +00:00
|
|
|
</PageSection>
|
2020-10-14 20:47:29 +00:00
|
|
|
</>
|
2020-09-15 19:54:52 +00:00
|
|
|
);
|
2021-10-29 16:11:06 +00:00
|
|
|
}
|