mirror of
https://github.com/open-webui/open-webui.git
synced 2026-10-11 03:38:02 +00:00
refac
This commit is contained in:
parent
a6b1c0b357
commit
d04f78b3bf
3 changed files with 25 additions and 5252 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
Loading…
Add table
Reference in a new issue