From edf3ae920989b01383be543e7379d3eade03c0b6 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 17 Jun 2026 00:33:30 +0200 Subject: [PATCH] refac --- backend/open_webui/config.py | 3 ++ backend/open_webui/routers/chats.py | 9 ++++++ backend/open_webui/routers/users.py | 7 +++-- .../admin/Users/Groups/Permissions.svelte | 16 +++++++++++ .../chat/Settings/DataControls.svelte | 28 ++++++++++--------- src/lib/constants/permissions.ts | 1 + 6 files changed, 49 insertions(+), 15 deletions(-) diff --git a/backend/open_webui/config.py b/backend/open_webui/config.py index d8d7b9032e..e3a90dbcf5 100644 --- a/backend/open_webui/config.py +++ b/backend/open_webui/config.py @@ -2768,6 +2768,8 @@ USER_PERMISSIONS_CHAT_ALLOW_PUBLIC_SHARING = ( USER_PERMISSIONS_CHAT_EXPORT = os.getenv('USER_PERMISSIONS_CHAT_EXPORT', 'True').lower() == 'true' +USER_PERMISSIONS_CHAT_IMPORT = os.getenv('USER_PERMISSIONS_CHAT_IMPORT', 'True').lower() == 'true' + USER_PERMISSIONS_CHAT_STT = os.getenv('USER_PERMISSIONS_CHAT_STT', 'True').lower() == 'true' USER_PERMISSIONS_CHAT_TTS = os.getenv('USER_PERMISSIONS_CHAT_TTS', 'True').lower() == 'true' @@ -2864,6 +2866,7 @@ DEFAULT_USER_PERMISSIONS = { 'edit': USER_PERMISSIONS_CHAT_EDIT, 'share': USER_PERMISSIONS_CHAT_SHARE, 'export': USER_PERMISSIONS_CHAT_EXPORT, + 'import': USER_PERMISSIONS_CHAT_IMPORT, '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 0ece8ec410..5017f821e2 100644 --- a/backend/open_webui/routers/chats.py +++ b/backend/open_webui/routers/chats.py @@ -585,10 +585,19 @@ async def create_new_chat( @router.post('/import', response_model=list[ChatResponse]) async def import_chats( + request: Request, form_data: ChatsImportForm, user=Depends(get_verified_user), db: AsyncSession = Depends(get_async_session), ): + if user.role != 'admin' and not await has_permission( + user.id, 'chat.import', request.app.state.config.USER_PERMISSIONS, db=db + ): + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, + detail=ERROR_MESSAGES.ACCESS_PROHIBITED, + ) + try: chats = await Chats.import_chats(user.id, form_data.chats, db=db) return chats diff --git a/backend/open_webui/routers/users.py b/backend/open_webui/routers/users.py index 158d6e8e15..ef1c989773 100644 --- a/backend/open_webui/routers/users.py +++ b/backend/open_webui/routers/users.py @@ -38,7 +38,7 @@ from open_webui.utils.auth import ( get_verified_user, validate_password, ) -from pydantic import BaseModel, ConfigDict +from pydantic import BaseModel, ConfigDict, Field from sqlalchemy.ext.asyncio import AsyncSession log = logging.getLogger(__name__) @@ -201,6 +201,8 @@ class AccessGrantsPermissions(BaseModel): class ChatPermissions(BaseModel): + model_config = ConfigDict(populate_by_name=True) + controls: bool = True valves: bool = True system_prompt: bool = True @@ -215,6 +217,7 @@ class ChatPermissions(BaseModel): edit: bool = True share: bool = True export: bool = True + import_: bool = Field(default=True, alias='import') stt: bool = True tts: bool = True call: bool = True @@ -265,7 +268,7 @@ async def get_default_user_permissions(request: Request, user=Depends(get_admin_ @router.post('/default/permissions') async def update_default_user_permissions(request: Request, form_data: UserPermissions, user=Depends(get_admin_user)): - request.app.state.config.USER_PERMISSIONS = form_data.model_dump() + request.app.state.config.USER_PERMISSIONS = form_data.model_dump(by_alias=True) return request.app.state.config.USER_PERMISSIONS diff --git a/src/lib/components/admin/Users/Groups/Permissions.svelte b/src/lib/components/admin/Users/Groups/Permissions.svelte index d0845d398c..bb07931efd 100644 --- a/src/lib/components/admin/Users/Groups/Permissions.svelte +++ b/src/lib/components/admin/Users/Groups/Permissions.svelte @@ -700,6 +700,22 @@ {/if} +
+
+
+ {$i18n.t('Allow Chat Import')} +
+ +
+ {#if defaultPermissions?.chat?.import && !permissions.chat['import']} +
+
+ {$i18n.t('This is a default user permission and will remain enabled.')} +
+
+ {/if} +
+
diff --git a/src/lib/components/chat/Settings/DataControls.svelte b/src/lib/components/chat/Settings/DataControls.svelte index 2507191bd4..61e27db573 100644 --- a/src/lib/components/chat/Settings/DataControls.svelte +++ b/src/lib/components/chat/Settings/DataControls.svelte @@ -187,20 +187,22 @@
{$i18n.t('Chats')}
-
-
-
{$i18n.t('Import Chats')}
- + {#if $user?.role === 'admin' || ($user.permissions?.chat?.import ?? true)} +
+
+
{$i18n.t('Import Chats')}
+ +
-
+ {/if} {#if $user?.role === 'admin' || ($user.permissions?.chat?.export ?? true)}
diff --git a/src/lib/constants/permissions.ts b/src/lib/constants/permissions.ts index a925938a1a..af52a04bf0 100644 --- a/src/lib/constants/permissions.ts +++ b/src/lib/constants/permissions.ts @@ -47,6 +47,7 @@ export const DEFAULT_PERMISSIONS = { edit: true, share: true, export: true, + import: true, stt: true, tts: true, call: true,