2021-04-29 06:28:59 +00:00
|
|
|
import React, { useState } from "react";
|
2021-11-09 17:53:06 +00:00
|
|
|
import { Link, useHistory, useLocation } from "react-router-dom";
|
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,
|
2020-10-27 23:45:35 +00:00
|
|
|
AlertVariant,
|
2021-03-16 12:37:57 +00:00
|
|
|
Tab,
|
|
|
|
TabTitleText,
|
|
|
|
Tabs,
|
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";
|
2020-12-14 08:57:05 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
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";
|
|
|
|
import { toGroupsSearch } from "./routes/GroupsSearch";
|
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";
|
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);
|
|
|
|
|
2020-11-12 12:55:52 +00:00
|
|
|
const adminClient = useAdminClient();
|
2021-03-24 14:07:49 +00:00
|
|
|
const { subGroups, setSubGroups, currentGroup } = useSubGroups();
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-01-12 14:00:44 +00:00
|
|
|
const { realm } = useRealm();
|
|
|
|
|
2021-03-24 14:07:49 +00:00
|
|
|
const [rename, setRename] = useState<string>();
|
|
|
|
|
2021-03-16 12:37:57 +00:00
|
|
|
const history = useHistory();
|
2021-03-19 18:37:21 +00:00
|
|
|
const location = useLocation();
|
2021-01-12 14:00:44 +00:00
|
|
|
const id = getLastId(location.pathname);
|
|
|
|
|
2021-01-05 19:56:16 +00:00
|
|
|
const deleteGroup = async (group: GroupRepresentation) => {
|
2020-12-14 08:57:05 +00:00
|
|
|
try {
|
2021-01-05 19:56:16 +00:00
|
|
|
await adminClient.groups.del({
|
2020-12-14 08:57:05 +00:00
|
|
|
id: group.id!,
|
2020-10-27 23:45:35 +00:00
|
|
|
});
|
2021-12-03 11:16:07 +00:00
|
|
|
addAlert(t("groupDeleted", { count: 1 }), AlertVariant.success);
|
2020-12-14 08:57:05 +00:00
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("groups:groupDeleteError", error);
|
2020-12-14 08:57:05 +00:00
|
|
|
}
|
2021-01-05 19:56:16 +00:00
|
|
|
return true;
|
2020-12-14 08:57:05 +00:00
|
|
|
};
|
2020-10-27 23:45:35 +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!) {
|
|
|
|
const group = await adminClient.groups.findOne({ id: i });
|
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
|
|
|
);
|
|
|
|
|
2021-03-24 14:07:49 +00:00
|
|
|
const SearchDropdown = (
|
|
|
|
<DropdownItem
|
|
|
|
key="searchGroup"
|
2021-11-09 17:53:06 +00:00
|
|
|
component={
|
|
|
|
<Link data-testid="searchGroup" to={toGroupsSearch({ realm })}>
|
|
|
|
{t("searchGroup")}
|
|
|
|
</Link>
|
|
|
|
}
|
|
|
|
/>
|
2021-03-24 14:07:49 +00:00
|
|
|
);
|
|
|
|
|
2020-09-15 19:54:52 +00:00
|
|
|
return (
|
2020-10-14 20:47:29 +00:00
|
|
|
<>
|
2021-03-24 14:07:49 +00:00
|
|
|
{rename && (
|
|
|
|
<GroupsModal
|
|
|
|
id={id}
|
|
|
|
rename={rename}
|
|
|
|
refresh={(group) =>
|
|
|
|
setSubGroups([...subGroups.slice(0, subGroups.length - 1), group!])
|
|
|
|
}
|
|
|
|
handleModalToggle={() => setRename(undefined)}
|
|
|
|
/>
|
|
|
|
)}
|
2021-03-01 15:06:04 +00:00
|
|
|
<ViewHeader
|
2022-02-21 14:58:28 +00:00
|
|
|
titleKey={!id ? "groups:groups" : currentGroup()?.name!}
|
2021-03-31 13:16:58 +00:00
|
|
|
subKey={!id ? "groups:groupsDescription" : ""}
|
2021-12-21 15:32:53 +00:00
|
|
|
helpUrl={!id ? helpUrls.groupsUrl : ""}
|
2021-03-31 13:16:58 +00:00
|
|
|
divider={!id}
|
2021-03-24 14:07:49 +00:00
|
|
|
dropdownItems={
|
|
|
|
id
|
|
|
|
? [
|
|
|
|
SearchDropdown,
|
|
|
|
<DropdownItem
|
|
|
|
data-testid="renameGroupAction"
|
|
|
|
key="renameGroup"
|
2022-02-21 14:58:28 +00:00
|
|
|
onClick={() => setRename(currentGroup()?.name)}
|
2021-03-24 14:07:49 +00:00
|
|
|
>
|
|
|
|
{t("renameGroup")}
|
|
|
|
</DropdownItem>,
|
|
|
|
<DropdownItem
|
|
|
|
data-testid="deleteGroup"
|
|
|
|
key="deleteGroup"
|
2021-12-03 11:16:07 +00:00
|
|
|
onClick={async () => {
|
|
|
|
await deleteGroup({ id });
|
2021-11-09 17:53:06 +00:00
|
|
|
history.push(toGroups({ realm }));
|
2021-03-24 14:07:49 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("deleteGroup")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]
|
|
|
|
: [SearchDropdown]
|
|
|
|
}
|
2021-03-01 15:06:04 +00:00
|
|
|
/>
|
2021-03-31 13:16:58 +00:00
|
|
|
<PageSection variant={PageSectionVariants.light} className="pf-u-p-0">
|
2021-03-16 12:37:57 +00:00
|
|
|
{subGroups.length > 0 && (
|
|
|
|
<Tabs
|
2021-03-31 13:16:58 +00:00
|
|
|
inset={{
|
|
|
|
default: "insetNone",
|
|
|
|
md: "insetSm",
|
|
|
|
xl: "inset2xl",
|
|
|
|
"2xl": "insetLg",
|
|
|
|
}}
|
2021-03-16 12:37:57 +00:00
|
|
|
activeKey={activeTab}
|
|
|
|
onSelect={(_, key) => setActiveTab(key as number)}
|
|
|
|
isBox
|
|
|
|
>
|
|
|
|
<Tab
|
|
|
|
data-testid="groups"
|
|
|
|
eventKey={0}
|
|
|
|
title={<TabTitleText>{t("childGroups")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<GroupTable />
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
data-testid="members"
|
|
|
|
eventKey={1}
|
|
|
|
title={<TabTitleText>{t("members")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<Members />
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
data-testid="attributes"
|
|
|
|
eventKey={2}
|
2021-03-30 01:03:26 +00:00
|
|
|
title={<TabTitleText>{t("common:attributes")}</TabTitleText>}
|
2021-03-16 12:37:57 +00:00
|
|
|
>
|
|
|
|
<GroupAttributes />
|
|
|
|
</Tab>
|
2021-12-07 12:56:25 +00:00
|
|
|
<Tab
|
|
|
|
eventKey={3}
|
|
|
|
data-testid="role-mapping-tab"
|
|
|
|
title={<TabTitleText>{t("roleMapping")}</TabTitleText>}
|
|
|
|
>
|
2022-02-21 14:58:28 +00:00
|
|
|
<GroupRoleMapping id={id!} name={currentGroup()?.name!} />
|
2021-12-07 12:56:25 +00:00
|
|
|
</Tab>
|
2022-04-28 13:18:14 +00:00
|
|
|
<Tab
|
|
|
|
eventKey={4}
|
|
|
|
data-testid="permissionsTab"
|
|
|
|
title={<TabTitleText>{t("common:permissions")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<PermissionsTab id={id} type="groups" />
|
|
|
|
</Tab>
|
2021-03-16 12:37:57 +00:00
|
|
|
</Tabs>
|
2021-03-01 15:06:04 +00:00
|
|
|
)}
|
2021-03-16 12:37:57 +00:00
|
|
|
{subGroups.length === 0 && <GroupTable />}
|
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
|
|
|
}
|