权限系统迁移完成:接口级权限 + 前置权限检查

## 核心改进
- 完全迁移到接口级权限(68个API端点)
- 实现前置权限检查机制,解决跨模块数据访问权限问题
- 添加权限管理API端点,支持前置权限查询

## 关键变更
1. 权限配置系统
   - 添加OPERATION_PREREQUISITES映射表
   - 所有操作配置前置权限依赖

2. 权限检查函数
   - role_has_api_permission()支持前置权限检查
   - get_missing_prerequisites()获取缺失权限列表

3. 权限管理API
   - GET /api-permissions/operations - 获取所有操作及前置权限
   - GET /api-permissions/operations/prerequisites - 获取前置权限依赖
   - GET /api-permissions/{endpoint_key}/prerequisites - 检查缺失权限

4. API端点迁移
   - 第1批:subjects, visits, aes, monitoring_visit_issues (23个)
   - 第2批:members, sites, project_milestones (11个)
   - 第3批:finance_contracts, fees_contracts, drug_shipments (15个)
   - 第4批:startup endpoints (19个)

## 测试验证
- 单元测试:24个测试全部通过
- 集成测试:22个迁移端点测试通过
- 前置权限测试:12个测试全部通过
- 总计:46个测试全部通过

## 向后兼容性
- 模块级权限表保留用于历史数据
- 接口级权限未配置时自动回退到模块级权限
- 现有权限配置继续有效

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
Cheng Zhou
2026-05-14 12:44:18 +08:00
parent 5327e00cf1
commit 3630b9000f
22 changed files with 1382 additions and 243 deletions
+47 -8
View File
@@ -8,7 +8,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from app.models.study_role_permission import StudyRolePermission
from app.models.api_endpoint_permission import ApiEndpointPermission
from app.core.api_permissions import API_ENDPOINT_PERMISSIONS, OPERATION_TO_ENDPOINTS
from app.core.api_permissions import API_ENDPOINT_PERMISSIONS, OPERATION_TO_ENDPOINTS, OPERATION_PREREQUISITES
from app.core.permission_cache import get_permission_cache
PROJECT_PERMISSION_ROLES = ("ADMIN", "PM", "CRA", "PV", "MEDICAL_REVIEW", "IMP", "QA")
@@ -266,12 +266,14 @@ async def role_has_api_permission(
study_id: uuid.UUID,
role: str | None,
endpoint_key: str,
check_prerequisites: bool = True,
) -> bool:
"""检查角色是否有权访问特定接口
权限检查优先级:
1. 接口级权限(如果已配置)
2. 模块级权限(向后兼容)
3. 前置权限检查(如果启用)
"""
if role == "ADMIN":
return True
@@ -286,16 +288,53 @@ async def role_has_api_permission(
)
perm = result.scalar_one_or_none()
if perm is not None:
return perm.allowed
has_main_permission = perm.allowed
else:
# 2. 如果没有接口级权限,回退到模块级权限(向后兼容)
endpoint_config = API_ENDPOINT_PERMISSIONS.get(endpoint_key)
if not endpoint_config:
return False
# 2. 如果没有接口级权限,回退到模块级权限(向后兼容)
endpoint_config = API_ENDPOINT_PERMISSIONS.get(endpoint_key)
if not endpoint_config:
module = endpoint_config["module"]
action = endpoint_config["action"]
has_main_permission = await role_has_project_permission(db, study_id, role, module, action)
if not has_main_permission:
return False
module = endpoint_config["module"]
action = endpoint_config["action"]
return await role_has_project_permission(db, study_id, role, module, action)
# 3. 检查前置权限
if check_prerequisites:
prerequisites = OPERATION_PREREQUISITES.get(endpoint_key, [])
for prereq_endpoint in prerequisites:
has_prereq = await role_has_api_permission(
db, study_id, role, prereq_endpoint, check_prerequisites=False
)
if not has_prereq:
return False
return True
async def get_missing_prerequisites(
db: AsyncSession,
study_id: uuid.UUID,
role: str | None,
endpoint_key: str,
) -> list[str]:
"""获取缺失的前置权限列表"""
if role == "ADMIN":
return []
missing = []
prerequisites = OPERATION_PREREQUISITES.get(endpoint_key, [])
for prereq_endpoint in prerequisites:
has_prereq = await role_has_api_permission(
db, study_id, role, prereq_endpoint, check_prerequisites=False
)
if not has_prereq:
missing.append(prereq_endpoint)
return missing
async def get_api_endpoint_permissions(