f8a959b801
【界面优化】
- 移除权限模板列表和角色概览卡片上的"系统预设"标签,视觉上减少噪音
- 将 PermissionTemplateSelector 改为纯展示组件,重命名为"角色权限概览"
- 移除"应用此模板"按钮及预览面板,卡片仅展示各角色权限统计
- 角色描述改为鼠标悬浮 tooltip 显示,节省卡片空间
- 未生效角色以虚线边框 + 降低透明度区分,悬浮提示状态说明
【生效管理】
- Study 模型新增 active_roles JSON 字段,记录项目已生效角色列表
- 新增 /studies/{study_id}/active-roles GET/PUT 接口
- 数据库迁移 20260518_01:studies 表添加 active_roles 列
- 成员管理的项目角色下拉列表仅显示已生效角色,未生效角色不可分配
【模板管理抽屉重构】
- 抽屉改为三标签页:模板列表 / 生效管理 / 编辑权限
- 生效管理:开关控制各角色生效状态,保存后同步后端
- 编辑权限:左右分栏设计,左侧角色列表点击选中,右侧实时展示权限勾选
- 原独立的角色权限编辑抽屉合并至此,角色概览卡片"编辑权限"直接跳转对应标签页
【操作类型标签统一】
- 系统级权限和编辑权限界面的操作类型标签统一使用细分类型
- 操作类型从粗粒度 read/write 细化为 read/create/update/delete/export
- 颜色规范:读取灰色、创建绿色、更新黄色、删除红色、导出无色
- 权限模板管理接口限制为 ADMIN 角色,修复原先权限过宽的问题
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
229 lines
7.2 KiB
Python
229 lines
7.2 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"])
|
||
|
||
# 项目级路由(需要 study_id,挂载到 /studies/{study_id}/api-permissions)
|
||
study_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,
|
||
}
|
||
|
||
|
||
@study_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)
|
||
|
||
# 构建返回格式(get_api_endpoint_permissions 已返回 {role: {key: {"allowed": bool}}})
|
||
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():
|
||
perm = permissions.get(role, {}).get(endpoint_key, {"allowed": False})
|
||
# perm 已经是 {"allowed": bool},直接使用
|
||
allowed = perm["allowed"] if isinstance(perm, dict) else bool(perm)
|
||
result[role][endpoint_key] = {"allowed": allowed}
|
||
|
||
return result
|
||
|
||
|
||
@study_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():
|
||
perm = permissions.get(role, {}).get(endpoint_key, {"allowed": False})
|
||
allowed = perm["allowed"] if isinstance(perm, dict) else bool(perm)
|
||
result[role][endpoint_key] = {"allowed": allowed}
|
||
|
||
return result
|