2021-05-18 12:25:46 +00:00
|
|
|
import React, { useState } from "react";
|
2021-06-21 17:55:51 +00:00
|
|
|
import { Link, useLocation } from "react-router-dom";
|
2021-03-16 12:37:57 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-02-02 10:33:57 +00:00
|
|
|
import { uniqBy } from "lodash-es";
|
2021-03-24 14:07:49 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
Checkbox,
|
|
|
|
Dropdown,
|
|
|
|
DropdownItem,
|
|
|
|
KebabToggle,
|
|
|
|
ToolbarItem,
|
|
|
|
} from "@patternfly/react-core";
|
2021-03-16 12:37:57 +00:00
|
|
|
|
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";
|
2021-03-16 12:37:57 +00:00
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
2021-06-21 17:55:51 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2021-03-24 14:07:49 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2021-03-16 12:37:57 +00:00
|
|
|
import { emptyFormatter } from "../util";
|
|
|
|
|
|
|
|
import { getLastId } from "./groupIdUtils";
|
|
|
|
import { useSubGroups } from "./SubGroupsContext";
|
2021-03-24 14:07:49 +00:00
|
|
|
import { MemberModal } from "./MembersModal";
|
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
2021-06-21 17:55:51 +00:00
|
|
|
import { GroupPath } from "../components/group/GroupPath";
|
2021-08-10 13:07:59 +00:00
|
|
|
import { toUser } from "../user/routes/User";
|
2022-05-09 10:44:37 +00:00
|
|
|
import { useAccess } from "../context/access/Access";
|
2021-03-16 12:37:57 +00:00
|
|
|
|
|
|
|
type MembersOf = UserRepresentation & {
|
|
|
|
membership: GroupRepresentation[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Members = () => {
|
|
|
|
const { t } = useTranslation("groups");
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-06-21 17:55:51 +00:00
|
|
|
const { realm } = useRealm();
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-03-19 18:37:21 +00:00
|
|
|
const location = useLocation();
|
2021-03-16 12:37:57 +00:00
|
|
|
const id = getLastId(location.pathname);
|
|
|
|
const [includeSubGroup, setIncludeSubGroup] = useState(false);
|
2021-05-18 12:25:46 +00:00
|
|
|
const { currentGroup } = useSubGroups();
|
2021-03-24 14:07:49 +00:00
|
|
|
const [addMembers, setAddMembers] = useState(false);
|
|
|
|
const [isKebabOpen, setIsKebabOpen] = useState(false);
|
|
|
|
const [selectedRows, setSelectedRows] = useState<UserRepresentation[]>([]);
|
2022-05-09 10:44:37 +00:00
|
|
|
const { hasAccess } = useAccess();
|
|
|
|
|
|
|
|
const isManager =
|
|
|
|
hasAccess("manage-users") || currentGroup()!.access!.manageMembership;
|
2021-03-16 12:37:57 +00:00
|
|
|
|
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
const refresh = () => setKey(new Date().getTime());
|
|
|
|
|
|
|
|
const getMembership = async (id: string) =>
|
|
|
|
await adminClient.users.listGroups({ id: id! });
|
|
|
|
|
|
|
|
const getSubGroups = (groups: GroupRepresentation[]) => {
|
|
|
|
let subGroups: GroupRepresentation[] = [];
|
|
|
|
for (const group of groups!) {
|
|
|
|
subGroups.push(group);
|
|
|
|
const subs = getSubGroups(group.subGroups!);
|
|
|
|
subGroups = subGroups.concat(subs);
|
|
|
|
}
|
|
|
|
return subGroups;
|
|
|
|
};
|
|
|
|
|
|
|
|
const loader = async (first?: number, max?: number) => {
|
|
|
|
let members = await adminClient.groups.listMembers({
|
|
|
|
id: id!,
|
|
|
|
first,
|
|
|
|
max,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (includeSubGroup) {
|
2022-02-21 14:58:28 +00:00
|
|
|
const subGroups = getSubGroups(currentGroup()?.subGroups!);
|
2021-03-16 12:37:57 +00:00
|
|
|
for (const group of subGroups) {
|
|
|
|
members = members.concat(
|
|
|
|
await adminClient.groups.listMembers({ id: group.id! })
|
|
|
|
);
|
|
|
|
}
|
2022-02-02 10:33:57 +00:00
|
|
|
members = uniqBy(members, (member) => member.username);
|
2021-03-16 12:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const memberOfPromises = await Promise.all(
|
|
|
|
members.map((member) => getMembership(member.id!))
|
|
|
|
);
|
|
|
|
return members.map((member: UserRepresentation, i) => {
|
|
|
|
return { ...member, membership: memberOfPromises[i] };
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const MemberOfRenderer = (member: MembersOf) => {
|
|
|
|
return (
|
|
|
|
<>
|
2021-06-21 17:55:51 +00:00
|
|
|
{member.membership.map((group, index) => (
|
|
|
|
<>
|
|
|
|
<GroupPath key={group.id} group={group} />
|
|
|
|
{member.membership[index + 1] ? ", " : ""}
|
|
|
|
</>
|
2021-03-16 12:37:57 +00:00
|
|
|
))}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-06-21 17:55:51 +00:00
|
|
|
const UserDetailLink = (user: MembersOf) => (
|
2021-08-26 12:15:28 +00:00
|
|
|
<Link key={user.id} to={toUser({ realm, id: user.id!, tab: "settings" })}>
|
|
|
|
{user.username}
|
|
|
|
</Link>
|
2021-06-21 17:55:51 +00:00
|
|
|
);
|
2021-03-16 12:37:57 +00:00
|
|
|
return (
|
2021-03-24 14:07:49 +00:00
|
|
|
<>
|
|
|
|
{addMembers && (
|
|
|
|
<MemberModal
|
|
|
|
groupId={id!}
|
|
|
|
onClose={() => {
|
|
|
|
setAddMembers(false);
|
|
|
|
refresh();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<KeycloakDataTable
|
2021-12-07 12:56:25 +00:00
|
|
|
data-testid="members-table"
|
2021-05-18 12:25:46 +00:00
|
|
|
key={`${id}${key}${includeSubGroup}`}
|
2021-03-24 14:07:49 +00:00
|
|
|
loader={loader}
|
|
|
|
ariaLabelKey="groups:members"
|
|
|
|
isPaginated
|
|
|
|
canSelectAll
|
|
|
|
onSelect={(rows) => setSelectedRows([...rows])}
|
|
|
|
toolbarItem={
|
2022-05-09 10:44:37 +00:00
|
|
|
isManager && (
|
|
|
|
<>
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button
|
|
|
|
data-testid="addMember"
|
|
|
|
variant="primary"
|
|
|
|
onClick={() => setAddMembers(true)}
|
|
|
|
>
|
|
|
|
{t("addMember")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
<ToolbarItem>
|
|
|
|
<Checkbox
|
|
|
|
data-testid="includeSubGroupsCheck"
|
|
|
|
label={t("includeSubGroups")}
|
|
|
|
id="kc-include-sub-groups"
|
|
|
|
isChecked={includeSubGroup}
|
|
|
|
onChange={() => setIncludeSubGroup(!includeSubGroup)}
|
|
|
|
/>
|
|
|
|
</ToolbarItem>
|
|
|
|
<ToolbarItem>
|
|
|
|
<Dropdown
|
|
|
|
toggle={
|
|
|
|
<KebabToggle
|
|
|
|
onToggle={() => setIsKebabOpen(!isKebabOpen)}
|
|
|
|
isDisabled={selectedRows.length === 0}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
isOpen={isKebabOpen}
|
|
|
|
isPlain
|
|
|
|
dropdownItems={[
|
|
|
|
<DropdownItem
|
|
|
|
key="action"
|
|
|
|
component="button"
|
|
|
|
onClick={async () => {
|
|
|
|
try {
|
|
|
|
await Promise.all(
|
|
|
|
selectedRows.map((user) =>
|
|
|
|
adminClient.users.delFromGroup({
|
|
|
|
id: user.id!,
|
|
|
|
groupId: id!,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
setIsKebabOpen(false);
|
|
|
|
addAlert(
|
|
|
|
t("usersLeft", { count: selectedRows.length }),
|
|
|
|
AlertVariant.success
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
addError("groups:usersLeftError", error);
|
|
|
|
}
|
2021-03-24 14:07:49 +00:00
|
|
|
|
2022-05-09 10:44:37 +00:00
|
|
|
refresh();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("leave")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</ToolbarItem>
|
|
|
|
</>
|
|
|
|
)
|
2021-03-24 14:07:49 +00:00
|
|
|
}
|
2022-05-09 10:44:37 +00:00
|
|
|
actions={
|
|
|
|
isManager
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
title: t("leave"),
|
|
|
|
onRowClick: async (user) => {
|
|
|
|
try {
|
|
|
|
await adminClient.users.delFromGroup({
|
|
|
|
id: user.id!,
|
|
|
|
groupId: id!,
|
|
|
|
});
|
|
|
|
addAlert(
|
|
|
|
t("usersLeft", { count: 1 }),
|
|
|
|
AlertVariant.success
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
addError("groups:usersLeftError", error);
|
|
|
|
}
|
2021-06-21 17:55:51 +00:00
|
|
|
|
2022-05-09 10:44:37 +00:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
: []
|
|
|
|
}
|
2021-03-24 14:07:49 +00:00
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "username",
|
|
|
|
displayKey: "common:name",
|
2021-06-21 17:55:51 +00:00
|
|
|
cellRenderer: UserDetailLink,
|
2021-03-24 14:07:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "email",
|
|
|
|
displayKey: "groups:email",
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "firstName",
|
|
|
|
displayKey: "groups:firstName",
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "lastName",
|
|
|
|
displayKey: "groups:lastName",
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "membership",
|
|
|
|
displayKey: "groups:membership",
|
|
|
|
cellRenderer: MemberOfRenderer,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
emptyState={
|
|
|
|
<ListEmptyState
|
|
|
|
message={t("users:noUsersFound")}
|
2022-05-09 10:44:37 +00:00
|
|
|
instructions={isManager ? t("users:emptyInstructions") : undefined}
|
|
|
|
primaryActionText={isManager ? t("addMember") : undefined}
|
2021-03-24 14:07:49 +00:00
|
|
|
onPrimaryAction={() => setAddMembers(true)}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</>
|
2021-03-16 12:37:57 +00:00
|
|
|
);
|
|
|
|
};
|