1313from apps .db .constant import DB
1414from apps .swagger .i18n import PLACEHOLDER_PREFIX
1515from apps .system .crud .assistant import AssistantOutDs , AssistantOutDsFactory , get_assistant_info
16- from apps .system .crud .assistant_manage import dynamic_upgrade_cors , save
16+ from apps .system .crud .assistant_manage import dynamic_upgrade_cors , get_ws_assistant , save
1717from apps .system .models .system_model import AssistantModel
1818from apps .system .schemas .auth import CacheName , CacheNamespace
1919from apps .system .schemas .permission import SqlbotPermission , require_permissions
@@ -177,13 +177,11 @@ def iterfile():
177177@router .patch ('/ui' , summary = f"{ PLACEHOLDER_PREFIX } assistant_ui_api" , description = f"{ PLACEHOLDER_PREFIX } assistant_ui_api" )
178178@require_permissions (permission = SqlbotPermission (role = ['ws_admin' ]))
179179@system_log (LogConfig (operation_type = OperationType .UPDATE , module = OperationModules .APPLICATION , result_id_expr = "id" ))
180- async def ui (session : SessionDep , data : str = Form (), files : List [UploadFile ] = []):
180+ async def ui (session : SessionDep , current_user : CurrentUser , trans : Trans , data : str = Form (), files : List [UploadFile ] = []):
181181 json_data = json .loads (data )
182182 uiSchema = AssistantUiSchema (** json_data )
183183 id = uiSchema .id
184- db_model = session .get (AssistantModel , id )
185- if not db_model :
186- raise ValueError (f"AssistantModel with id { id } not found" )
184+ db_model = get_ws_assistant (session , id , current_user , trans )
187185 configuration = db_model .configuration
188186 config_obj = json .loads (configuration ) if configuration else {}
189187
@@ -332,12 +330,15 @@ async def add(request: Request, session: SessionDep, current_user: CurrentUser,
332330@require_permissions (permission = SqlbotPermission (role = ['ws_admin' ]))
333331@clear_cache (namespace = CacheNamespace .EMBEDDED_INFO , cacheName = CacheName .ASSISTANT_INFO , keyExpression = "editor.id" )
334332@system_log (LogConfig (operation_type = OperationType .UPDATE , module = OperationModules .APPLICATION , resource_id_expr = "editor.id" ))
335- async def update (request : Request , session : SessionDep , editor : AssistantDTO ):
333+ async def update (request : Request , session : SessionDep , current_user : CurrentUser , trans : Trans , editor : AssistantDTO ):
336334 id = editor .id
337- db_model = session .get (AssistantModel , id )
338- if not db_model :
339- raise ValueError (f"AssistantModel with id { id } not found" )
335+ db_model = get_ws_assistant (session , id , current_user , trans )
340336 update_data = AssistantModel .model_validate (editor )
337+ if not current_user .isAdmin :
338+ # 忽略请求体 oid(含默认值 1):非 admin 不允许跨工作空间移动/接管
339+ update_data .oid = db_model .oid
340+ # create_time 不在请求体中,model_validate 会填默认值 0,更新时保留原值
341+ update_data .create_time = db_model .create_time
341342 db_model .sqlmodel_update (update_data )
342343 session .add (db_model )
343344 session .commit ()
@@ -357,10 +358,8 @@ async def get_one(session: SessionDep, id: int = Path(description="ID")):
357358@require_permissions (permission = SqlbotPermission (role = ['ws_admin' ]))
358359@clear_cache (namespace = CacheNamespace .EMBEDDED_INFO , cacheName = CacheName .ASSISTANT_INFO , keyExpression = "id" )
359360@system_log (LogConfig (operation_type = OperationType .DELETE , module = OperationModules .APPLICATION , resource_id_expr = "id" ))
360- async def delete (request : Request , session : SessionDep , id : int = Path (description = "ID" )):
361- db_model = session .get (AssistantModel , id )
362- if not db_model :
363- raise ValueError (f"AssistantModel with id { id } not found" )
361+ async def delete (request : Request , session : SessionDep , current_user : CurrentUser , trans : Trans , id : int = Path (description = "ID" )):
362+ db_model = get_ws_assistant (session , id , current_user , trans )
364363 session .delete (db_model )
365364 session .commit ()
366365 dynamic_upgrade_cors (request = request , session = session )
0 commit comments