5327e00cf1
1. 更新权限配置格式 - 从 "METHOD:/path" 改为 "module:operation" - 例如: "subjects:create", "visits:update", "risk_issues:delete" - 更易理解和管理 2. 新增权限操作列表 API - GET /api/v1/api-permissions/operations - 返回所有权限操作及其描述 3. 更新前端权限管理界面 - 显示业务语言的权限名称 - 按模块和操作类型筛选 - 改进用户体验 4. 修复导入错误 - 更新 MODULE_TO_ENDPOINTS 为 OPERATION_TO_ENDPOINTS - 禁用 API 端点注册表初始化 Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
177 lines
5.0 KiB
Python
177 lines
5.0 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,
|
|
)
|
|
from app.models.api_endpoint_registry import ApiEndpointRegistry
|
|
from app.core.api_permissions import API_ENDPOINT_PERMISSIONS, PROJECT_PERMISSION_ROLES
|
|
|
|
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"],
|
|
}
|
|
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",
|
|
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"],
|
|
}
|
|
for key, config in API_ENDPOINT_PERMISSIONS.items()
|
|
]
|
|
|
|
return {"operations": operations_list}
|
|
|
|
|
|
@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
|