939fc70532
新增项目级角色权限矩阵,覆盖 PM、CRA、PV、IMP、QA 等项目角色,并通过迁移表持久化各业务模块的读写权限。 后端将参与者、合同费用、物资、启动、里程碑、风险问题、监查稽查、FAQ、共享库、文件版本等接口接入矩阵校验,修复审计日志导出权限和登录 502 的依赖导入问题。 前端新增权限管理页面和项目路由权限映射,按矩阵控制导航显示、直接访问拦截、操作按钮启停,并限制管理后台仅 admin 和授权 PM 可进入。 补充后端权限归一化与接口静态测试、前端路由/权限工具/权限管理页面/工作台等回归测试。
178 lines
6.8 KiB
Python
178 lines
6.8 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_study_permission
|
|
from app.crud import audit as audit_crud
|
|
from app.crud import finance_contract as contract_crud
|
|
from app.crud import site as site_crud
|
|
from app.crud import study as study_crud
|
|
from app.schemas.finance_contract import FinanceContractCreate, FinanceContractRead, FinanceContractUpdate
|
|
|
|
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_site_name_active(db: AsyncSession, study_id: uuid.UUID, site_name: str | None):
|
|
if not site_name:
|
|
return
|
|
active_names = await site_crud.list_active_names(db, study_id)
|
|
if site_name not in active_names:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="中心已停用")
|
|
|
|
|
|
@router.post(
|
|
"/contracts",
|
|
response_model=FinanceContractRead,
|
|
status_code=status.HTTP_201_CREATED,
|
|
dependencies=[Depends(require_study_permission("fees", "write"))],
|
|
)
|
|
async def create_contract(
|
|
study_id: uuid.UUID,
|
|
contract_in: FinanceContractCreate,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> FinanceContractRead:
|
|
await _ensure_study_exists(db, study_id)
|
|
cra_scope = await get_cra_site_scope(db, study_id, current_user)
|
|
if cra_scope and contract_in.site_name not in cra_scope[1]:
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
|
|
await _ensure_site_name_active(db, study_id, contract_in.site_name)
|
|
contract = await contract_crud.create_contract(db, study_id, contract_in, created_by=current_user.id)
|
|
await audit_crud.log_action(
|
|
db,
|
|
study_id=study_id,
|
|
entity_type="finance_contract",
|
|
entity_id=contract.id,
|
|
action="CREATE_FINANCE_CONTRACT",
|
|
detail=f"合同费用 {contract.contract_no} 已创建",
|
|
operator_id=current_user.id,
|
|
operator_role=current_user.role,
|
|
)
|
|
return FinanceContractRead.model_validate(contract)
|
|
|
|
|
|
@router.get(
|
|
"/contracts",
|
|
response_model=list[FinanceContractRead],
|
|
dependencies=[Depends(require_study_permission("fees", "read"))],
|
|
)
|
|
async def list_contracts(
|
|
study_id: uuid.UUID,
|
|
site_name: str | None = None,
|
|
contract_no: str | None = None,
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> list[FinanceContractRead]:
|
|
await _ensure_study_exists(db, study_id)
|
|
cra_scope = await get_cra_site_scope(db, study_id, current_user)
|
|
site_names = cra_scope[1] if cra_scope else None
|
|
if site_name and site_names is not None and site_name not in site_names:
|
|
return []
|
|
items = await contract_crud.list_contracts(
|
|
db,
|
|
study_id,
|
|
site_name=site_name,
|
|
site_names=site_names,
|
|
contract_no=contract_no,
|
|
skip=skip,
|
|
limit=limit,
|
|
)
|
|
return [FinanceContractRead.model_validate(item) for item in items]
|
|
|
|
|
|
@router.get(
|
|
"/contracts/{contract_id}",
|
|
response_model=FinanceContractRead,
|
|
dependencies=[Depends(require_study_permission("fees", "read"))],
|
|
)
|
|
async def get_contract(
|
|
study_id: uuid.UUID,
|
|
contract_id: uuid.UUID,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> FinanceContractRead:
|
|
await _ensure_study_exists(db, study_id)
|
|
contract = await contract_crud.get_contract(db, contract_id)
|
|
if not contract or contract.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 contract.site_name not in cra_scope[1]:
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
|
|
return FinanceContractRead.model_validate(contract)
|
|
|
|
|
|
@router.patch(
|
|
"/contracts/{contract_id}",
|
|
response_model=FinanceContractRead,
|
|
dependencies=[Depends(require_study_permission("fees", "write"))],
|
|
)
|
|
async def update_contract(
|
|
study_id: uuid.UUID,
|
|
contract_id: uuid.UUID,
|
|
contract_in: FinanceContractUpdate,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> FinanceContractRead:
|
|
await _ensure_study_exists(db, study_id)
|
|
contract = await contract_crud.get_contract(db, contract_id)
|
|
if not contract or contract.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 contract.site_name not in cra_scope[1]:
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
|
|
await _ensure_site_name_active(db, study_id, contract.site_name)
|
|
contract = await contract_crud.update_contract(db, contract, contract_in)
|
|
await audit_crud.log_action(
|
|
db,
|
|
study_id=study_id,
|
|
entity_type="finance_contract",
|
|
entity_id=contract_id,
|
|
action="UPDATE_FINANCE_CONTRACT",
|
|
detail=f"合同费用 {contract_id} 已更新",
|
|
operator_id=current_user.id,
|
|
operator_role=current_user.role,
|
|
)
|
|
return FinanceContractRead.model_validate(contract)
|
|
|
|
|
|
@router.delete(
|
|
"/contracts/{contract_id}",
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
dependencies=[Depends(require_study_permission("fees", "write"))],
|
|
)
|
|
async def delete_contract(
|
|
study_id: uuid.UUID,
|
|
contract_id: uuid.UUID,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> None:
|
|
await _ensure_study_exists(db, study_id)
|
|
contract = await contract_crud.get_contract(db, contract_id)
|
|
if not contract or contract.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 contract.site_name not in cra_scope[1]:
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
|
|
await _ensure_site_name_active(db, study_id, contract.site_name)
|
|
await contract_crud.delete_contract(db, contract)
|
|
await audit_crud.log_action(
|
|
db,
|
|
study_id=study_id,
|
|
entity_type="finance_contract",
|
|
entity_id=contract_id,
|
|
action="DELETE_FINANCE_CONTRACT",
|
|
detail=f"合同费用 {contract_id} 已删除",
|
|
operator_id=current_user.id,
|
|
operator_role=current_user.role,
|
|
)
|