2023-04-18 16:50:46 +00:00
|
|
|
import type GroupRepresentation from "@keycloak/keycloak-admin-client/lib/defs/groupRepresentation";
|
2020-09-28 15:58:03 +00:00
|
|
|
import {
|
2023-04-18 16:50:46 +00:00
|
|
|
Drawer,
|
|
|
|
DrawerContent,
|
|
|
|
DrawerContentBody,
|
|
|
|
DrawerHead,
|
|
|
|
DrawerPanelContent,
|
2020-09-28 15:58:03 +00:00
|
|
|
DropdownItem,
|
|
|
|
PageSection,
|
|
|
|
PageSectionVariants,
|
2021-03-16 12:37:57 +00:00
|
|
|
Tab,
|
|
|
|
TabTitleText,
|
|
|
|
Tabs,
|
2020-09-28 15:58:03 +00:00
|
|
|
} from "@patternfly/react-core";
|
2023-04-18 16:50:46 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useLocation, useNavigate } from "react-router-dom";
|
2020-09-09 09:07:17 +00:00
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../admin-client";
|
2023-04-18 16:50:46 +00:00
|
|
|
import { GroupBreadCrumbs } from "../components/bread-crumb/GroupBreadCrumbs";
|
|
|
|
import { PermissionsTab } from "../components/permission-tab/PermissionTab";
|
2020-12-14 08:57:05 +00:00
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
2023-04-18 16:50:46 +00:00
|
|
|
import { useAccess } from "../context/access/Access";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { useFetch } from "../context/auth/AdminClient";
|
2021-01-12 14:00:44 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2023-04-18 16:50:46 +00:00
|
|
|
import helpUrls from "../help-urls";
|
|
|
|
import useIsFeatureEnabled, { Feature } from "../utils/useIsFeatureEnabled";
|
|
|
|
import useToggle from "../utils/useToggle";
|
2023-04-11 11:28:24 +00:00
|
|
|
import { GroupAttributes } from "./GroupAttributes";
|
|
|
|
import { GroupRoleMapping } from "./GroupRoleMapping";
|
2023-04-18 16:50:46 +00:00
|
|
|
import { GroupTable } from "./GroupTable";
|
|
|
|
import { GroupsModal } from "./GroupsModal";
|
|
|
|
import { Members } from "./Members";
|
|
|
|
import { useSubGroups } from "./SubGroupsContext";
|
2023-04-11 11:28:24 +00:00
|
|
|
import { DeleteGroup } from "./components/DeleteGroup";
|
2023-04-18 16:50:46 +00:00
|
|
|
import { GroupTree } from "./components/GroupTree";
|
|
|
|
import { getId, getLastId } from "./groupIdUtils";
|
|
|
|
import { toGroups } from "./routes/Groups";
|
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);
|
|
|
|
|
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();
|
2023-04-18 16:50:46 +00:00
|
|
|
const isFeatureEnabled = useIsFeatureEnabled();
|
2022-11-17 10:18:52 +00:00
|
|
|
const canViewPermissions =
|
2023-04-18 16:50:46 +00:00
|
|
|
isFeatureEnabled(Feature.AdminFineGrainedAuthz) &&
|
2022-11-17 10:18:52 +00:00
|
|
|
hasAccess("manage-authorization", "manage-users", "manage-clients");
|
2022-05-09 10:44:37 +00:00
|
|
|
const canManageGroup =
|
|
|
|
hasAccess("manage-users") || currentGroup()?.access?.manage;
|
|
|
|
const canManageRoles = hasAccess("manage-users");
|
2023-02-17 18:23:02 +00:00
|
|
|
const canViewDetails =
|
|
|
|
hasAccess("query-groups", "view-users") ||
|
|
|
|
hasAccess("manage-users", "query-groups");
|
2023-03-31 19:11:13 +00:00
|
|
|
const canViewMembers =
|
|
|
|
hasAccess("view-users") ||
|
|
|
|
currentGroup()?.access?.viewMembers ||
|
|
|
|
currentGroup()?.access?.manageMembers;
|
2022-05-09 10:44:37 +00:00
|
|
|
|
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
|
2023-03-13 06:45:26 +00:00
|
|
|
mountOnEnter
|
|
|
|
unmountOnExit
|
2022-10-10 15:03:27 +00:00
|
|
|
>
|
|
|
|
<Tab
|
|
|
|
data-testid="groups"
|
|
|
|
eventKey={0}
|
|
|
|
title={<TabTitleText>{t("childGroups")}</TabTitleText>}
|
|
|
|
>
|
2023-02-17 18:23:02 +00:00
|
|
|
<GroupTable
|
|
|
|
refresh={refresh}
|
|
|
|
canViewDetails={canViewDetails}
|
|
|
|
/>
|
2022-10-10 15:03:27 +00:00
|
|
|
</Tab>
|
2023-03-31 19:11:13 +00:00
|
|
|
{canViewMembers && (
|
|
|
|
<Tab
|
|
|
|
data-testid="members"
|
|
|
|
eventKey={1}
|
|
|
|
title={<TabTitleText>{t("members")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<Members />
|
|
|
|
</Tab>
|
|
|
|
)}
|
2022-10-10 15:03:27 +00:00
|
|
|
<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>
|
|
|
|
)}
|
2023-02-17 18:23:02 +00:00
|
|
|
{subGroups.length === 0 && (
|
|
|
|
<GroupTable
|
|
|
|
refresh={refresh}
|
|
|
|
canViewDetails={canViewDetails}
|
|
|
|
/>
|
|
|
|
)}
|
2022-10-10 15:03:27 +00:00
|
|
|
</DrawerHead>
|
|
|
|
</DrawerPanelContent>
|
|
|
|
}
|
2021-03-16 12:37:57 +00:00
|
|
|
>
|
2022-10-10 15:03:27 +00:00
|
|
|
<DrawerContentBody>
|
2023-02-17 18:23:02 +00:00
|
|
|
<GroupTree refresh={refresh} canViewDetails={canViewDetails} />
|
2022-10-10 15:03:27 +00:00
|
|
|
</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
|
|
|
}
|