-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.py
More file actions
54 lines (46 loc) · 1.71 KB
/
Copy pathhooks.py
File metadata and controls
54 lines (46 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from typing import Any, cast
from fastapi import FastAPI
from sqlalchemy import Select, event
from sqlalchemy.orm import ORMExecuteState, Session
from backend.common.context import ctx
from backend.plugin.ai.policy.registry import register_ai_resource_policy
from backend.plugin.ai_group.listener import (
AI_GROUP_RESOURCE_MODEL_CLASSES,
apply_ai_group_visibility_criteria,
get_statement_ai_group_resource_models,
)
from backend.plugin.ai_group.policy.resource import ai_group_resource_policy
@event.listens_for(Session, 'do_orm_execute', propagate=True)
def inject_ai_group_visibility_filter(orm_execute_state: ORMExecuteState) -> None:
"""
为 AI 资源查询自动追加分组可见性过滤条件
:param orm_execute_state: ORM 执行状态
:return:
"""
if not orm_execute_state.is_select or orm_execute_state.is_column_load:
return
statement = cast('Select[Any]', orm_execute_state.statement)
resource_models = {
mapper.class_ for mapper in orm_execute_state.all_mappers if mapper.class_ in AI_GROUP_RESOURCE_MODEL_CLASSES
}
if not resource_models:
resource_models = get_statement_ai_group_resource_models(statement)
if not resource_models:
return
user_id = ctx.user_id
is_superuser = ctx.is_superuser
if user_id is None or is_superuser:
return
orm_execute_state.statement = apply_ai_group_visibility_criteria(
statement,
user_id=user_id,
resource_models=resource_models,
is_superuser=is_superuser,
)
def setup(app: FastAPI) -> None:
"""
注册 AI 分组资源调用策略
:param app: FastAPI 应用
:return:
"""
register_ai_resource_policy(ai_group_resource_policy)