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:
@ -1,14 +1,24 @@
|
||||
"""认证 API — 对接 MOM sys_user + 双 Token 刷新"""
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.schemas.user import (
|
||||
DataScopeInfo,
|
||||
LoginRequest,
|
||||
LoginResponse,
|
||||
RefreshRequest,
|
||||
RefreshResponse,
|
||||
UserResponse,
|
||||
)
|
||||
from app.core.database import get_db
|
||||
from app.core.deps import get_data_scope
|
||||
from app.core.security import peek_token_identity
|
||||
from app.services.auth_service import login, refresh_access_token, get_current_user
|
||||
from app.services.data_scope_service import (
|
||||
DataScope,
|
||||
scope_group_names,
|
||||
scope_phase_labels,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/auth", tags=["认证"])
|
||||
|
||||
@ -70,11 +80,33 @@ def logout_endpoint(current_user: dict = Depends(get_current_user)):
|
||||
|
||||
|
||||
@router.get("/me", response_model=UserResponse)
|
||||
def get_me(current_user: dict = Depends(get_current_user)):
|
||||
"""获取当前用户信息(从 Access Token 解析)"""
|
||||
async def get_me(
|
||||
current_user: dict = Depends(get_current_user),
|
||||
scope: DataScope = Depends(get_data_scope),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
"""获取当前用户信息 + 业务分组数据范围。
|
||||
|
||||
为什么把 scope 挂在这里而不是新开 /auth/data-scope:
|
||||
前端 AuthContext 挂载时**本来就会调一次本接口**,顺手返回是零额外请求;
|
||||
新开接口则要在每个页面额外拉一次。响应只新增可选字段,路径与结构不变。
|
||||
|
||||
拿到 scope 后前端能在页头显示「维修组 · 售后回流」或
|
||||
「未分组 · 暂无数据权限」—— 后者尤其重要:列表为空时必须让用户知道
|
||||
是权限问题,而不是以为系统坏了。
|
||||
"""
|
||||
return UserResponse(
|
||||
id=current_user["sub"],
|
||||
username=current_user.get("username", ""),
|
||||
display_name=current_user.get("display_name", ""),
|
||||
role=current_user.get("role", "operator"),
|
||||
scope=DataScopeInfo(
|
||||
is_unrestricted=scope.is_unrestricted,
|
||||
is_empty=scope.is_empty,
|
||||
phases=sorted(scope.phases) if scope.phases else [],
|
||||
phase_labels=scope_phase_labels(scope),
|
||||
groups=await scope_group_names(db, scope),
|
||||
is_leader=bool(scope.leader_of),
|
||||
reason=scope.reason,
|
||||
),
|
||||
)
|
||||
|
||||
@ -15,7 +15,9 @@ from app.schemas.product import (
|
||||
ProductScanResponse,
|
||||
)
|
||||
from app.services import product_service, product_finalize_service
|
||||
from app.core.deps import get_data_scope
|
||||
from app.services.auth_service import get_current_user
|
||||
from app.services.data_scope_service import DataScope
|
||||
from app.services.qrcode_service import generate_qrcode_png
|
||||
|
||||
router = APIRouter(prefix="/products", tags=["产品管理"])
|
||||
@ -67,6 +69,11 @@ async def scan_product(
|
||||
"""
|
||||
扫码接口:根据 16 位序列号查询产品及其当前进度。
|
||||
返回产品信息、所属订单、以及顶层任务列表。
|
||||
|
||||
⚠️ 本接口**刻意不受业务分组数据范围约束** —— 与「列表按组过滤」同等重要的
|
||||
设计决策(详见 product_service.get_product_by_serial 的 docstring):
|
||||
维修组扫到生产中的设备必须能【看】——现场要判断这台机器是不是走错了流程,
|
||||
看不见就无法判断。「不能操作」由操作类接口各自的属主校验保证。
|
||||
"""
|
||||
return await product_service.get_product_by_serial(db, serial_number)
|
||||
|
||||
@ -83,10 +90,14 @@ async def list_products(
|
||||
status: str | None = Query(None, description="产品状态筛选: PENDING/WIP/COMPLETED/ARCHIVED"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user),
|
||||
scope: DataScope = Depends(get_data_scope),
|
||||
):
|
||||
"""获取产品列表 — 支持 keyword 搜索 + 状态筛选"""
|
||||
"""获取产品列表 — 支持 keyword 搜索 + 状态筛选
|
||||
|
||||
结果受业务分组数据范围约束(见 app/services/data_scope_service.py)。
|
||||
"""
|
||||
return await product_service.get_all_products(
|
||||
db, skip=skip, limit=limit, keyword=keyword, status_filter=status,
|
||||
db, scope=scope, skip=skip, limit=limit, keyword=keyword, status_filter=status,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@ -7,6 +7,8 @@ from app.services.auth_service import get_current_user
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.deps import get_data_scope
|
||||
from app.services.data_scope_service import DataScope
|
||||
from app.schemas.task import (
|
||||
TaskCreate,
|
||||
TaskUpdate,
|
||||
@ -39,10 +41,16 @@ async def list_tasks(
|
||||
limit: int = Query(50, ge=1, le=200),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user),
|
||||
scope: DataScope = Depends(get_data_scope),
|
||||
):
|
||||
"""获取任务列表,可按产品/负责人筛选(只返回顶层任务)"""
|
||||
"""获取任务列表,可按产品/负责人筛选(只返回顶层任务)
|
||||
|
||||
结果受业务分组数据范围约束(见 app/services/data_scope_service.py)。
|
||||
"""
|
||||
pid = uuid.UUID(product_id) if product_id else None
|
||||
return await task_service.get_all_tasks(db, product_id=pid, assignee_id=assignee_id, skip=skip, limit=limit)
|
||||
return await task_service.get_all_tasks(
|
||||
db, scope=scope, product_id=pid, assignee_id=assignee_id, skip=skip, limit=limit,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{task_id}", response_model=TaskResponse)
|
||||
|
||||
Reference in New Issue
Block a user