3630b9000f
## 核心改进
- 完全迁移到接口级权限(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>
225 lines
6.7 KiB
Python
225 lines
6.7 KiB
Python
"""接口级权限管理API"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Depends, status
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.deps import get_db_session, require_study_roles
|
|
from app.core.project_permissions import (
|
|
get_api_endpoint_permissions,
|
|
replace_api_endpoint_permissions,
|
|
get_missing_prerequisites,
|
|
)
|
|
from app.models.api_endpoint_registry import ApiEndpointRegistry
|
|
from app.core.api_permissions import API_ENDPOINT_PERMISSIONS, PROJECT_PERMISSION_ROLES, OPERATION_PREREQUISITES
|
|
|
|
router = APIRouter(prefix="/api-permissions", tags=["api-permissions"])
|
|
|
|
|
|
@router.get(
|
|
"/operations",
|
|
summary="获取系统中所有权限操作",
|
|
description="返回系统中所有权限操作及其描述和前置权限",
|
|
)
|
|
async def list_api_operations() -> dict[str, list[dict]]:
|
|
"""获取所有权限操作"""
|
|
operations_list = [
|
|
{
|
|
"operation_key": key,
|
|
"module": config["module"],
|
|
"action": config["action"],
|
|
"description": config["description"],
|
|
"default_roles": config["default_roles"],
|
|
"prerequisite_permissions": config.get("prerequisite_permissions", []),
|
|
}
|
|
for key, config in API_ENDPOINT_PERMISSIONS.items()
|
|
]
|
|
|
|
return {"operations": operations_list}
|
|
|
|
|
|
@router.get(
|
|
"/endpoints",
|
|
summary="获取系统中所有已注册的API端点",
|
|
description="返回系统中所有已注册的API端点及其元数据",
|
|
)
|
|
async def list_api_endpoints(
|
|
db: Annotated[AsyncSession, Depends(get_db_session)],
|
|
) -> dict[str, list[dict]]:
|
|
"""获取所有已注册的API端点"""
|
|
result = await db.execute(select(ApiEndpointRegistry))
|
|
endpoints = result.scalars().all()
|
|
|
|
endpoints_list = [
|
|
{
|
|
"endpoint_key": ep.endpoint_key,
|
|
"method": ep.method,
|
|
"path": ep.path,
|
|
"module": ep.module,
|
|
"action": ep.action,
|
|
"description": ep.description,
|
|
"default_roles": ep.default_roles.split(",") if ep.default_roles else [],
|
|
}
|
|
for ep in endpoints
|
|
]
|
|
|
|
return {"endpoints": endpoints_list}
|
|
|
|
|
|
@router.get(
|
|
"/operations/prerequisites",
|
|
summary="获取所有权限操作的前置权限依赖",
|
|
description="返回系统中所有权限操作及其前置权限依赖关系",
|
|
)
|
|
async def list_operation_prerequisites() -> dict[str, dict]:
|
|
"""获取所有权限操作的前置权限依赖"""
|
|
prerequisites_map = {}
|
|
for operation_key, prerequisites in OPERATION_PREREQUISITES.items():
|
|
prerequisites_map[operation_key] = {
|
|
"prerequisites": prerequisites,
|
|
"description": API_ENDPOINT_PERMISSIONS.get(operation_key, {}).get("description", ""),
|
|
}
|
|
|
|
return {"prerequisites": prerequisites_map}
|
|
|
|
|
|
@router.get(
|
|
"/{endpoint_key}/prerequisites",
|
|
summary="获取特定操作的缺失前置权限",
|
|
description="检查指定角色对特定操作的前置权限是否满足",
|
|
)
|
|
async def check_operation_prerequisites(
|
|
study_id: uuid.UUID,
|
|
endpoint_key: str,
|
|
role: str,
|
|
db: Annotated[AsyncSession, Depends(get_db_session)],
|
|
) -> dict:
|
|
"""检查特定操作的前置权限
|
|
|
|
返回格式:
|
|
{
|
|
"endpoint_key": "subjects:create",
|
|
"role": "CRA",
|
|
"has_main_permission": true,
|
|
"prerequisites": ["sites:read"],
|
|
"missing_prerequisites": [],
|
|
"can_perform": true
|
|
}
|
|
"""
|
|
from app.core.project_permissions import role_has_api_permission
|
|
|
|
if role == "ADMIN":
|
|
return {
|
|
"endpoint_key": endpoint_key,
|
|
"role": role,
|
|
"has_main_permission": True,
|
|
"prerequisites": OPERATION_PREREQUISITES.get(endpoint_key, []),
|
|
"missing_prerequisites": [],
|
|
"can_perform": True,
|
|
}
|
|
|
|
has_main = await role_has_api_permission(
|
|
db, study_id, role, endpoint_key, check_prerequisites=False
|
|
)
|
|
missing = await get_missing_prerequisites(db, study_id, role, endpoint_key)
|
|
|
|
return {
|
|
"endpoint_key": endpoint_key,
|
|
"role": role,
|
|
"has_main_permission": has_main,
|
|
"prerequisites": OPERATION_PREREQUISITES.get(endpoint_key, []),
|
|
"missing_prerequisites": missing,
|
|
"can_perform": has_main and len(missing) == 0,
|
|
}
|
|
|
|
|
|
@router.get(
|
|
"",
|
|
summary="获取项目的接口级权限矩阵",
|
|
description="返回项目中各角色对API端点的权限配置",
|
|
response_model=None,
|
|
)
|
|
async def get_study_api_permissions(
|
|
study_id: uuid.UUID,
|
|
_=Depends(require_study_roles(["PM"])),
|
|
db: Annotated[AsyncSession, Depends(get_db_session)] = None,
|
|
):
|
|
"""获取项目的接口级权限矩阵
|
|
|
|
返回格式:
|
|
{
|
|
"role": {
|
|
"endpoint_key": {
|
|
"allowed": true/false
|
|
}
|
|
}
|
|
}
|
|
"""
|
|
permissions = await get_api_endpoint_permissions(db, study_id)
|
|
|
|
# 构建返回格式
|
|
result: dict[str, dict[str, dict[str, bool]]] = {}
|
|
for role in PROJECT_PERMISSION_ROLES:
|
|
if role == "ADMIN":
|
|
continue
|
|
result[role] = {}
|
|
for endpoint_key in API_ENDPOINT_PERMISSIONS.keys():
|
|
result[role][endpoint_key] = {
|
|
"allowed": permissions.get(role, {}).get(endpoint_key, False)
|
|
}
|
|
|
|
return result
|
|
|
|
|
|
@router.put(
|
|
"",
|
|
summary="更新项目的接口级权限矩阵",
|
|
description="批量更新项目中各角色对API端点的权限配置",
|
|
status_code=status.HTTP_200_OK,
|
|
response_model=None,
|
|
)
|
|
async def update_study_api_permissions(
|
|
study_id: uuid.UUID,
|
|
payload: dict[str, dict[str, bool]],
|
|
_=Depends(require_study_roles(["PM"])),
|
|
db: Annotated[AsyncSession, Depends(get_db_session)] = None,
|
|
):
|
|
"""更新项目的接口级权限矩阵
|
|
|
|
请求体格式:
|
|
{
|
|
"role": {
|
|
"endpoint_key": true/false
|
|
}
|
|
}
|
|
"""
|
|
# 验证输入
|
|
for role in payload.keys():
|
|
if role == "ADMIN":
|
|
continue
|
|
if role not in PROJECT_PERMISSION_ROLES:
|
|
raise ValueError(f"无效的角色: {role}")
|
|
|
|
# 替换权限配置
|
|
await replace_api_endpoint_permissions(db, study_id, payload)
|
|
|
|
# 返回更新后的权限矩阵
|
|
permissions = await get_api_endpoint_permissions(db, study_id)
|
|
|
|
result: dict[str, dict[str, dict[str, bool]]] = {}
|
|
for role in PROJECT_PERMISSION_ROLES:
|
|
if role == "ADMIN":
|
|
continue
|
|
result[role] = {}
|
|
for endpoint_key in API_ENDPOINT_PERMISSIONS.keys():
|
|
result[role][endpoint_key] = {
|
|
"allowed": permissions.get(role, {}).get(endpoint_key, False)
|
|
}
|
|
|
|
return result
|