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>
171 lines
6.6 KiB
Python
171 lines
6.6 KiB
Python
import uuid
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.deps import 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 site as site_crud
|
|
from app.crud import subject as subject_crud
|
|
from app.crud import subject_history as history_crud
|
|
from app.crud import study as study_crud
|
|
from app.schemas.subject_history import SubjectHistoryCreate, SubjectHistoryRead, SubjectHistoryUpdate
|
|
|
|
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_subject_active(db: AsyncSession, subject) -> None:
|
|
if not subject or not subject.site_id:
|
|
return
|
|
site = await site_crud.get_site(db, subject.site_id)
|
|
if not site or not site.is_active:
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="中心已停用")
|
|
|
|
|
|
@router.post(
|
|
"/histories",
|
|
response_model=SubjectHistoryRead,
|
|
status_code=status.HTTP_201_CREATED,
|
|
dependencies=[Depends(require_api_permission("subject_histories:create"))],
|
|
)
|
|
async def create_history(
|
|
study_id: uuid.UUID,
|
|
subject_id: uuid.UUID,
|
|
history_in: SubjectHistoryCreate,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> SubjectHistoryRead:
|
|
await _ensure_study_exists(db, study_id)
|
|
subject = await subject_crud.get_subject(db, subject_id)
|
|
if not subject or subject.study_id != study_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="参与者不存在")
|
|
await _ensure_subject_active(db, subject)
|
|
if history_in.subject_id != subject_id:
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="参与者不匹配")
|
|
history = await history_crud.create_history(db, study_id, history_in, created_by=current_user.id)
|
|
await audit_crud.log_action(
|
|
db,
|
|
study_id=study_id,
|
|
entity_type="subject_history",
|
|
entity_id=history.id,
|
|
action="CREATE_SUBJECT_HISTORY",
|
|
detail="病史记录已创建",
|
|
operator_id=current_user.id,
|
|
operator_role=current_user.role,
|
|
)
|
|
return SubjectHistoryRead.model_validate(history)
|
|
|
|
|
|
@router.get(
|
|
"/histories",
|
|
response_model=list[SubjectHistoryRead],
|
|
dependencies=[Depends(require_api_permission("subject_histories:read"))],
|
|
)
|
|
async def list_histories(
|
|
study_id: uuid.UUID,
|
|
subject_id: uuid.UUID,
|
|
skip: int = 0,
|
|
limit: int = 200,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
) -> list[SubjectHistoryRead]:
|
|
await _ensure_study_exists(db, study_id)
|
|
subject = await subject_crud.get_subject(db, subject_id)
|
|
if not subject or subject.study_id != study_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="参与者不存在")
|
|
items = await history_crud.list_histories(db, study_id, subject_id, skip=skip, limit=limit)
|
|
return [SubjectHistoryRead.model_validate(item) for item in items]
|
|
|
|
|
|
@router.get(
|
|
"/histories/{history_id}",
|
|
response_model=SubjectHistoryRead,
|
|
dependencies=[Depends(require_api_permission("subject_histories:read"))],
|
|
)
|
|
async def get_history(
|
|
study_id: uuid.UUID,
|
|
subject_id: uuid.UUID,
|
|
history_id: uuid.UUID,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
) -> SubjectHistoryRead:
|
|
await _ensure_study_exists(db, study_id)
|
|
subject = await subject_crud.get_subject(db, subject_id)
|
|
if not subject or subject.study_id != study_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="参与者不存在")
|
|
history = await history_crud.get_history(db, history_id)
|
|
if not history or history.study_id != study_id or history.subject_id != subject_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="病史记录不存在")
|
|
return SubjectHistoryRead.model_validate(history)
|
|
|
|
|
|
@router.patch(
|
|
"/histories/{history_id}",
|
|
response_model=SubjectHistoryRead,
|
|
dependencies=[Depends(require_api_permission("subject_histories:update"))],
|
|
)
|
|
async def update_history(
|
|
study_id: uuid.UUID,
|
|
subject_id: uuid.UUID,
|
|
history_id: uuid.UUID,
|
|
history_in: SubjectHistoryUpdate,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> SubjectHistoryRead:
|
|
await _ensure_study_exists(db, study_id)
|
|
subject = await subject_crud.get_subject(db, subject_id)
|
|
if not subject or subject.study_id != study_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="参与者不存在")
|
|
history = await history_crud.get_history(db, history_id)
|
|
if not history or history.study_id != study_id or history.subject_id != subject_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="病史记录不存在")
|
|
history = await history_crud.update_history(db, history, history_in)
|
|
await audit_crud.log_action(
|
|
db,
|
|
study_id=study_id,
|
|
entity_type="subject_history",
|
|
entity_id=history_id,
|
|
action="UPDATE_SUBJECT_HISTORY",
|
|
detail="病史记录已更新",
|
|
operator_id=current_user.id,
|
|
operator_role=current_user.role,
|
|
)
|
|
return SubjectHistoryRead.model_validate(history)
|
|
|
|
|
|
@router.delete(
|
|
"/histories/{history_id}",
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
dependencies=[Depends(require_api_permission("subject_histories:delete"))],
|
|
)
|
|
async def delete_history(
|
|
study_id: uuid.UUID,
|
|
subject_id: uuid.UUID,
|
|
history_id: uuid.UUID,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> None:
|
|
await _ensure_study_exists(db, study_id)
|
|
subject = await subject_crud.get_subject(db, subject_id)
|
|
if not subject or subject.study_id != study_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="参与者不存在")
|
|
history = await history_crud.get_history(db, history_id)
|
|
if not history or history.study_id != study_id or history.subject_id != subject_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="病史记录不存在")
|
|
await history_crud.delete_history(db, history)
|
|
await audit_crud.log_action(
|
|
db,
|
|
study_id=study_id,
|
|
entity_type="subject_history",
|
|
entity_id=history_id,
|
|
action="DELETE_SUBJECT_HISTORY",
|
|
detail="病史记录已删除",
|
|
operator_id=current_user.id,
|
|
operator_role=current_user.role,
|
|
)
|