This commit is contained in:
Timothy Jaeryang Baek 2026-10-10 20:29:50 +04:00
parent a6b1c0b357
commit d04f78b3bf
3 changed files with 25 additions and 5252 deletions

View file

@ -554,6 +554,24 @@ class ChatTable:
history['currentId'] = latest_leaf_id
return True
async def require_chat_creation_permission(self, user_id: str, db: AsyncSession | None = None) -> None:
from fastapi import HTTPException
from open_webui.constants import ERROR_MESSAGES
from open_webui.models.config import Config
from open_webui.models.users import Users
from open_webui.utils.access_control import get_permissions
user = await Users.get_user_by_id(user_id, db=db)
if user and user.role == 'admin':
return
if not user:
raise HTTPException(status_code=403, detail=ERROR_MESSAGES.ACCESS_PROHIBITED)
permissions = await get_permissions(user_id, await Config.get('user.permissions'), db=db)
chat_permissions = permissions.get('chat', {})
if chat_permissions.get('temporary') and chat_permissions.get('temporary_enforced'):
raise HTTPException(status_code=403, detail=ERROR_MESSAGES.ACCESS_PROHIBITED)
async def insert_new_chat(
self,
id: str,
@ -564,6 +582,7 @@ class ChatTable:
internal_meta: dict | None = None,
timer_at: int | None = None,
) -> ChatModel | None:
await self.require_chat_creation_permission(user_id, db=db)
async with get_async_db_context(db) as session:
chat = ChatModel(
**{
@ -692,6 +711,7 @@ class ChatTable:
chat_import_forms: list[ChatImportForm],
db: AsyncSession | None = None,
) -> list[ChatModel]:
await self.require_chat_creation_permission(user_id, db=db)
async with get_async_db_context(db) as session:
from open_webui.utils.access_control.folders import has_folder_write_access

View file

@ -833,6 +833,8 @@ async def create_new_chat(
data={'title': chat.title, 'folder_id': chat.folder_id},
)
return ChatResponse.model_validate(chat, from_attributes=True)
except HTTPException:
raise
except Exception as e:
log.exception(e)
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT())
@ -862,6 +864,8 @@ async def import_chats(
data={'count': len(chats), 'chat_ids': [chat.id for chat in chats]},
)
return chats
except HTTPException:
raise
except Exception as e:
log.exception(e)
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT())

File diff suppressed because one or more lines are too long