feat(db): 地磅、操作日志与工作流补全数据权限过滤
DB 列表/仪表盘 scoped 聚合,操作日志按 user_id 过滤,工作流实例详情与审批前校验 scope。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
2905dc3d5f
commit
66d5a8714b
8 files changed
+284
-92
No files matched your search
@@ -5,11 +5,12 @@ from fastapi import APIRouter, Depends, Query
|
||||
from pydantic import AliasChoices, BaseModel, ConfigDict, Field
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.data_scope import assert_row_in_data_scope
|
||||
from app.core.database import get_db
|
||||
from app.core.menu_guard import require_admin_or_super_admin
|
||||
from app.core.response import success_response, error_response
|
||||
from app.crud import workflow as wf_crud
|
||||
from app.models.models import SysUser, WfNode, WfProcessDef, WfProcessVersion, WfTask
|
||||
from app.models.models import SysUser, WfNode, WfProcessDef, WfProcessInstance, WfProcessVersion, WfTask
|
||||
from app.services.workflow_engine import WorkflowEngine
|
||||
|
||||
router = APIRouter(prefix="/web/admin/workflow", tags=["工作流管理"])
|
||||
@@ -29,6 +30,30 @@ def _business_type_label(code: Optional[str]) -> str:
|
||||
return _BUSINESS_TYPE_LABELS.get(code, code)
|
||||
|
||||
|
||||
def _portal_user_info(user: dict) -> dict:
|
||||
dept_id = (user.get("region_id") or user.get("dept_id") or "").strip()
|
||||
return {
|
||||
"user_id": (user.get("user_id") or user.get("id") or "").strip(),
|
||||
"role": (user.get("role") or "").strip(),
|
||||
"tenant_no": (user.get("tenant_no") or "").strip(),
|
||||
"dept_id": dept_id,
|
||||
"region_id": dept_id,
|
||||
}
|
||||
|
||||
|
||||
def _assert_wf_instance_in_scope(
|
||||
db: Session,
|
||||
instance: WfProcessInstance,
|
||||
user: dict,
|
||||
*,
|
||||
skip_scope: bool = False,
|
||||
) -> None:
|
||||
if skip_scope:
|
||||
return
|
||||
tenant_no = user.get("tenant_no", "shanxia2.0")
|
||||
assert_row_in_data_scope(db, instance, WfProcessInstance, _portal_user_info(user), tenant_no)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 业务表单字段提供者注册表
|
||||
# 新增业务类型时,只需添加一个 provider 函数并注册即可,前端无需改动
|
||||
@@ -357,12 +382,13 @@ async def list_my_instances(
|
||||
@router.get("/instances/{instance_id}")
|
||||
async def get_instance_detail(
|
||||
instance_id: str,
|
||||
_user: dict = Depends(require_admin_or_super_admin),
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
instance = wf_crud.get_instance(db, instance_id)
|
||||
if not instance:
|
||||
return error_response(404, "流程实例不存在")
|
||||
_assert_wf_instance_in_scope(db, instance, user)
|
||||
tasks = db.query(WfTask).filter(
|
||||
WfTask.instance_id == instance_id,
|
||||
WfTask.del_flag == 0,
|
||||
@@ -466,6 +492,15 @@ async def approve_task(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
task = wf_crud.get_task(db, task_id)
|
||||
if not task:
|
||||
return error_response(404, "任务不存在")
|
||||
instance = wf_crud.get_instance(db, task.instance_id)
|
||||
if not instance:
|
||||
return error_response(404, "流程实例不存在")
|
||||
user_id = (user.get("user_id") or user.get("id") or "").strip()
|
||||
skip_scope = task.assignee_user_id == user_id
|
||||
_assert_wf_instance_in_scope(db, instance, user, skip_scope=skip_scope)
|
||||
tenant_no = user.get("tenant_no", "shanxia2.0")
|
||||
engine = WorkflowEngine(db)
|
||||
instance = engine.approve_task(task_id, body.comment or "", tenant_no)
|
||||
@@ -482,6 +517,15 @@ async def reject_task(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
task = wf_crud.get_task(db, task_id)
|
||||
if not task:
|
||||
return error_response(404, "任务不存在")
|
||||
instance = wf_crud.get_instance(db, task.instance_id)
|
||||
if not instance:
|
||||
return error_response(404, "流程实例不存在")
|
||||
user_id = (user.get("user_id") or user.get("id") or "").strip()
|
||||
skip_scope = task.assignee_user_id == user_id
|
||||
_assert_wf_instance_in_scope(db, instance, user, skip_scope=skip_scope)
|
||||
tenant_no = user.get("tenant_no", "shanxia2.0")
|
||||
engine = WorkflowEngine(db)
|
||||
instance = engine.reject_task(task_id, body.comment or "", tenant_no)
|
||||
@@ -499,9 +543,22 @@ async def reject_task(
|
||||
async def get_business_all_tasks(
|
||||
business_type: str,
|
||||
business_id: str,
|
||||
_user: dict = Depends(require_admin_or_super_admin),
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
instances = (
|
||||
db.query(WfProcessInstance)
|
||||
.filter(
|
||||
WfProcessInstance.business_type == business_type,
|
||||
WfProcessInstance.business_id == business_id,
|
||||
WfProcessInstance.del_flag == 0,
|
||||
)
|
||||
.order_by(WfProcessInstance.create_time.asc())
|
||||
.all()
|
||||
)
|
||||
if instances:
|
||||
_assert_wf_instance_in_scope(db, instances[0], user)
|
||||
|
||||
tasks = wf_crud.get_all_tasks_for_business(db, business_type, business_id)
|
||||
|
||||
business_form_fields = _get_business_form_fields(db, business_type, business_id)
|
||||
|
||||
Reference in new issue
Block a user