From 2f7b5acdf8814ebeebb9a3abf9a02035c9912c07 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 23 Apr 2025 14:43:33 +0900 Subject: [PATCH] enh: chat share & export permissions --- backend/open_webui/config.py | 10 ++++ backend/open_webui/routers/chats.py | 11 ++++- backend/open_webui/routers/users.py | 2 + src/lib/components/admin/Users/Groups.svelte | 2 + .../admin/Users/Groups/Permissions.svelte | 18 +++++++ src/lib/components/chat/Settings/Chats.svelte | 47 ++++++++++--------- src/lib/components/layout/Navbar/Menu.svelte | 23 +++++---- .../components/layout/Sidebar/ChatMenu.svelte | 41 +++++++++------- 8 files changed, 103 insertions(+), 51 deletions(-) diff --git a/backend/open_webui/config.py b/backend/open_webui/config.py index bd977bdc6..b597050dc 100644 --- a/backend/open_webui/config.py +++ b/backend/open_webui/config.py @@ -1068,6 +1068,14 @@ USER_PERMISSIONS_CHAT_EDIT = ( os.environ.get("USER_PERMISSIONS_CHAT_EDIT", "True").lower() == "true" ) +USER_PERMISSIONS_CHAT_SHARE = ( + os.environ.get("USER_PERMISSIONS_CHAT_SHARE", "True").lower() == "true" +) + +USER_PERMISSIONS_CHAT_EXPORT = ( + os.environ.get("USER_PERMISSIONS_CHAT_EXPORT", "True").lower() == "true" +) + USER_PERMISSIONS_CHAT_STT = ( os.environ.get("USER_PERMISSIONS_CHAT_STT", "True").lower() == "true" ) @@ -1132,6 +1140,8 @@ DEFAULT_USER_PERMISSIONS = { "file_upload": USER_PERMISSIONS_CHAT_FILE_UPLOAD, "delete": USER_PERMISSIONS_CHAT_DELETE, "edit": USER_PERMISSIONS_CHAT_EDIT, + "share": USER_PERMISSIONS_CHAT_SHARE, + "export": USER_PERMISSIONS_CHAT_EXPORT, "stt": USER_PERMISSIONS_CHAT_STT, "tts": USER_PERMISSIONS_CHAT_TTS, "call": USER_PERMISSIONS_CHAT_CALL, diff --git a/backend/open_webui/routers/chats.py b/backend/open_webui/routers/chats.py index 5fd44ab9f..6f00dd4d7 100644 --- a/backend/open_webui/routers/chats.py +++ b/backend/open_webui/routers/chats.py @@ -638,8 +638,17 @@ async def archive_chat_by_id(id: str, user=Depends(get_verified_user)): @router.post("/{id}/share", response_model=Optional[ChatResponse]) -async def share_chat_by_id(id: str, user=Depends(get_verified_user)): +async def share_chat_by_id(request: Request, id: str, user=Depends(get_verified_user)): + if not has_permission( + user.id, "chat.share", request.app.state.config.USER_PERMISSIONS + ): + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail=ERROR_MESSAGES.ACCESS_PROHIBITED, + ) + chat = Chats.get_chat_by_id_and_user_id(id, user.id) + if chat: if chat.share_id: shared_chat = Chats.update_shared_chat_by_chat_id(chat.id) diff --git a/backend/open_webui/routers/users.py b/backend/open_webui/routers/users.py index a6c2c90c4..29638199e 100644 --- a/backend/open_webui/routers/users.py +++ b/backend/open_webui/routers/users.py @@ -88,6 +88,8 @@ class ChatPermissions(BaseModel): file_upload: bool = True delete: bool = True edit: bool = True + share: bool = True + export: bool = True stt: bool = True tts: bool = True call: bool = True diff --git a/src/lib/components/admin/Users/Groups.svelte b/src/lib/components/admin/Users/Groups.svelte index dce8423e5..36c1014ba 100644 --- a/src/lib/components/admin/Users/Groups.svelte +++ b/src/lib/components/admin/Users/Groups.svelte @@ -63,6 +63,8 @@ file_upload: true, delete: true, edit: true, + share: true, + export: true, stt: true, tts: true, call: true, diff --git a/src/lib/components/admin/Users/Groups/Permissions.svelte b/src/lib/components/admin/Users/Groups/Permissions.svelte index c7a1308a5..9edf20ca0 100644 --- a/src/lib/components/admin/Users/Groups/Permissions.svelte +++ b/src/lib/components/admin/Users/Groups/Permissions.svelte @@ -24,6 +24,8 @@ file_upload: true, delete: true, edit: true, + share: true, + export: true, stt: true, tts: true, call: true, @@ -276,6 +278,22 @@ +
+
+ {$i18n.t('Allow Chat Share')} +
+ + +
+ +
+
+ {$i18n.t('Allow Chat Export')} +
+ + +
+
{$i18n.t('Allow Speech to Text')} diff --git a/src/lib/components/chat/Settings/Chats.svelte b/src/lib/components/chat/Settings/Chats.svelte index e7c424b05..8a0ada02d 100644 --- a/src/lib/components/chat/Settings/Chats.svelte +++ b/src/lib/components/chat/Settings/Chats.svelte @@ -140,28 +140,31 @@
{$i18n.t('Import Chats')}
- + + {#if $user?.role === 'admin' || ($user.permissions?.chat?.export ?? true)} + + {/if}

diff --git a/src/lib/components/layout/Navbar/Menu.svelte b/src/lib/components/layout/Navbar/Menu.svelte index 73468a197..d0bed0808 100644 --- a/src/lib/components/layout/Navbar/Menu.svelte +++ b/src/lib/components/layout/Navbar/Menu.svelte @@ -18,7 +18,8 @@ showArtifacts, mobile, temporaryChatEnabled, - theme + theme, + user } from '$lib/stores'; import { flyAndScale } from '$lib/utils/transitions'; @@ -212,7 +213,7 @@ {/if} - {#if !$temporaryChatEnabled} + {#if !$temporaryChatEnabled && ($user?.role === 'admin' || ($user.permissions?.chat?.share ?? true))} - { - downloadJSONExport(); - }} - > -
{$i18n.t('Export chat (.json)')}
-
+ {#if $user?.role === 'admin' || ($user.permissions?.chat?.export ?? true)} + { + downloadJSONExport(); + }} + > +
{$i18n.t('Export chat (.json)')}
+
+ {/if} { diff --git a/src/lib/components/layout/Sidebar/ChatMenu.svelte b/src/lib/components/layout/Sidebar/ChatMenu.svelte index 395b0ec3a..1f60e8bf3 100644 --- a/src/lib/components/layout/Sidebar/ChatMenu.svelte +++ b/src/lib/components/layout/Sidebar/ChatMenu.svelte @@ -26,7 +26,7 @@ getChatPinnedStatusById, toggleChatPinnedStatusById } from '$lib/apis/chats'; - import { chats, theme } from '$lib/stores'; + import { chats, theme, user } from '$lib/stores'; import { createMessagesList } from '$lib/utils'; import { downloadChatAsPDF } from '$lib/apis/utils'; import Download from '$lib/components/icons/Download.svelte'; @@ -233,15 +233,17 @@
{$i18n.t('Archive')}
- { - shareHandler(); - }} - > - -
{$i18n.t('Share')}
-
+ {#if $user?.role === 'admin' || ($user.permissions?.chat?.share ?? true)} + { + shareHandler(); + }} + > + +
{$i18n.t('Share')}
+
+ {/if} - { - downloadJSONExport(); - }} - > -
{$i18n.t('Export chat (.json)')}
-
+ {#if $user?.role === 'admin' || ($user.permissions?.chat?.export ?? true)} + { + downloadJSONExport(); + }} + > +
{$i18n.t('Export chat (.json)')}
+
+ {/if} + {