Skip to content

Commit 945275f

Browse files
committed
refac
1 parent 2e16592 commit 945275f

5 files changed

Lines changed: 56 additions & 6 deletions

File tree

backend/open_webui/models/access_grants.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ def has_public_read_access_grant(access_grants: Optional[list]) -> bool:
192192
return False
193193

194194

195+
def has_public_write_access_grant(access_grants: Optional[list]) -> bool:
196+
"""
197+
Returns True when a direct grant list includes wildcard public-write.
198+
"""
199+
for grant in normalize_access_grants(access_grants):
200+
if grant['principal_type'] == 'user' and grant['principal_id'] == '*' and grant['permission'] == 'write':
201+
return True
202+
return False
203+
204+
195205
def has_user_access_grant(access_grants: Optional[list]) -> bool:
196206
"""
197207
Returns True when a direct grant list includes any non-wildcard user grant.

backend/open_webui/routers/channels.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
ChannelWebhookModel,
3737
ChannelWebhookForm,
3838
)
39-
from open_webui.models.access_grants import AccessGrants, has_public_read_access_grant
39+
from open_webui.models.access_grants import AccessGrants, has_public_read_access_grant, has_public_write_access_grant
4040
from open_webui.models.messages import (
4141
Messages,
4242
MessageModel,
@@ -88,7 +88,7 @@ def channel_has_access(
8888
):
8989
return True
9090

91-
if not strict and permission == 'write' and has_public_read_access_grant(channel.access_grants):
91+
if not strict and permission == 'write' and has_public_write_access_grant(channel.access_grants):
9292
return True
9393

9494
return False

backend/open_webui/routers/notes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from open_webui.utils.access_control import (
3131
has_permission,
3232
has_public_read_access_grant,
33+
has_public_write_access_grant,
3334
filter_allowed_access_grants,
3435
)
3536
from open_webui.models.access_grants import AccessGrants
@@ -234,7 +235,7 @@ async def get_note_by_id(
234235
permission='write',
235236
db=db,
236237
)
237-
or has_public_read_access_grant(note.access_grants)
238+
or has_public_write_access_grant(note.access_grants)
238239
)
239240

240241
return NoteResponse(**note.model_dump(), write_access=write_access)

backend/open_webui/utils/access_control/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from open_webui.models.groups import Groups
66
from open_webui.models.access_grants import (
77
has_public_read_access_grant,
8+
has_public_write_access_grant,
89
has_user_access_grant,
910
strip_user_access_grants,
1011
)
@@ -225,7 +226,7 @@ def filter_allowed_access_grants(
225226
return access_grants
226227

227228
# Check if user can share publicly
228-
if has_public_read_access_grant(access_grants) and not has_permission(
229+
if (has_public_read_access_grant(access_grants) or has_public_write_access_grant(access_grants)) and not has_permission(
229230
user_id,
230231
public_permission_key,
231232
default_permissions,

src/lib/components/workspace/common/AccessControl.svelte

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import Plus from '$lib/components/icons/Plus.svelte';
1313
import AddAccessModal from './AddAccessModal.svelte';
1414
import Tooltip from '$lib/components/common/Tooltip.svelte';
15+
import Switch from '$lib/components/common/Switch.svelte';
1516
1617
type AccessGrant = {
1718
id?: string;
@@ -159,6 +160,12 @@
159160
grant.principal_type === 'user' && grant.principal_id === '*' && grant.permission === 'read'
160161
);
161162
163+
const hasPublicWriteGrant = (grants: AccessGrant[]): boolean =>
164+
grants.some(
165+
(grant) =>
166+
grant.principal_type === 'user' && grant.principal_id === '*' && grant.permission === 'write'
167+
);
168+
162169
const currentGrants = (): AccessGrant[] =>
163170
Array.isArray(accessGrants) ? (accessGrants as AccessGrant[]) : [];
164171
@@ -194,12 +201,12 @@
194201
};
195202
196203
const setPublic = (isPublic: boolean) => {
204+
// Remove all user:* grants
197205
const filtered = currentGrants().filter(
198206
(grant) =>
199207
!(
200208
grant.principal_type === 'user' &&
201-
grant.principal_id === '*' &&
202-
grant.permission === 'read'
209+
grant.principal_id === '*'
203210
)
204211
);
205212
if (isPublic) {
@@ -212,6 +219,23 @@
212219
commitAccessGrants(filtered);
213220
};
214221
222+
const togglePublicWrite = () => {
223+
let next = [...currentGrants()];
224+
if (hasPublicWriteGrant(next)) {
225+
next = next.filter(
226+
(grant) =>
227+
!(
228+
grant.principal_type === 'user' &&
229+
grant.principal_id === '*' &&
230+
grant.permission === 'write'
231+
)
232+
);
233+
} else {
234+
next = upsertPrincipalGrant('user', '*', 'write', next);
235+
}
236+
commitAccessGrants(next);
237+
};
238+
215239
const upsertPrincipalGrant = (
216240
principalType: 'user' | 'group',
217241
principalId: string,
@@ -491,6 +515,20 @@
491515
</div>
492516
</div>
493517
</div>
518+
519+
{#if hasPublicReadGrant(accessGrants ?? []) && accessRoles.includes('write')}
520+
<div class="flex w-full justify-between mt-2 ml-0.5">
521+
<div class="self-center text-xs">
522+
{$i18n.t('Allow everyone to edit')}
523+
</div>
524+
<Switch
525+
state={hasPublicWriteGrant(accessGrants ?? [])}
526+
on:change={() => {
527+
togglePublicWrite();
528+
}}
529+
/>
530+
</div>
531+
{/if}
494532
</div>
495533

496534
{#if share}

0 commit comments

Comments
 (0)