[ui] Group users edit (#13362)
* [UI] group user controls * Add documentation on user configuration via admin center
No files matched your search
|
After Width: | Height: | Size: 193 KiB |
|
After Width: | Height: | Size: 300 KiB |
|
After Width: | Height: | Size: 249 KiB |
|
After Width: | Height: | Size: 228 KiB |
|
After Width: | Height: | Size: 208 KiB |
|
After Width: | Height: | Size: 243 KiB |
|
After Width: | Height: | Size: 322 KiB |
@@ -26,7 +26,7 @@ InvenTree provides multiple administration interfaces with different safety leve
|
||||
|
||||
The Admin Center is the main interface for managing InvenTree. It provides a user-friendly interface for managing all aspects of the system, including:
|
||||
|
||||
- Users / Groups
|
||||
- [Users / Groups](./permissions.md#managing-users-and-groups)
|
||||
- Data import / export
|
||||
- Customisation (e.g. project codes, custom states, parameters and units)
|
||||
- Operational controls (e.g. background tasks, errors, currencies, identity federation controls)
|
||||
|
||||
@@ -53,6 +53,70 @@ It is strongly recommended to register any users with staff / superuser flags wi
|
||||
|
||||
Practicing account tiering is strongly recommended.
|
||||
|
||||
## Managing Users and Groups
|
||||
|
||||
Users and groups are managed from the *Users / Access* panel of the [Admin Center](./admin.md#admin-center).
|
||||
|
||||
!!! info "Required Permissions"
|
||||
Creating and editing users and groups requires the *staff* flag **and** the relevant permissions for the *Admin* role. Users without these permissions can view the users and groups, but cannot make any changes.
|
||||
|
||||
### Users
|
||||
|
||||
The *Users* table lists all user accounts, along with the number of groups each user belongs to and their *Administrator*, *Superuser* and *Active* flags. Use the {{ icon("plus", color="green") }} button to create a new user.
|
||||
|
||||
{{ image("settings/admin_users_table.png", "Users table") }}
|
||||
|
||||
Click on a user to open the *Edit User* drawer.
|
||||
|
||||
#### User Details
|
||||
|
||||
The *User Details* section allows the user's name and account flags to be edited.
|
||||
|
||||
{{ image("settings/admin_edit_user.png", "Edit user details") }}
|
||||
|
||||
!!! tip "Deactivate Users"
|
||||
Rather than deleting a user account, consider unchecking the *Is Active* flag. This prevents the user from logging in, while retaining the history of their actions.
|
||||
|
||||
#### User Groups
|
||||
|
||||
The *User Groups* section shows which groups the user belongs to. Move groups between the *Available* and *Selected* lists, then press *Save* to update the user's group memberships.
|
||||
|
||||
{{ image("settings/admin_user_groups.png", "Edit user groups") }}
|
||||
|
||||
### Groups
|
||||
|
||||
The *Groups* table lists all user groups. Use the {{ icon("plus", color="green") }} button to create a new group.
|
||||
|
||||
{{ image("settings/admin_groups_table.png", "Groups table") }}
|
||||
|
||||
Click on a group to open the *Edit Group* drawer.
|
||||
|
||||
#### Group Details
|
||||
|
||||
The *Group Details* section allows the name of the group to be changed.
|
||||
|
||||
{{ image("settings/admin_edit_group.png", "Edit group details") }}
|
||||
|
||||
#### Group Roles
|
||||
|
||||
The *Group Roles* section sets the [role permissions](#role-permissions) for every user in the group. Check the required *View*, *Change*, *Add* and *Delete* permissions for each role, then press *Save*.
|
||||
|
||||
{{ image("settings/admin_group_roles.png", "Edit group roles") }}
|
||||
|
||||
#### Group Users
|
||||
|
||||
The *Group Users* section lists the users who belong to the group. From here, users can be quickly added to or removed from the group:
|
||||
|
||||
- **Add a user** - search for the user in the *Add User* field and select them. The user is added to the group immediately.
|
||||
- **Remove a user** - press the {{ icon("user-minus", color="red") }} button next to the user.
|
||||
|
||||
These changes only affect membership of this group - the user's other group memberships are not changed.
|
||||
|
||||
{{ image("settings/admin_group_users.png", "Edit group users") }}
|
||||
|
||||
!!! info "Permissions"
|
||||
Adding or removing users from a group edits the *user* account, so it requires *change* permission for users (as well as the *staff* flag). If you do not have this permission, the list of users is displayed in read-only mode.
|
||||
|
||||
|
||||
## Database Admin Permissions
|
||||
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
import { t } from '@lingui/core/macro';
|
||||
import { Trans } from '@lingui/react/macro';
|
||||
import { Accordion, LoadingOverlay, Stack, Text } from '@mantine/core';
|
||||
import {
|
||||
Accordion,
|
||||
Group,
|
||||
LoadingOverlay,
|
||||
Stack,
|
||||
Table,
|
||||
Text
|
||||
} from '@mantine/core';
|
||||
import { showNotification } from '@mantine/notifications';
|
||||
import { useCallback, useMemo, useState } from 'react';
|
||||
|
||||
import { ActionButton } from '@lib/components/ActionButton';
|
||||
import { AddItemButton } from '@lib/components/AddItemButton';
|
||||
import {
|
||||
type RowAction,
|
||||
@@ -19,11 +28,14 @@ import { getDetailUrl } from '@lib/functions/Navigation';
|
||||
import useTable from '@lib/hooks/UseTable';
|
||||
import type { ApiFormModalProps } from '@lib/index';
|
||||
import type { TableColumn, TableState } from '@lib/types/Tables';
|
||||
import { IconUsersGroup } from '@tabler/icons-react';
|
||||
import { IconUserMinus, IconUsersGroup } from '@tabler/icons-react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { api } from '../../App';
|
||||
import { EditApiForm } from '../../components/forms/ApiForm';
|
||||
import { StandaloneField } from '../../components/forms/StandaloneField';
|
||||
import { RoleTable, type RuleSet } from '../../components/items/RoleTable';
|
||||
import { InvenTreeTable } from '../../components/tables/InvenTreeTable';
|
||||
import { showApiErrorMessage } from '../../functions/notifications';
|
||||
import {
|
||||
useCreateApiFormModal,
|
||||
useDeleteApiFormModal
|
||||
@@ -36,6 +48,176 @@ export interface GroupDetailI {
|
||||
name: string;
|
||||
}
|
||||
|
||||
interface GroupMemberI {
|
||||
pk: number;
|
||||
username: string;
|
||||
first_name?: string;
|
||||
last_name?: string;
|
||||
email?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display (and optionally edit) the users which belong to a group.
|
||||
*
|
||||
* Membership changes are made via the existing user detail endpoint
|
||||
* (PATCH /api/user/<pk>/ with 'group_ids'), so the same permission checks
|
||||
* apply as when editing a user's groups from the "Edit User" drawer.
|
||||
*/
|
||||
function GroupUsers({
|
||||
groupId,
|
||||
users,
|
||||
onChange
|
||||
}: Readonly<{
|
||||
groupId: number;
|
||||
users: GroupMemberI[];
|
||||
onChange: () => void;
|
||||
}>) {
|
||||
const user = useUserState();
|
||||
|
||||
// Membership is edited via the user endpoint, which requires staff + user change permission
|
||||
const canEdit: boolean = useMemo(
|
||||
() => user.isStaff() && user.hasChangePermission(ModelType.user),
|
||||
[user]
|
||||
);
|
||||
|
||||
const [busy, setBusy] = useState<boolean>(false);
|
||||
|
||||
// Used to reset the "add user" field after a selection is made
|
||||
const [fieldKey, setFieldKey] = useState<number>(0);
|
||||
|
||||
const sortedUsers = useMemo(
|
||||
() =>
|
||||
[...(users ?? [])].sort((a, b) => a.username.localeCompare(b.username)),
|
||||
[users]
|
||||
);
|
||||
|
||||
// Fetch the user's current groups, apply the modification, and save
|
||||
const updateMembership = useCallback(
|
||||
async (userId: number, add: boolean) => {
|
||||
if (!canEdit || busy) return;
|
||||
|
||||
setBusy(true);
|
||||
|
||||
try {
|
||||
const response = await api.get(apiUrl(ApiEndpoints.user_list, userId));
|
||||
const current: number[] = (response.data?.groups ?? []).map(
|
||||
(g: any) => g.pk
|
||||
);
|
||||
|
||||
if (add === current.includes(groupId)) {
|
||||
// Nothing to do (already a member / already removed)
|
||||
return;
|
||||
}
|
||||
|
||||
const group_ids = add
|
||||
? [...current, groupId]
|
||||
: current.filter((pk) => pk !== groupId);
|
||||
|
||||
await api.patch(apiUrl(ApiEndpoints.user_list, userId), {
|
||||
group_ids
|
||||
});
|
||||
|
||||
showNotification({
|
||||
title: add ? t`User added` : t`User removed`,
|
||||
message: add ? t`User added to group` : t`User removed from group`,
|
||||
color: 'green'
|
||||
});
|
||||
} catch (error) {
|
||||
showApiErrorMessage({
|
||||
error: error,
|
||||
title: t`Error updating group membership`
|
||||
});
|
||||
} finally {
|
||||
setBusy(false);
|
||||
onChange();
|
||||
}
|
||||
},
|
||||
[canEdit, busy, groupId, onChange]
|
||||
);
|
||||
|
||||
const memberIds: number[] = useMemo(
|
||||
() => sortedUsers.map((u) => u.pk),
|
||||
[sortedUsers]
|
||||
);
|
||||
|
||||
return (
|
||||
<Stack gap='xs'>
|
||||
<LoadingOverlay visible={busy} />
|
||||
{canEdit && (
|
||||
<StandaloneField
|
||||
key={`group-add-user-${fieldKey}`}
|
||||
fieldName='user'
|
||||
fieldDefinition={{
|
||||
field_type: 'related field',
|
||||
label: t`Add User`,
|
||||
description: t`Select a user to add to this group`,
|
||||
api_url: apiUrl(ApiEndpoints.user_list),
|
||||
model: ModelType.user,
|
||||
required: false,
|
||||
adjustFilters: (value) => ({ ...value.filters, is_active: true }),
|
||||
onValueChange: (value: any) => {
|
||||
if (!value) return;
|
||||
const pk = Number(value);
|
||||
setFieldKey((k) => k + 1);
|
||||
if (!memberIds.includes(pk)) {
|
||||
updateMembership(pk, true);
|
||||
}
|
||||
}
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{sortedUsers.length === 0 ? (
|
||||
<Text c='dimmed' fs='italic'>
|
||||
<Trans>No users in this group</Trans>
|
||||
</Text>
|
||||
) : (
|
||||
<Table striped>
|
||||
<Table.Thead>
|
||||
<Table.Tr>
|
||||
<Table.Th>
|
||||
<Trans>Username</Trans>
|
||||
</Table.Th>
|
||||
<Table.Th>
|
||||
<Trans>Name</Trans>
|
||||
</Table.Th>
|
||||
<Table.Th>
|
||||
<Trans>Email</Trans>
|
||||
</Table.Th>
|
||||
{canEdit && <Table.Th />}
|
||||
</Table.Tr>
|
||||
</Table.Thead>
|
||||
<Table.Tbody>
|
||||
{sortedUsers.map((member) => (
|
||||
<Table.Tr key={`group-user-${member.pk}`}>
|
||||
<Table.Td>{member.username}</Table.Td>
|
||||
<Table.Td>
|
||||
{[member.first_name, member.last_name]
|
||||
.filter(Boolean)
|
||||
.join(' ')}
|
||||
</Table.Td>
|
||||
<Table.Td>{member.email}</Table.Td>
|
||||
{canEdit && (
|
||||
<Table.Td>
|
||||
<Group justify='right'>
|
||||
<ActionButton
|
||||
icon={<IconUserMinus />}
|
||||
color='red'
|
||||
tooltip={t`Remove user from group`}
|
||||
disabled={busy}
|
||||
onClick={() => updateMembership(member.pk, false)}
|
||||
/>
|
||||
</Group>
|
||||
</Table.Td>
|
||||
)}
|
||||
</Table.Tr>
|
||||
))}
|
||||
</Table.Tbody>
|
||||
</Table>
|
||||
)}
|
||||
</Stack>
|
||||
);
|
||||
}
|
||||
|
||||
export function GroupDrawer({
|
||||
id,
|
||||
refreshTable
|
||||
@@ -61,7 +243,9 @@ export function GroupDrawer({
|
||||
return instance?.roles ?? [];
|
||||
}, [instance]);
|
||||
|
||||
if (isFetching) {
|
||||
// Only block the drawer on the initial load, so that refreshing the group
|
||||
// (e.g. after a membership change) does not collapse the open panel
|
||||
if (isFetching && !instance?.pk) {
|
||||
return <LoadingOverlay visible={true} />;
|
||||
}
|
||||
|
||||
@@ -117,6 +301,24 @@ export function GroupDrawer({
|
||||
<RoleTable roles={groupRoles} editable />
|
||||
</Accordion.Panel>
|
||||
</Accordion.Item>
|
||||
|
||||
<Accordion.Item key='users' value='users'>
|
||||
<Accordion.Control>
|
||||
<StylishText size='lg'>
|
||||
<Trans>Group Users</Trans>
|
||||
</StylishText>
|
||||
</Accordion.Control>
|
||||
<Accordion.Panel>
|
||||
<GroupUsers
|
||||
groupId={Number(id)}
|
||||
users={instance?.users ?? []}
|
||||
onChange={() => {
|
||||
refreshInstance();
|
||||
refreshTable();
|
||||
}}
|
||||
/>
|
||||
</Accordion.Panel>
|
||||
</Accordion.Item>
|
||||
</Accordion>
|
||||
</Stack>
|
||||
);
|
||||
|
||||
@@ -106,6 +106,32 @@ test('Admin - Users and Groups', async ({ browser }) => {
|
||||
await page.keyboard.press('Escape');
|
||||
await page.waitForTimeout(250);
|
||||
|
||||
// Manage the group membership from the "Edit Group" drawer
|
||||
await page.getByRole('cell', { name: groupName, exact: true }).click();
|
||||
await page.getByText('Edit Group').first().waitFor();
|
||||
await openAccordion(page, 'Group Users');
|
||||
|
||||
const groupUsers = page.getByRole('region', { name: 'Group Users' });
|
||||
const memberRow = groupUsers.getByRole('row', {
|
||||
name: new RegExp(userName)
|
||||
});
|
||||
await memberRow.waitFor();
|
||||
|
||||
// Remove the user from the group
|
||||
await memberRow.getByLabel('action-button-remove-user-from-group').click();
|
||||
await page.getByText('User removed from group').first().waitFor();
|
||||
await expect(memberRow).toHaveCount(0);
|
||||
await groupUsers.getByText('No users in this group').waitFor();
|
||||
|
||||
// Add the user back into the group
|
||||
await groupUsers.getByLabel('related-field-user').fill(userName);
|
||||
await page.getByRole('option', { name: new RegExp(userName) }).click();
|
||||
await page.getByText('User added to group').first().waitFor();
|
||||
await memberRow.waitFor();
|
||||
|
||||
await page.keyboard.press('Escape');
|
||||
await page.waitForTimeout(250);
|
||||
|
||||
// Lock and unlock the user via the row actions
|
||||
const userCell = page.getByRole('cell', { name: userName, exact: true });
|
||||
await clickOnRowMenu(userCell);
|
||||
|
||||