20ce6bccef
后端: - 删除 StudyRolePermission 模型和 project_permissions API 文件 - 重写 project_permissions.py core,移除所有模块级权限函数 - 重写 permission_cache.py,移除模块级权限缓存逻辑 - 新增权限模板功能(PermissionTemplate 模型、API、服务层) - 新增 permission_templates 数据库迁移 - 迁移 8 个模块(attachments、audit_logs、dashboard、faqs、 faq_categories、fees_attachments、knowledge_notes、 material_equipments、overview、subject_histories、subject_pds) 至接口级权限检查 - 删除所有模块级权限相关测试文件,新增权限模板测试 前端: - 删除 ProjectPermissions.vue 和 ProjectPermissionsModule.vue - 重写 projectRoutePermissions.ts,改为基于接口级权限格式 - 更新 store/study.ts、router/index.ts、AuditLogs.vue、Projects.vue 中的权限 API 调用,从 fetchProjectRolePermissions 改为 fetchApiEndpointPermissions - 清理 types/api.ts 中的旧模块级权限类型定义 - 新增 PermissionTemplateSelector.vue 组件 - 更新权限管理页面,移除模块级权限 tab Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
137 lines
3.9 KiB
Python
137 lines
3.9 KiB
Python
from __future__ import annotations
|
|
|
|
import uuid
|
|
|
|
from sqlalchemy import delete, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.models.api_endpoint_permission import ApiEndpointPermission
|
|
from app.core.api_permissions import API_ENDPOINT_PERMISSIONS, OPERATION_PREREQUISITES
|
|
from app.core.permission_cache import get_permission_cache
|
|
|
|
|
|
async def role_has_api_permission(
|
|
db: AsyncSession,
|
|
study_id: uuid.UUID,
|
|
role: str | None,
|
|
endpoint_key: str,
|
|
check_prerequisites: bool = True,
|
|
) -> bool:
|
|
"""检查角色是否有权访问特定接口"""
|
|
if role == "ADMIN":
|
|
return True
|
|
|
|
result = await db.execute(
|
|
select(ApiEndpointPermission).where(
|
|
ApiEndpointPermission.study_id == study_id,
|
|
ApiEndpointPermission.role == role,
|
|
ApiEndpointPermission.endpoint_key == endpoint_key,
|
|
)
|
|
)
|
|
perm = result.scalar_one_or_none()
|
|
if perm is None:
|
|
return False
|
|
|
|
if not perm.allowed:
|
|
return False
|
|
|
|
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(
|
|
db: AsyncSession,
|
|
study_id: uuid.UUID,
|
|
) -> dict[str, dict[str, dict[str, bool]]]:
|
|
"""获取项目的接口级权限矩阵
|
|
|
|
返回格式: {role: {endpoint_key: {allowed: bool}}}
|
|
"""
|
|
result = await db.execute(
|
|
select(ApiEndpointPermission).where(
|
|
ApiEndpointPermission.study_id == study_id,
|
|
)
|
|
)
|
|
rows = result.scalars().all()
|
|
|
|
roles = ["PM", "CRA", "PV", "MEDICAL_REVIEW", "IMP", "QA"]
|
|
matrix: dict[str, dict[str, dict[str, bool]]] = {}
|
|
for role in roles:
|
|
matrix[role] = {}
|
|
for endpoint_key, config in API_ENDPOINT_PERMISSIONS.items():
|
|
default_allowed = role in config.get("default_roles", [])
|
|
matrix[role][endpoint_key] = {"allowed": default_allowed}
|
|
|
|
for row in rows:
|
|
if row.role not in matrix:
|
|
matrix[row.role] = {}
|
|
matrix[row.role][row.endpoint_key] = {"allowed": row.allowed}
|
|
|
|
return matrix
|
|
|
|
|
|
async def replace_api_endpoint_permissions(
|
|
db: AsyncSession,
|
|
study_id: uuid.UUID,
|
|
payload: dict[str, dict[str, bool]],
|
|
) -> dict[str, dict[str, dict[str, bool]]]:
|
|
"""替换项目的接口级权限矩阵"""
|
|
await db.execute(
|
|
delete(ApiEndpointPermission).where(
|
|
ApiEndpointPermission.study_id == study_id,
|
|
)
|
|
)
|
|
|
|
for role, endpoints in payload.items():
|
|
if role == "ADMIN":
|
|
continue
|
|
for endpoint_key, allowed in endpoints.items():
|
|
if endpoint_key not in API_ENDPOINT_PERMISSIONS:
|
|
continue
|
|
db.add(
|
|
ApiEndpointPermission(
|
|
study_id=study_id,
|
|
role=role,
|
|
endpoint_key=endpoint_key,
|
|
allowed=allowed,
|
|
)
|
|
)
|
|
|
|
await db.commit()
|
|
|
|
cache = get_permission_cache()
|
|
cache.invalidate_project_permissions(study_id)
|
|
cache.invalidate_all_member_roles(study_id)
|
|
|
|
return await get_api_endpoint_permissions(db, study_id)
|