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>
190 lines
7.3 KiB
Python
190 lines
7.3 KiB
Python
import uuid
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.deps import get_cra_site_scope, get_current_user, get_db_session, require_study_not_locked, require_api_permission
|
|
from app.crud import audit as audit_crud
|
|
from app.crud import drug_shipment as shipment_crud
|
|
from app.crud import site as site_crud
|
|
from app.crud import study as study_crud
|
|
from app.schemas.drug_shipment import DrugShipmentCreate, DrugShipmentRead, DrugShipmentUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
async def _ensure_study_exists(db: AsyncSession, study_id: uuid.UUID):
|
|
study = await study_crud.get(db, study_id)
|
|
if not study:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="项目不存在")
|
|
return study
|
|
|
|
|
|
async def _ensure_center_active(db: AsyncSession, study_id: uuid.UUID, center_id: uuid.UUID | None):
|
|
if not center_id:
|
|
return None
|
|
site = await site_crud.get_site(db, center_id)
|
|
if not site or site.study_id != study_id or not site.is_active:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="中心已停用")
|
|
return site
|
|
|
|
|
|
@router.post(
|
|
"/shipments",
|
|
response_model=DrugShipmentRead,
|
|
status_code=status.HTTP_201_CREATED,
|
|
dependencies=[Depends(require_api_permission("drug_shipments:create")), Depends(require_study_not_locked())],
|
|
)
|
|
async def create_shipment(
|
|
study_id: uuid.UUID,
|
|
shipment_in: DrugShipmentCreate,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> DrugShipmentRead:
|
|
await _ensure_study_exists(db, study_id)
|
|
cra_scope = await get_cra_site_scope(db, study_id, current_user)
|
|
if cra_scope and shipment_in.center_id not in cra_scope[0]:
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
|
|
site = await _ensure_center_active(db, study_id, shipment_in.center_id)
|
|
shipment_in = shipment_in.model_copy(update={"site_name": site.name})
|
|
shipment = await shipment_crud.create_shipment(db, study_id, shipment_in, created_by=current_user.id)
|
|
await audit_crud.log_action(
|
|
db,
|
|
study_id=study_id,
|
|
entity_type="drug_shipment",
|
|
entity_id=shipment.id,
|
|
action="CREATE_DRUG_SHIPMENT",
|
|
detail=f"药品运输 {shipment.id} 已创建",
|
|
operator_id=current_user.id,
|
|
operator_role=current_user.role,
|
|
)
|
|
return DrugShipmentRead.model_validate(shipment)
|
|
|
|
|
|
@router.get(
|
|
"/shipments",
|
|
response_model=list[DrugShipmentRead],
|
|
dependencies=[Depends(require_api_permission("drug_shipments:read"))],
|
|
)
|
|
async def list_shipments(
|
|
study_id: uuid.UUID,
|
|
center_id: uuid.UUID | None = None,
|
|
site_name: str | None = None,
|
|
tracking_no: str | None = None,
|
|
direction: str | None = None,
|
|
status: str | None = None,
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> list[DrugShipmentRead]:
|
|
await _ensure_study_exists(db, study_id)
|
|
cra_scope = await get_cra_site_scope(db, study_id, current_user)
|
|
center_ids = cra_scope[0] if cra_scope else None
|
|
if center_id and center_ids is not None and center_id not in center_ids:
|
|
return []
|
|
items = await shipment_crud.list_shipments(
|
|
db,
|
|
study_id,
|
|
center_id=center_id,
|
|
center_ids=center_ids,
|
|
site_name=site_name,
|
|
tracking_no=tracking_no,
|
|
direction=direction,
|
|
status=status,
|
|
skip=skip,
|
|
limit=limit,
|
|
)
|
|
return [DrugShipmentRead.model_validate(item) for item in items]
|
|
|
|
|
|
@router.get(
|
|
"/shipments/{shipment_id}",
|
|
response_model=DrugShipmentRead,
|
|
dependencies=[Depends(require_api_permission("drug_shipments:read"))],
|
|
)
|
|
async def get_shipment(
|
|
study_id: uuid.UUID,
|
|
shipment_id: uuid.UUID,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> DrugShipmentRead:
|
|
await _ensure_study_exists(db, study_id)
|
|
shipment = await shipment_crud.get_shipment(db, shipment_id)
|
|
if not shipment or shipment.study_id != study_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="运输记录不存在")
|
|
cra_scope = await get_cra_site_scope(db, study_id, current_user)
|
|
if cra_scope and shipment.center_id not in cra_scope[0]:
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
|
|
return DrugShipmentRead.model_validate(shipment)
|
|
|
|
|
|
@router.patch(
|
|
"/shipments/{shipment_id}",
|
|
response_model=DrugShipmentRead,
|
|
dependencies=[Depends(require_api_permission("drug_shipments:update")), Depends(require_study_not_locked())],
|
|
)
|
|
async def update_shipment(
|
|
study_id: uuid.UUID,
|
|
shipment_id: uuid.UUID,
|
|
shipment_in: DrugShipmentUpdate,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> DrugShipmentRead:
|
|
await _ensure_study_exists(db, study_id)
|
|
shipment = await shipment_crud.get_shipment(db, shipment_id)
|
|
if not shipment or shipment.study_id != study_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="运输记录不存在")
|
|
cra_scope = await get_cra_site_scope(db, study_id, current_user)
|
|
if cra_scope and shipment.center_id not in cra_scope[0]:
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
|
|
if shipment_in.center_id:
|
|
site = await _ensure_center_active(db, study_id, shipment_in.center_id)
|
|
shipment_in = shipment_in.model_copy(update={"site_name": site.name})
|
|
else:
|
|
await _ensure_center_active(db, study_id, shipment.center_id)
|
|
shipment = await shipment_crud.update_shipment(db, shipment, shipment_in)
|
|
await audit_crud.log_action(
|
|
db,
|
|
study_id=study_id,
|
|
entity_type="drug_shipment",
|
|
entity_id=shipment_id,
|
|
action="UPDATE_DRUG_SHIPMENT",
|
|
detail=f"药品运输 {shipment_id} 已更新",
|
|
operator_id=current_user.id,
|
|
operator_role=current_user.role,
|
|
)
|
|
return DrugShipmentRead.model_validate(shipment)
|
|
|
|
|
|
@router.delete(
|
|
"/shipments/{shipment_id}",
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
dependencies=[Depends(require_api_permission("drug_shipments:delete")), Depends(require_study_not_locked())],
|
|
)
|
|
async def delete_shipment(
|
|
study_id: uuid.UUID,
|
|
shipment_id: uuid.UUID,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> None:
|
|
await _ensure_study_exists(db, study_id)
|
|
shipment = await shipment_crud.get_shipment(db, shipment_id)
|
|
if not shipment or shipment.study_id != study_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="运输记录不存在")
|
|
cra_scope = await get_cra_site_scope(db, study_id, current_user)
|
|
if cra_scope and shipment.center_id not in cra_scope[0]:
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
|
|
await _ensure_center_active(db, study_id, shipment.center_id)
|
|
await shipment_crud.delete_shipment(db, shipment)
|
|
await audit_crud.log_action(
|
|
db,
|
|
study_id=study_id,
|
|
entity_type="drug_shipment",
|
|
entity_id=shipment_id,
|
|
action="DELETE_DRUG_SHIPMENT",
|
|
detail=f"药品运输 {shipment_id} 已删除",
|
|
operator_id=current_user.id,
|
|
operator_role=current_user.role,
|
|
)
|