Files
outline/app/scenes/Settings/components/GroupDialogs.tsx
Tom Moorandcodegen-sh[bot] 617504d8bb feat: Add group admin role (#10030)
* Add admin role to GroupUser

This change adds an admin role to GroupUser that allows group admins to:
1. Administer other users in the group
2. Change the group name

Changes include:
- Database migration to add isAdmin field to group_users table
- Updated GroupUser model to include isAdmin field
- Added isGroupAdmin policy utility function
- Updated group policy to allow group admins to update groups
- Added API endpoints for managing admin status
- Updated GroupUsersStore to handle admin functionality
- Added tests for the new functionality

* Replace isAdmin with role-based approach for GroupUser

- Added role field to GroupUser model using UserRole.Admin and UserRole.Member
- Created migration to convert isAdmin boolean to role enum
- Updated policies to be synchronous and require pre-loaded relationships
- Updated API endpoints to support both role and legacy isAdmin parameters
- Updated GroupUsersStore to handle role-based functionality
- Updated tests to use role instead of isAdmin
- Maintained backward compatibility with isAdmin in presenters

* Remove isAdmin logic from GroupUser implementation

- Removed isAdmin parameter from GroupUsersStore methods
- Removed isAdmin field from presenter output
- Removed isAdmin from API schemas
- Removed isAdmin parameter handling in API endpoints
- Updated tests to use role instead of isAdmin
- Simplified role handling throughout the codebase

* lint

* tests

* role -> permission

* fe

* test

* Change permission label from 'Admin' to 'Manage'

---------

Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com>
2025-09-01 05:10:32 -04:00

508 lines
13 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import debounce from "lodash/debounce";
import { observer } from "mobx-react";
import { PlusIcon } from "outline-icons";
import * as React from "react";
import { Trans, useTranslation } from "react-i18next";
import { toast } from "sonner";
import Group from "~/models/Group";
import User from "~/models/User";
import Invite from "~/scenes/Invite";
import { Avatar, AvatarSize } from "~/components/Avatar";
import Badge from "~/components/Badge";
import Button from "~/components/Button";
import ButtonLink from "~/components/ButtonLink";
import ConfirmationDialog from "~/components/ConfirmationDialog";
import DelayedMount from "~/components/DelayedMount";
import Empty from "~/components/Empty";
import Flex from "~/components/Flex";
import Input from "~/components/Input";
import PlaceholderList from "~/components/List/Placeholder";
import PaginatedList from "~/components/PaginatedList";
import { ListItem } from "~/components/Sharing/components/ListItem";
import Text from "~/components/Text";
import Time from "~/components/Time";
import useCurrentTeam from "~/hooks/useCurrentTeam";
import usePolicy from "~/hooks/usePolicy";
import useRequest from "~/hooks/useRequest";
import useStores from "~/hooks/useStores";
import InputMemberPermissionSelect from "~/components/InputMemberPermissionSelect";
import { GroupPermission } from "@shared/types";
import { EmptySelectValue, Permission } from "~/types";
import GroupUser from "~/models/GroupUser";
type Props = {
group: Group;
onSubmit: () => void;
};
export function CreateGroupDialog() {
const { dialogs, groups } = useStores();
const { t } = useTranslation();
const [name, setName] = React.useState<string | undefined>();
const [isSaving, setIsSaving] = React.useState(false);
const handleSubmit = React.useCallback(
async (ev: React.SyntheticEvent) => {
ev.preventDefault();
setIsSaving(true);
const group = new Group(
{
name,
},
groups
);
try {
await group.save();
dialogs.closeAllModals();
dialogs.openModal({
title: t("Group members"),
content: <ViewGroupMembersDialog group={group} />,
});
} catch (err) {
toast.error(err.message);
} finally {
setIsSaving(false);
}
},
[t, dialogs, groups, name]
);
return (
<form onSubmit={handleSubmit}>
<Text as="p" type="secondary">
<Trans>
Groups are for organizing your team. They work best when centered
around a function or a responsibility — Support or Engineering for
example.
</Trans>
</Text>
<Flex>
<Input
type="text"
label="Name"
onChange={(e) => setName(e.target.value)}
value={name}
required
autoFocus
flex
/>
</Flex>
<Text as="p" type="secondary">
<Trans>You’ll be able to add people to the group next.</Trans>
</Text>
<Button type="submit" disabled={isSaving || !name}>
{isSaving ? `${t("Creating")}…` : t("Continue")}
</Button>
</form>
);
}
export function EditGroupDialog({ group, onSubmit }: Props) {
const { t } = useTranslation();
const [name, setName] = React.useState(group.name);
const [isSaving, setIsSaving] = React.useState(false);
const handleSubmit = React.useCallback(
async (ev: React.SyntheticEvent) => {
ev.preventDefault();
setIsSaving(true);
try {
await group.save({
name,
});
onSubmit();
} catch (err) {
toast.error(err.message);
} finally {
setIsSaving(false);
}
},
[group, onSubmit, name]
);
const handleNameChange = React.useCallback(
(ev: React.ChangeEvent<HTMLInputElement>) => {
setName(ev.target.value);
},
[]
);
return (
<form onSubmit={handleSubmit}>
<Text as="p" type="secondary">
<Trans>
You can edit the name of this group at any time, however doing so too
often might confuse your team mates.
</Trans>
</Text>
<Flex>
<Input
type="text"
label={t("Name")}
onChange={handleNameChange}
value={name}
required
autoFocus
flex
/>
</Flex>
<Button type="submit" disabled={isSaving || !name}>
{isSaving ? `${t("Saving")}…` : t("Save")}
</Button>
</form>
);
}
export function DeleteGroupDialog({ group, onSubmit }: Props) {
const { t } = useTranslation();
const handleSubmit = async () => {
await group.delete();
onSubmit();
};
return (
<ConfirmationDialog
onSubmit={handleSubmit}
submitText={t("I’m sure – Delete")}
savingText={`${t("Deleting")}…`}
danger
>
<Trans
defaults="Are you sure about that? Deleting the <em>{{groupName}}</em> group will cause its members to lose access to collections and documents that it is associated with."
values={{
groupName: group.name,
}}
components={{
em: <strong />,
}}
/>
</ConfirmationDialog>
);
}
export const ViewGroupMembersDialog = observer(function ({
group,
}: Pick<Props, "group">) {
const { dialogs, users, groupUsers } = useStores();
const { t } = useTranslation();
const can = usePolicy(group);
const handleAddPeople = React.useCallback(() => {
dialogs.openModal({
title: t(`Add people to {{groupName}}`, {
groupName: group.name,
}),
content: <AddPeopleToGroupDialog group={group} />,
replace: true,
});
}, [t, group, dialogs]);
const handleRemoveUser = React.useCallback(
async (user: User) => {
try {
await groupUsers.delete({
groupId: group.id,
userId: user.id,
});
toast.success(
t(`{{userName}} was removed from the group`, {
userName: user.name,
}),
{
icon: <Avatar model={user} size={AvatarSize.Toast} />,
}
);
} catch (_err) {
toast.error(t("Could not remove user"));
}
},
[t, groupUsers, group.id]
);
return (
<Flex column>
{can.update ? (
<>
<Text as="p" type="secondary">
<Trans
defaults="Add and remove members to the <em>{{groupName}}</em> group. Members of the group will have access to any collections this group has been added to."
values={{
groupName: group.name,
}}
components={{
em: <strong />,
}}
/>
</Text>
{can.update && (
<span>
<Button
type="button"
onClick={handleAddPeople}
icon={<PlusIcon />}
neutral
>
{t("Add people")}…
</Button>
</span>
)}
<br />
</>
) : (
<Text as="p" type="secondary">
<Trans
defaults="Listing members of the <em>{{groupName}}</em> group."
values={{
groupName: group.name,
}}
components={{
em: <strong />,
}}
/>
</Text>
)}
<PaginatedList<User>
items={users.inGroup(group.id)}
fetch={groupUsers.fetchPage}
options={{
id: group.id,
}}
empty={<Empty>{t("This group has no members.")}</Empty>}
renderItem={(user) => (
<GroupMemberListItem
key={user.id}
user={user}
group={group}
groupUser={groupUsers.orderedData.find(
(gu) => gu.userId === user.id && gu.groupId === group.id
)}
onRemove={can.update ? () => handleRemoveUser(user) : undefined}
/>
)}
/>
</Flex>
);
});
const AddPeopleToGroupDialog = observer(function ({
group,
}: Pick<Props, "group">) {
const { dialogs, users, groupUsers } = useStores();
const { t } = useTranslation();
const team = useCurrentTeam();
const can = usePolicy(team);
const [query, setQuery] = React.useState("");
const debouncedFetch = React.useMemo(
() => debounce((q) => users.fetchPage({ query: q }), 250),
[users]
);
const handleFilter = React.useCallback(
(ev: React.ChangeEvent<HTMLInputElement>) => {
const updatedQuery = ev.target.value;
setQuery(updatedQuery);
void debouncedFetch(updatedQuery);
},
[debouncedFetch]
);
const handleAddUser = React.useCallback(
async (user: User) => {
try {
await groupUsers.create({
groupId: group.id,
userId: user.id,
});
toast.success(
t(`{{userName}} was added to the group`, {
userName: user.name,
}),
{
icon: <Avatar model={user} size={AvatarSize.Toast} />,
}
);
} catch (_err) {
toast.error(t("Could not add user"));
}
},
[t, groupUsers, group.id]
);
const handleInvitePeople = React.useCallback(() => {
dialogs.openModal({
title: t("Invite people"),
content: <Invite onSubmit={dialogs.closeAllModals} />,
replace: true,
});
}, [t, dialogs]);
const { loading } = useRequest(
React.useCallback(
() => groupUsers.fetchAll({ id: group.id }),
[groupUsers, group]
),
true
);
return (
<Flex column>
<Text as="p" type="secondary">
{t(
"Add members below to give them access to the group. Need to add someone who’s not yet a member?"
)}{" "}
{can.inviteUser ? (
<ButtonLink onClick={handleInvitePeople}>
{t("Invite them to {{teamName}}", {
teamName: team.name,
})}
</ButtonLink>
) : (
t("Ask an admin to invite them first")
)}
.
</Text>
<Input
type="search"
placeholder={`${t("Search by name")}…`}
value={query}
onChange={handleFilter}
label={t("Search people")}
labelHidden
autoFocus
flex
/>
{loading ? (
<DelayedMount>
<PlaceholderList count={5} />
</DelayedMount>
) : (
<PaginatedList<User>
empty={
query ? (
<Empty>{t("No people matching your search")}</Empty>
) : (
<Empty>{t("No people left to add")}</Empty>
)
}
items={users.notInGroup(group.id, query)}
fetch={query ? undefined : users.fetchPage}
renderItem={(item) => (
<GroupMemberListItem
key={item.id}
user={item}
group={group}
groupUser={undefined}
onAdd={() => handleAddUser(item)}
/>
)}
/>
)}
</Flex>
);
});
type GroupMemberListItemProps = {
user: User;
group: Group;
groupUser: GroupUser | undefined;
onAdd?: () => Promise<void>;
onRemove?: () => Promise<void>;
};
const GroupMemberListItem = observer(function ({
user,
group,
groupUser,
onAdd,
}: GroupMemberListItemProps) {
const { t } = useTranslation();
const { groupUsers } = useStores();
const can = usePolicy(group);
const permissions = React.useMemo(
() =>
[
{
label: t("Manage"),
value: GroupPermission.Admin,
},
{
label: t("Member"),
value: GroupPermission.Member,
},
{
divider: true,
label: t("Remove"),
value: EmptySelectValue,
},
] as Permission[],
[t]
);
return (
<ListItem
title={user.name}
subtitle={
<>
{user.lastActiveAt ? (
<Trans>
Active <Time dateTime={user.lastActiveAt} /> ago
</Trans>
) : (
t("Never signed in")
)}
{user.isInvited && <Badge>{t("Invited")}</Badge>}
{user.isAdmin && <Badge primary={user.isAdmin}>{t("Admin")}</Badge>}
</>
}
image={<Avatar model={user} size={AvatarSize.Large} />}
actions={
<Flex align="center">
{onAdd ? (
<Button onClick={onAdd} neutral>
{t("Add")}
</Button>
) : (
<div style={{ marginRight: -8 }}>
<InputMemberPermissionSelect
permissions={permissions}
onChange={async (
permission: GroupPermission | typeof EmptySelectValue
) => {
try {
if (permission === EmptySelectValue) {
await groupUsers.delete({
userId: user.id,
groupId: group.id,
});
} else {
await groupUsers.update({
userId: user.id,
groupId: group.id,
permission,
});
}
} catch (err) {
toast.error(err.message);
return false;
}
return true;
}}
disabled={!can.update}
value={groupUser?.permission}
/>
</div>
)}
</Flex>
}
/>
);
});