fix(access): exclude already granted groups from the Add Access modal

This commit is contained in:
silentoplayz 2026-08-07 08:34:39 -04:00
parent 8faaf2cd1e
commit 88548c1cae
2 changed files with 101 additions and 64 deletions

View file

@ -3,8 +3,9 @@
const i18n = getContext('i18n');
import { user as sessionUser } from '$lib/stores';
import { getGroups, getGroupById, getGroupInfoById } from '$lib/apis/groups';
import { getUserInfoById } from '$lib/apis/users';
import { getUserInfoById, searchUsers } from '$lib/apis/users';
import { WEBUI_API_BASE_URL } from '$lib/constants';
import XMark from '$lib/components/icons/XMark.svelte';
import Badge from '$lib/components/common/Badge.svelte';
@ -40,6 +41,7 @@
export let defaultPermission: 'read' | 'write' = 'read';
let groups: any[] = [];
let userTotal: number | null = null;
const resolvingGroupIds = new Set<string>();
let userById: Record<string, any> = {};
const resolvingUserIds = new Set<string>();
@ -428,6 +430,20 @@
.filter((group) => readGroupIds.includes(group.id) || writeGroupIds.includes(group.id))
.sort((a, b) => a.name.localeCompare(b.name));
$: selectableGroupCount = allowGroups
? groups.filter(
(group) => !readGroupIds.includes(group.id) && !writeGroupIds.includes(group.id)
).length
: 0;
$: selectableUserCount =
shareUsers && userTotal !== null
? Math.max(0, userTotal - 1 - selectedUserIds.filter((id) => id !== $sessionUser?.id).length)
: 0;
$: canAddAccess =
selectableGroupCount > 0 || (shareUsers && userTotal === null) || selectableUserCount > 0;
$: if (selectedUserIds.length > 0) {
void ensureUsersByIds(selectedUserIds);
}
@ -464,6 +480,14 @@
groups = [...groups, ...res].filter(
(g, index, self) => index === self.findIndex((t) => t.id === g.id)
);
if (shareUsers) {
const userRes = await searchUsers(localStorage.token, '', 'name', 'asc', 1).catch((error) => {
console.error(error);
return null;
});
userTotal = userRes?.total ?? null;
}
});
</script>
@ -571,16 +595,18 @@
{$i18n.t('Access List')}
</div>
<div class="flex gap-1">
<button
class="px-2 py-1 bg-transparent hover:bg-gray-100 dark:hover:bg-gray-800 rounded-lg transition text-xs font-normal flex items-center gap-1"
type="button"
on:click={() => {
showAddAccessModal = true;
}}
>
<Plus className="size-3" />
{$i18n.t('Add Access')}
</button>
{#if canAddAccess}
<button
class="px-2 py-1 bg-transparent hover:bg-gray-100 dark:hover:bg-gray-800 rounded-lg transition text-xs font-normal flex items-center gap-1"
type="button"
on:click={() => {
showAddAccessModal = true;
}}
>
<Plus className="size-3" />
{$i18n.t('Add Access')}
</button>
{/if}
</div>
</div>

View file

@ -31,9 +31,23 @@
let filteredGroups = [];
$: filteredGroups = groups
? groups.filter((group) => group.name.toLowerCase().includes(query.toLowerCase()))
? groups.filter(
(group) =>
group.name.toLowerCase().includes(query.toLowerCase()) &&
!accessGrants.some(
(grant) => grant.principal_type === 'group' && grant.principal_id === group.id
)
)
: [];
$: filteredUsers = (users ?? []).filter(
(user: any) =>
!accessGrants.some(
(grant) => grant.principal_type === 'user' && grant.principal_id === user.id
) &&
(includeSessionUser || user?.id !== $_user?.id)
);
let selectedGroup = {};
let selectedUsers = {};
@ -195,7 +209,7 @@
</div>
</div>
{#if users.length > 0 || filteredGroups.length > 0}
{#if filteredUsers.length > 0 || filteredGroups.length > 0}
<div class="scrollbar-hidden relative whitespace-nowrap w-full max-w-full">
<div class=" text-sm text-left text-gray-500 dark:text-gray-400 w-full max-w-full">
<div class="w-full max-h-96 overflow-y-auto rounded-lg">
@ -241,64 +255,61 @@
</div>
{/if}
{#if includeUsers}
{#if includeUsers && filteredUsers.length > 0}
<div class="text-xs text-gray-500 mb-1 mx-1">
{$i18n.t('Users')}
</div>
<div>
{#each users as user, userIdx (user.id)}
{#if !accessGrants.some((grant) => grant.principal_type === 'user' && grant.principal_id === user.id) && (includeSessionUser || user?.id !== $_user?.id)}
<button
class=" dark:border-gray-850 text-xs flex items-center justify-between w-full"
type="button"
on:click={() => {
if ((userIds ?? []).includes(user.id)) {
userIds = userIds.filter((id) => id !== user.id);
delete selectedUsers[user.id];
} else {
userIds = [...userIds, user.id];
selectedUsers[user.id] = user;
}
}}
>
<div class="px-3 py-1.5 font-normal text-gray-900 dark:text-white flex-1">
<div class="flex items-center gap-2">
<ProfilePreview {user} side="right" align="center" sideOffset={6}>
<img
class="rounded-2xl w-6 h-6 object-cover flex-shrink-0"
src={`${WEBUI_API_BASE_URL}/users/${user.id}/profile/image`}
alt="user"
/>
</ProfilePreview>
<Tooltip content={user.email} placement="top-start">
<div class="font-normal truncate">{user.name}</div>
</Tooltip>
{#if user?.is_active}
<div>
<span class="relative flex size-1.5">
<span
class="absolute inline-flex h-full w-full animate-ping rounded-full bg-green-400 opacity-75"
></span>
<span
class="relative inline-flex size-1.5 rounded-full bg-green-500"
></span>
</span>
</div>
{/if}
</div>
</div>
<div class="px-3 py-1">
<div class=" translate-y-0.5">
<Checkbox
state={(userIds ?? []).includes(user.id) ? 'checked' : 'unchecked'}
{#each filteredUsers as user (user.id)}
<button
class=" dark:border-gray-850 text-xs flex items-center justify-between w-full"
type="button"
on:click={() => {
if ((userIds ?? []).includes(user.id)) {
userIds = userIds.filter((id) => id !== user.id);
delete selectedUsers[user.id];
} else {
userIds = [...userIds, user.id];
selectedUsers[user.id] = user;
}
}}
>
<div class="px-3 py-1.5 font-normal text-gray-900 dark:text-white flex-1">
<div class="flex items-center gap-2">
<ProfilePreview {user} side="right" align="center" sideOffset={6}>
<img
class="rounded-2xl w-6 h-6 object-cover flex-shrink-0"
src={`${WEBUI_API_BASE_URL}/users/${user.id}/profile/image`}
alt="user"
/>
</div>
</ProfilePreview>
<Tooltip content={user.email} placement="top-start">
<div class="font-normal truncate">{user.name}</div>
</Tooltip>
{#if user?.is_active}
<div>
<span class="relative flex size-1.5">
<span
class="absolute inline-flex h-full w-full animate-ping rounded-full bg-green-400 opacity-75"
></span>
<span class="relative inline-flex size-1.5 rounded-full bg-green-500"
></span>
</span>
</div>
{/if}
</div>
</button>
{/if}
</div>
<div class="px-3 py-1">
<div class=" translate-y-0.5">
<Checkbox
state={(userIds ?? []).includes(user.id) ? 'checked' : 'unchecked'}
/>
</div>
</div>
</button>
{/each}
</div>
{/if}