2021-03-23 19:02:27 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
Checkbox,
|
2021-04-22 20:37:10 +00:00
|
|
|
Popover,
|
2021-03-23 19:02:27 +00:00
|
|
|
} from "@patternfly/react-core";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { QuestionCircleIcon } from "@patternfly/react-icons";
|
2021-03-23 19:02:27 +00:00
|
|
|
import { cellWidth } from "@patternfly/react-table";
|
2021-08-26 08:39:35 +00:00
|
|
|
import type GroupRepresentation from "@keycloak/keycloak-admin-client/lib/defs/groupRepresentation";
|
|
|
|
import type UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation";
|
2022-02-02 10:33:57 +00:00
|
|
|
import { intersectionBy, sortBy } from "lodash-es";
|
2021-07-21 09:30:18 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
2021-06-21 17:55:51 +00:00
|
|
|
import { GroupPath } from "../components/group/GroupPath";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { GroupPickerDialog } from "../components/group/GroupPickerDialog";
|
|
|
|
import { useHelp } from "../components/help-enabler/HelpHeader";
|
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
2021-09-02 13:44:31 +00:00
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { emptyFormatter } from "../util";
|
2021-04-14 18:19:39 +00:00
|
|
|
|
2021-09-02 13:44:31 +00:00
|
|
|
type UserGroupsProps = {
|
|
|
|
user: UserRepresentation;
|
2021-04-14 18:19:39 +00:00
|
|
|
};
|
2021-03-23 19:02:27 +00:00
|
|
|
|
2021-09-02 13:44:31 +00:00
|
|
|
export const UserGroups = ({ user }: UserGroupsProps) => {
|
2021-06-16 11:35:03 +00:00
|
|
|
const { t } = useTranslation("users");
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-03-23 19:02:27 +00:00
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
const refresh = () => setKey(new Date().getTime());
|
|
|
|
|
2021-11-03 13:45:37 +00:00
|
|
|
const [selectedGroups, setSelectedGroups] = useState<GroupRepresentation[]>(
|
|
|
|
[]
|
|
|
|
);
|
2021-03-23 19:02:27 +00:00
|
|
|
const [search, setSearch] = useState("");
|
|
|
|
|
2021-04-01 18:47:05 +00:00
|
|
|
const [isDirectMembership, setDirectMembership] = useState(true);
|
2021-04-05 17:55:17 +00:00
|
|
|
const [directMembershipList, setDirectMembershipList] = useState<
|
|
|
|
GroupRepresentation[]
|
|
|
|
>([]);
|
2021-03-23 19:02:27 +00:00
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
2021-07-21 09:30:18 +00:00
|
|
|
const { enabled } = useHelp();
|
2021-04-22 20:37:10 +00:00
|
|
|
|
2021-03-23 19:02:27 +00:00
|
|
|
const adminClient = useAdminClient();
|
2021-04-01 18:47:05 +00:00
|
|
|
const alphabetize = (groupsList: GroupRepresentation[]) => {
|
2021-11-03 13:45:37 +00:00
|
|
|
return sortBy(groupsList, (group) => group.path?.toUpperCase());
|
2021-04-01 18:47:05 +00:00
|
|
|
};
|
|
|
|
|
2021-03-23 19:02:27 +00:00
|
|
|
const loader = async (first?: number, max?: number, search?: string) => {
|
|
|
|
const params: { [name: string]: string | number } = {
|
|
|
|
first: first!,
|
|
|
|
max: max!,
|
|
|
|
};
|
|
|
|
|
|
|
|
const searchParam = search || "";
|
|
|
|
if (searchParam) {
|
|
|
|
params.search = searchParam;
|
|
|
|
setSearch(searchParam);
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:21:10 +00:00
|
|
|
const joinedUserGroups = await adminClient.users.listGroups({
|
|
|
|
...params,
|
2021-09-02 13:44:31 +00:00
|
|
|
id: user.id!,
|
2021-05-06 19:21:10 +00:00
|
|
|
});
|
2021-04-01 18:47:05 +00:00
|
|
|
|
|
|
|
const allCreatedGroups = await adminClient.groups.find();
|
|
|
|
|
2021-05-06 19:21:10 +00:00
|
|
|
const getAllPaths = joinedUserGroups.reduce(
|
2021-04-01 18:47:05 +00:00
|
|
|
(acc: string[], cur) => (cur.path && acc.push(cur.path), acc),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
const parentGroupNames: string[] = [];
|
|
|
|
const allGroupMembership: string[] = [];
|
|
|
|
const slicedGroups: string[] = [];
|
|
|
|
const rootLevelGroups: GroupRepresentation[] = [...allCreatedGroups];
|
|
|
|
let allPaths: GroupRepresentation[] = [];
|
|
|
|
|
|
|
|
const getAllSubgroupPaths = (
|
|
|
|
o: any,
|
|
|
|
f: any,
|
|
|
|
context: GroupRepresentation[]
|
|
|
|
): GroupRepresentation[] => {
|
|
|
|
f(o, context);
|
|
|
|
if (typeof o !== "object") return context;
|
|
|
|
if (Array.isArray(o))
|
|
|
|
return o.forEach((e) => getAllSubgroupPaths(e, f, context)), context;
|
|
|
|
for (const prop in o) getAllSubgroupPaths(o[prop], f, context);
|
|
|
|
return context;
|
|
|
|
};
|
|
|
|
|
|
|
|
const arr = getAllSubgroupPaths(
|
|
|
|
rootLevelGroups,
|
2021-08-12 11:15:47 +00:00
|
|
|
(
|
|
|
|
x: GroupRepresentation | undefined,
|
|
|
|
context: GroupRepresentation[][]
|
|
|
|
) => {
|
|
|
|
if (x?.subGroups) context.push(x.subGroups);
|
2021-04-01 18:47:05 +00:00
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const allSubgroups: GroupRepresentation[] = [].concat(...(arr as any));
|
|
|
|
|
|
|
|
allPaths = [...rootLevelGroups, ...allSubgroups];
|
|
|
|
|
|
|
|
getAllPaths.forEach((item) => {
|
|
|
|
const paths = item.split("/");
|
|
|
|
const groups: string[] = [];
|
|
|
|
|
|
|
|
paths.reduce((acc, value) => {
|
|
|
|
const path = acc + "/" + value;
|
|
|
|
groups.push(path);
|
|
|
|
return path;
|
|
|
|
}, "");
|
|
|
|
|
|
|
|
for (let i = 1; i < groups.length; i++) {
|
|
|
|
slicedGroups.push(groups[i].substring(1));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
allGroupMembership.push(...slicedGroups);
|
|
|
|
|
|
|
|
allPaths.forEach((item) => {
|
|
|
|
if (item.subGroups!.length !== 0) {
|
|
|
|
allPaths.push(...item!.subGroups!);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
allPaths = allPaths.filter((group) =>
|
|
|
|
allGroupMembership.includes(group.path as any)
|
|
|
|
);
|
|
|
|
|
|
|
|
const topLevelGroups = allCreatedGroups.filter((value) =>
|
|
|
|
parentGroupNames.includes(value.name!)
|
|
|
|
);
|
|
|
|
|
|
|
|
const subgroupArray: any[] = [];
|
|
|
|
|
|
|
|
topLevelGroups.forEach((group) => subgroupArray.push(group.subGroups));
|
|
|
|
|
2021-05-06 19:21:10 +00:00
|
|
|
const directMembership = joinedUserGroups!.filter(
|
2021-04-01 18:47:05 +00:00
|
|
|
(value) => !topLevelGroups.includes(value)
|
|
|
|
);
|
|
|
|
|
2021-04-05 17:55:17 +00:00
|
|
|
setDirectMembershipList(directMembership);
|
2021-04-14 18:19:39 +00:00
|
|
|
|
2021-04-01 18:47:05 +00:00
|
|
|
const filterDupesfromGroups = allPaths.filter(
|
|
|
|
(thing, index, self) =>
|
|
|
|
index === self.findIndex((t) => t.name === thing.name)
|
|
|
|
);
|
|
|
|
|
2021-05-06 19:21:10 +00:00
|
|
|
if (!isDirectMembership) {
|
|
|
|
return alphabetize(filterDupesfromGroups);
|
2021-04-01 18:47:05 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 19:21:10 +00:00
|
|
|
return alphabetize(directMembership);
|
2021-03-23 19:02:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
refresh();
|
|
|
|
}, [isDirectMembership]);
|
|
|
|
|
2021-08-26 12:15:28 +00:00
|
|
|
const AliasRenderer = (group: GroupRepresentation) => group.name;
|
2021-03-23 19:02:27 +00:00
|
|
|
|
2021-04-14 18:19:39 +00:00
|
|
|
const toggleModal = () => {
|
|
|
|
setOpen(!open);
|
|
|
|
};
|
|
|
|
|
2021-03-23 19:02:27 +00:00
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
2021-06-16 11:35:03 +00:00
|
|
|
titleKey: t("leaveGroup", {
|
2021-11-03 13:45:37 +00:00
|
|
|
count: selectedGroups.length,
|
|
|
|
name: selectedGroups[0]?.name,
|
2021-03-23 19:02:27 +00:00
|
|
|
}),
|
2021-06-16 11:35:03 +00:00
|
|
|
messageKey: t("leaveGroupConfirmDialog", {
|
2021-11-03 13:45:37 +00:00
|
|
|
count: selectedGroups.length,
|
|
|
|
groupname: selectedGroups[0]?.name,
|
2021-09-02 13:44:31 +00:00
|
|
|
username: user.username,
|
2021-03-23 19:02:27 +00:00
|
|
|
}),
|
2021-06-16 11:35:03 +00:00
|
|
|
continueButtonLabel: "leave",
|
2021-03-23 19:02:27 +00:00
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
2021-11-03 13:45:37 +00:00
|
|
|
await Promise.all(
|
|
|
|
selectedGroups.map((group) =>
|
|
|
|
adminClient.users.delFromGroup({
|
|
|
|
id: user.id!,
|
|
|
|
groupId: group.id!,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2021-03-23 19:02:27 +00:00
|
|
|
refresh();
|
2021-06-16 11:35:03 +00:00
|
|
|
addAlert(t("removedGroupMembership"), AlertVariant.success);
|
2021-03-23 19:02:27 +00:00
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("users:removedGroupMembershipError", error);
|
2021-03-23 19:02:27 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-11-03 13:45:37 +00:00
|
|
|
const leave = (group: GroupRepresentation[]) => {
|
|
|
|
setSelectedGroups(group);
|
2021-03-23 19:02:27 +00:00
|
|
|
toggleDeleteDialog();
|
|
|
|
};
|
|
|
|
|
2021-04-05 17:55:17 +00:00
|
|
|
const LeaveButtonRenderer = (group: GroupRepresentation) => {
|
2021-06-16 11:35:03 +00:00
|
|
|
const canLeaveGroup =
|
2021-04-05 17:55:17 +00:00
|
|
|
directMembershipList.some((item) => item.id === group.id) ||
|
2021-04-14 18:19:39 +00:00
|
|
|
directMembershipList.length === 0 ||
|
2021-06-16 11:35:03 +00:00
|
|
|
isDirectMembership;
|
|
|
|
return (
|
2021-08-26 12:15:28 +00:00
|
|
|
canLeaveGroup && (
|
|
|
|
<Button
|
|
|
|
data-testid={`leave-${group.name}`}
|
2021-11-03 13:45:37 +00:00
|
|
|
onClick={() => leave([group])}
|
2021-08-26 12:15:28 +00:00
|
|
|
variant="link"
|
2022-04-21 15:03:48 +00:00
|
|
|
isDisabled={!user.access?.manageGroupMembership}
|
2021-08-26 12:15:28 +00:00
|
|
|
>
|
|
|
|
{t("leave")}
|
|
|
|
</Button>
|
|
|
|
)
|
2021-06-16 11:35:03 +00:00
|
|
|
);
|
2021-04-05 17:55:17 +00:00
|
|
|
};
|
|
|
|
|
2021-04-19 19:53:28 +00:00
|
|
|
const addGroups = async (groups: GroupRepresentation[]): Promise<void> => {
|
|
|
|
const newGroups = groups;
|
|
|
|
|
|
|
|
newGroups.forEach(async (group) => {
|
|
|
|
try {
|
|
|
|
await adminClient.users.addToGroup({
|
2021-09-02 13:44:31 +00:00
|
|
|
id: user.id!,
|
2021-04-19 19:53:28 +00:00
|
|
|
groupId: group.id!,
|
|
|
|
});
|
|
|
|
refresh();
|
2021-06-16 11:35:03 +00:00
|
|
|
addAlert(t("addedGroupMembership"), AlertVariant.success);
|
2021-04-19 19:53:28 +00:00
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("users:addedGroupMembershipError", error);
|
2021-04-19 19:53:28 +00:00
|
|
|
}
|
|
|
|
});
|
2021-04-14 18:19:39 +00:00
|
|
|
};
|
|
|
|
|
2021-06-21 17:55:51 +00:00
|
|
|
const Path = (group: GroupRepresentation) => <GroupPath group={group} />;
|
|
|
|
|
2021-03-23 19:02:27 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-06-23 10:42:14 +00:00
|
|
|
<DeleteConfirm />
|
|
|
|
{open && (
|
|
|
|
<GroupPickerDialog
|
2021-09-02 13:44:31 +00:00
|
|
|
id={user.id}
|
2021-06-23 10:42:14 +00:00
|
|
|
type="selectMany"
|
|
|
|
text={{
|
2021-09-02 13:44:31 +00:00
|
|
|
title: t("joinGroupsFor", { username: user.username }),
|
2021-06-23 10:42:14 +00:00
|
|
|
ok: "users:join",
|
|
|
|
}}
|
|
|
|
onClose={() => setOpen(false)}
|
|
|
|
onConfirm={(groups) => {
|
2022-05-12 13:46:10 +00:00
|
|
|
addGroups(groups || []);
|
2021-06-23 10:42:14 +00:00
|
|
|
setOpen(false);
|
2021-11-03 13:45:37 +00:00
|
|
|
refresh();
|
2021-06-23 10:42:14 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<KeycloakDataTable
|
|
|
|
key={key}
|
|
|
|
loader={loader}
|
|
|
|
className="keycloak_user-section_groups-table"
|
|
|
|
isPaginated
|
|
|
|
ariaLabelKey="roles:roleList"
|
|
|
|
searchPlaceholderKey="groups:searchGroup"
|
|
|
|
canSelectAll
|
2021-11-03 13:45:37 +00:00
|
|
|
onSelect={(groups) =>
|
|
|
|
isDirectMembership
|
|
|
|
? setSelectedGroups(groups)
|
|
|
|
: setSelectedGroups(
|
|
|
|
intersectionBy(groups, directMembershipList, "id")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
isRowDisabled={(group) =>
|
|
|
|
!isDirectMembership &&
|
|
|
|
directMembershipList.every((item) => item.id !== group.id)
|
|
|
|
}
|
2021-06-23 10:42:14 +00:00
|
|
|
toolbarItem={
|
|
|
|
<>
|
|
|
|
<Button
|
|
|
|
className="kc-join-group-button"
|
|
|
|
onClick={toggleModal}
|
|
|
|
data-testid="add-group-button"
|
2022-04-21 15:03:48 +00:00
|
|
|
isDisabled={!user.access?.manageGroupMembership}
|
2021-06-23 10:42:14 +00:00
|
|
|
>
|
|
|
|
{t("joinGroup")}
|
|
|
|
</Button>
|
|
|
|
<Checkbox
|
|
|
|
label={t("directMembership")}
|
|
|
|
key="direct-membership-check"
|
|
|
|
id="kc-direct-membership-checkbox"
|
|
|
|
onChange={() => setDirectMembership(!isDirectMembership)}
|
|
|
|
isChecked={isDirectMembership}
|
|
|
|
className="direct-membership-check"
|
|
|
|
/>
|
2021-11-03 13:45:37 +00:00
|
|
|
<Button
|
|
|
|
onClick={() => leave(selectedGroups)}
|
|
|
|
data-testid="leave-group-button"
|
|
|
|
variant="link"
|
|
|
|
isDisabled={selectedGroups.length === 0}
|
|
|
|
>
|
|
|
|
{t("leave")}
|
|
|
|
</Button>
|
|
|
|
|
2021-06-23 10:42:14 +00:00
|
|
|
{enabled && (
|
|
|
|
<Popover
|
|
|
|
aria-label="Basic popover"
|
|
|
|
position="bottom"
|
|
|
|
bodyContent={<div>{t("whoWillAppearPopoverText")}</div>}
|
2021-03-23 19:02:27 +00:00
|
|
|
>
|
2021-06-23 10:42:14 +00:00
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
className="kc-who-will-appear-button"
|
|
|
|
key="who-will-appear-button"
|
|
|
|
icon={<QuestionCircleIcon />}
|
2021-04-22 20:37:10 +00:00
|
|
|
>
|
2021-06-23 10:42:14 +00:00
|
|
|
{t("whoWillAppearLinkText")}
|
|
|
|
</Button>
|
|
|
|
</Popover>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "groupMembership",
|
|
|
|
displayKey: "users:groupMembership",
|
|
|
|
cellRenderer: AliasRenderer,
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
transforms: [cellWidth(40)],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "path",
|
|
|
|
displayKey: "users:path",
|
|
|
|
cellRenderer: Path,
|
|
|
|
transforms: [cellWidth(45)],
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "",
|
|
|
|
cellRenderer: LeaveButtonRenderer,
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
transforms: [cellWidth(20)],
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
emptyState={
|
|
|
|
!search ? (
|
|
|
|
<ListEmptyState
|
|
|
|
hasIcon={true}
|
|
|
|
message={t("noGroups")}
|
|
|
|
instructions={t("noGroupsText")}
|
|
|
|
primaryActionText={t("joinGroup")}
|
|
|
|
onPrimaryAction={toggleModal}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
""
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
2021-03-23 19:02:27 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|