feat(分组权限): 业务分组模型 + DataScope 判定 + 列表接口接入

第一阶段:模型 → 判定 → /auth/me → 列表。统计接口与前端管理页随后。

1) 模型与迁移(head 从 k1l2m3n4o5p6 推进到 l1m2n3o4p5q6)
   · business_groups        组定义,parent_id 表达「大组 > 小组」
   · business_group_phases  可见范围,独立成表以支持多选 —— 需求要求
                            「范围可配置、不要写死」,单列存不下多个 phase
   · business_group_members 成员,一人可属多组(这是「同时看生产+维修」的实现)
   只建表、不写种子数据,所以可以先部署代码再建组。

2) DataScope 判定模块(app/services/data_scope_service.py)
   全仓库唯一的权限谓词来源,业务代码里不准再出现 lifecycle_phase 过滤。
   两条红线照抄部门隔离的教训:
   · None(不限) 与 frozenset()(空) 语义相反,绝不共用一个哨兵值
   · 空集合必须显式 false() —— SQLAlchemy 对 in_(()) 生 成 IN (NULL),
     一旦退化成不过滤就是全量泄漏
   解析优先级:SUPER_ADMIN 硬放行(不可被分组覆盖)
             > 显式分组(分组优先于角色)
             > 未分组 SUPERVISOR 默认全厂
             > 未分组普通用户

3) 过渡期开关 DATA_SCOPE_UNGROUPED(默认 ALL)
   直接上严格模式会让所有未分组工人当场看不到自己的任务、现场停摆。
   默认 ALL 先放行并打 WARNING 记录「谁还没分组」,配好组后再改 NONE。

4) /auth/me 返回 scope,phase 中文标签由服务端下发
   —— 前端已有两份 phase 词表副本,不再加第三份。

5) 列表接口接入
   · get_all_products:过滤加在 offset/limit 之前(其下有 6 段基于 product_ids
     的批量预计算,过滤晚了等于算完再丢)
   · get_all_tasks:谓词进【共享 filters】,保证 count 与 select 两条独立语句
     同时生效,否则 total 与实际页不一致、移动端 hasMore 判断跟着错
   · 扫码 get_product_by_serial 刻意不过滤,理由写死在 docstring 里

实测:空 scope 生成 false、受限 scope 生成 JOIN + IN 谓词;
/products 返回 2 条、/tasks 的 total 与 returned 一致。
This commit is contained in:
2026-09-21 17:07:46 +08:00
parent f9f3d90f96
commit cfcfca7269
12 changed files with 580 additions and 12 deletions

View File

@ -23,6 +23,8 @@ from app.core.lifecycle import (
from app.models.product import Product
from app.models.task_log import TaskLog
from app.core.roles import ADMIN_ROLES
# 业务分组数据范围 —— 只借用类型,谓词一律由 DataScope 生成,不在这里手写 IN 条件
from app.services.data_scope_service import DataScope
from app.schemas.task import (
TaskCreate,
TaskUpdate,
@ -455,22 +457,38 @@ async def update_task(db: AsyncSession, task_id: uuid.UUID, data: TaskUpdate) ->
async def get_all_tasks(
db: AsyncSession, product_id: uuid.UUID | None = None,
db: AsyncSession, scope: DataScope,
product_id: uuid.UUID | None = None,
assignee_id: str | None = None, skip: int = 0, limit: int = 50
) -> TaskListResponse:
"""获取任务列表,可按产品/负责人筛选"""
"""获取任务列表,可按产品/负责人筛选
⚠️ tasks 表**没有** lifecycle_phase —— 业务分组的范围判定必须 join(Product)。
所以仅在 scope 受限时补 join不限范围超管时不加避免给每次请求
白搭一个 join。
⚠️ scope **不给默认值**:它是权限边界,漏传要当场 TypeError不能静默全量。
"""
filters = []
if product_id:
filters.append(Task.product_id == product_id)
if assignee_id:
filters.append(Task.assignee_id == assignee_id)
# 🚀 数据范围谓词进【共享的 filters】—— 下面 count 与 select 是两条独立语句,
# 谓词只写一次就天然同时生效。若只给其中一条加过滤total 会与实际页内容
# 不一致,移动端 hasMore靠 tasks.length < total判断跟着错。
needs_join = scope.phases is not None
if needs_join:
filters.append(scope.task_where())
# 总数必须独立 COUNT移动端「我的任务」用 total 判断 hasMore
# tasks.length < total若 total 取当前页条数,首页满员时
# hasMore 恒为 false列表永远停在第一页。
total = await db.scalar(
select(func.count()).select_from(Task).where(*filters)
) or 0
total_stmt = select(func.count()).select_from(Task)
if needs_join:
total_stmt = total_stmt.join(Product, Task.product_id == Product.id)
total = await db.scalar(total_stmt.where(*filters)) or 0
stmt = (
select(Task)
@ -483,6 +501,8 @@ async def get_all_tasks(
.limit(limit)
.order_by(Task.created_at.desc())
)
if needs_join:
stmt = stmt.join(Product, Task.product_id == Product.id)
result = await db.execute(stmt)
tasks = result.scalars().all()