From 49eca68e28f373b602ab1746b7b79f8bfad45ce4 Mon Sep 17 00:00:00 2001 From: tarmst Date: Fri, 10 Jan 2025 18:44:26 +0000 Subject: [PATCH] Add toggle to read/write perms on access control --- backend/open_webui/routers/knowledge.py | 4 +-- backend/open_webui/routers/prompts.py | 5 ++-- backend/open_webui/routers/tools.py | 3 ++- src/lib/components/workspace/Models.svelte | 8 +++++- .../workspace/common/AccessControl.svelte | 26 ++++++++++++++++++- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/backend/open_webui/routers/knowledge.py b/backend/open_webui/routers/knowledge.py index e58442c50..5f80b810e 100644 --- a/backend/open_webui/routers/knowledge.py +++ b/backend/open_webui/routers/knowledge.py @@ -213,8 +213,8 @@ async def update_knowledge_by_id( status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.NOT_FOUND, ) - - if knowledge.user_id != user.id and user.role != "admin": + # Is the user the original creator, in a group with write access, or an admin + if knowledge.user_id != user.id and not has_access(user.id, "write", knowledge.access_control) and user.role != "admin": raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.ACCESS_PROHIBITED, diff --git a/backend/open_webui/routers/prompts.py b/backend/open_webui/routers/prompts.py index 4f1c48482..37747bfbe 100644 --- a/backend/open_webui/routers/prompts.py +++ b/backend/open_webui/routers/prompts.py @@ -111,8 +111,9 @@ async def update_prompt_by_command( status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND, ) - - if prompt.user_id != user.id and user.role != "admin": + + # Is the user the original creator, in a group with write access, or an admin + if prompt.user_id != user.id and not has_access(user.id, "write", prompt.access_control) and user.role != "admin": raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.ACCESS_PROHIBITED, diff --git a/backend/open_webui/routers/tools.py b/backend/open_webui/routers/tools.py index 9e95ebe5a..36100e289 100644 --- a/backend/open_webui/routers/tools.py +++ b/backend/open_webui/routers/tools.py @@ -165,7 +165,8 @@ async def update_tools_by_id( detail=ERROR_MESSAGES.NOT_FOUND, ) - if tools.user_id != user.id and user.role != "admin": + # Is the user the original creator, in a group with write access, or an admin + if tools.user_id != user.id and not has_access(user.id, "write", tools.access_control) and user.role != "admin": raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.UNAUTHORIZED, diff --git a/src/lib/components/workspace/Models.svelte b/src/lib/components/workspace/Models.svelte index df19c0097..e574dde06 100644 --- a/src/lib/components/workspace/Models.svelte +++ b/src/lib/components/workspace/Models.svelte @@ -21,6 +21,7 @@ } from '$lib/apis/models'; import { getModels } from '$lib/apis'; + import { getGroups } from '$lib/apis/groups'; import EllipsisHorizontal from '../icons/EllipsisHorizontal.svelte'; import ModelMenu from './Models/ModelMenu.svelte'; @@ -47,6 +48,8 @@ let showModelDeleteConfirm = false; + let group_ids = []; + $: if (models) { filteredModels = models.filter( (m) => searchValue === '' || m.name.toLowerCase().includes(searchValue.toLowerCase()) @@ -151,6 +154,9 @@ onMount(async () => { models = await getWorkspaceModels(localStorage.token); + let groups = await getGroups(localStorage.token); + group_ids = groups.map(group => group.id); + loaded = true; @@ -308,7 +314,7 @@ {:else} - {#if $user?.role === 'admin' || model.user_id === $user?.id} + {#if $user?.role === 'admin' || model.user_id === $user?.id || model.access_control.write.group_ids.some(wg => group_ids.includes(wg))} {}; @@ -91,6 +92,9 @@ accessControl = { read: { group_ids: [] + }, + write: { + group_ids: [] } }; } @@ -110,7 +114,6 @@ - {#if accessControl !== null} {@const accessGroups = groups.filter((group) => accessControl.read.group_ids.includes(group.id) @@ -138,6 +141,27 @@
+ +