Merge branch 'dev'
Storage Persistence Guard / storage-persistence-audit (push) Has been cancelled
Storage Persistence Guard / storage-persistence-audit (pull_request) Has been cancelled

This commit is contained in:
Cheng Zhou
2026-06-23 17:20:57 +08:00
173 changed files with 11487 additions and 5590 deletions
File diff suppressed because it is too large Load Diff
+2
View File
@@ -0,0 +1,2 @@
env = dev
base-url = http://127.0.0.1:8888
+1
View File
@@ -5,6 +5,7 @@ Thumbs.db
# IDE/editor # IDE/editor
.idea/ .idea/
.vscode/ .vscode/
.claude/
*.swp *.swp
*.swo *.swo
+5
View File
@@ -1,5 +1,10 @@
# CTMS 项目快速上手 # CTMS 项目快速上手
## 交互安装入口
- 推荐执行 `./install.sh`,通过键盘 `↑/↓` 选择安装、更新、卸载、资源状态等操作;该入口不接受命令行参数。
- 菜单顶部会常驻显示当前 CTMS 容器部署状态,包括已检测到的环境、Compose 项目和容器运行数量。
- 底层脚本位于 `scripts/``install.sh``update.sh``uninstall.sh``status.sh`;状态脚本使用 `docker compose stats --no-stream` 采集资源快照并美化展示。
## 生产部署 ## 生产部署
- 生产入口:`docker-compose.yaml` - 生产入口:`docker-compose.yaml`
- 初始化方式:`docker compose run --rm backend-init` - 初始化方式:`docker compose run --rm backend-init`
+22 -3
View File
@@ -44,6 +44,15 @@ def _is_overdue(ae: AERead) -> bool:
return bool(ae.report_due_date and date.today() > ae.report_due_date and ae.status != "CLOSED") return bool(ae.report_due_date and date.today() > ae.report_due_date and ae.status != "CLOSED")
def _ae_audit_name(ae) -> str:
return str(getattr(ae, "term", "") or "").strip() or "AE 记录"
def _ae_audit_detail(action: str, ae) -> str:
name = _ae_audit_name(ae)
return json.dumps({"targetName": name, "description": f"{action}AE“{name}"}, ensure_ascii=False)
@router.post( @router.post(
"/", "/",
response_model=AERead, response_model=AERead,
@@ -77,7 +86,7 @@ async def create_ae(
entity_type="ae", entity_type="ae",
entity_id=ae.id, entity_id=ae.id,
action="CREATE_AE", action="CREATE_AE",
detail=f"AE {ae.id} 已创建", detail=_ae_audit_detail("创建", ae),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -247,7 +256,16 @@ async def update_ae(
entity_type="ae", entity_type="ae",
entity_id=ae_id, entity_id=ae_id,
action=action, action=action,
detail=json.dumps({"before": detail_before, "after": detail_after}, ensure_ascii=False, default=str), detail=json.dumps(
{
"targetName": _ae_audit_name(updated),
"description": f"{'变更' if action == 'AE_STATUS_CHANGE' else '更新'}AE“{_ae_audit_name(updated)}",
"before": detail_before,
"after": detail_after,
},
ensure_ascii=False,
default=str,
),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -272,6 +290,7 @@ async def delete_ae(
if not ae or ae.study_id != study_id: if not ae or ae.study_id != study_id:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="AE 不存在") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="AE 不存在")
await _ensure_ae_visible(db, study_id, AERead.model_validate(ae)) await _ensure_ae_visible(db, study_id, AERead.model_validate(ae))
ae_detail = _ae_audit_detail("删除", ae)
await ae_crud.delete_ae(db, ae) await ae_crud.delete_ae(db, ae)
await audit_crud.log_action( await audit_crud.log_action(
db, db,
@@ -279,7 +298,7 @@ async def delete_ae(
entity_type="ae", entity_type="ae",
entity_id=ae_id, entity_id=ae_id,
action="DELETE_AE", action="DELETE_AE",
detail=f"AE {ae_id} 已删除", detail=ae_detail,
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
+23 -20
View File
@@ -1,15 +1,34 @@
import json
import uuid import uuid
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.core.deps import get_operator_role_label, get_current_user, get_db_session, require_roles, require_api_permission from app.core.deps import get_operator_role_label, get_current_user, get_db_session, require_api_permission
from app.crud import audit as audit_crud from app.crud import audit as audit_crud
from app.crud import study as study_crud
from app.schemas.audit import AuditEventCreate, AuditLogRead from app.schemas.audit import AuditEventCreate, AuditLogRead
router = APIRouter() router = APIRouter()
async def _build_export_audit_detail(db: AsyncSession, study_id: uuid.UUID, action: str) -> str:
study = await study_crud.get(db, study_id)
if not study:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="项目不存在")
scope = "system" if action == "AUDIT_EXPORT_SYSTEM" else "project"
description = "导出了系统审计日志" if scope == "system" else f"导出了项目审计日志:{study.name}"
return json.dumps(
{
"targetName": "系统审计日志" if scope == "system" else study.name,
"scope": scope,
"description": description,
"result": "SUCCESS",
},
ensure_ascii=False,
)
@router.get( @router.get(
"/", "/",
response_model=list[AuditLogRead], response_model=list[AuditLogRead],
@@ -38,22 +57,6 @@ async def list_audit_logs(
return list(logs) return list(logs)
@router.delete(
"/{log_id}",
status_code=status.HTTP_204_NO_CONTENT,
dependencies=[Depends(require_roles(["ADMIN"]))],
)
async def delete_audit_log(
study_id: uuid.UUID,
log_id: uuid.UUID,
db: AsyncSession = Depends(get_db_session),
):
log = await audit_crud.get_log(db, log_id)
if not log or log.study_id != study_id:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="审计日志不存在")
await audit_crud.delete_log(db, log)
@router.post( @router.post(
"/events", "/events",
status_code=status.HTTP_201_CREATED, status_code=status.HTTP_201_CREATED,
@@ -71,10 +74,10 @@ async def create_audit_event(
await audit_crud.log_action( await audit_crud.log_action(
db, db,
study_id=study_id, study_id=study_id,
entity_type=payload.entity_type or "audit_log", entity_type="audit_log",
entity_id=payload.entity_id, entity_id=None,
action=payload.action, action=payload.action,
detail=payload.detail, detail=await _build_export_audit_detail(db, study_id, payload.action),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
+12 -28
View File
@@ -39,18 +39,15 @@ class ExtendResponse(BaseModel):
expiresAt: datetime expiresAt: datetime
class UnlockRequest(LoginRequest):
pass
class UnlockResponse(BaseModel):
accessToken: str
expiresAt: datetime
router = APIRouter() router = APIRouter()
AVATAR_ROOT = Path(__file__).resolve().parent.parent.parent / "uploads" / "avatars" AVATAR_ROOT = Path(__file__).resolve().parent.parent.parent / "uploads" / "avatars"
AVATAR_ROOT.mkdir(parents=True, exist_ok=True) AVATAR_ROOT.mkdir(parents=True, exist_ok=True)
AVATAR_ALLOWED_CONTENT_TYPES = {
"image/png": ".png",
"image/jpeg": ".jpg",
"image/gif": ".gif",
"image/webp": ".webp",
}
def issue_user_token(db_user) -> Token: def issue_user_token(db_user) -> Token:
@@ -197,24 +194,6 @@ async def extend_access_token(
return ExtendResponse(accessToken=new_token, expiresAt=expires_at) return ExtendResponse(accessToken=new_token, expiresAt=expires_at)
@router.post("/unlock", response_model=UnlockResponse)
async def unlock_session(
payload: UnlockRequest,
db: AsyncSession = Depends(get_db_session),
) -> UnlockResponse:
db_user = await authenticate_encrypted_password(payload, db)
if db_user.status != UserStatus.ACTIVE:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="账号已停用")
session_start = datetime.now(timezone.utc)
access_token = create_access_token(
user_id=str(db_user.id),
expires_minutes=None,
session_start=session_start,
)
expires_at = session_start + timedelta(minutes=settings.JWT_EXPIRE_MINUTES)
return UnlockResponse(accessToken=access_token, expiresAt=expires_at)
@router.patch("/me", response_model=UserRead) @router.patch("/me", response_model=UserRead)
async def update_me( async def update_me(
payload: UserSelfUpdate, payload: UserSelfUpdate,
@@ -242,10 +221,15 @@ async def upload_avatar(
current_user=Depends(get_current_user), current_user=Depends(get_current_user),
db: AsyncSession = Depends(get_db_session), db: AsyncSession = Depends(get_db_session),
) -> UserRead: ) -> UserRead:
ext = AVATAR_ALLOWED_CONTENT_TYPES.get(file.content_type or "")
if not ext:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="头像仅支持图片格式",
)
AVATAR_ROOT.mkdir(parents=True, exist_ok=True) AVATAR_ROOT.mkdir(parents=True, exist_ok=True)
user_dir = AVATAR_ROOT / str(current_user.id) user_dir = AVATAR_ROOT / str(current_user.id)
user_dir.mkdir(parents=True, exist_ok=True) user_dir.mkdir(parents=True, exist_ok=True)
ext = Path(file.filename).suffix or ".png"
filename = f"{uuid.uuid4()}{ext}" filename = f"{uuid.uuid4()}{ext}"
dest = user_dir / filename dest = user_dir / filename
content = await file.read() content = await file.read()
+8 -3
View File
@@ -23,6 +23,11 @@ def _shipment_audit_name(shipment) -> str:
return shipment.tracking_no or shipment.batch_no or shipment.site_name or "药品运输记录" return shipment.tracking_no or shipment.batch_no or shipment.site_name or "药品运输记录"
def _shipment_audit_description(action: str, shipment) -> str:
name = _shipment_audit_name(shipment)
return f"{action}药品流向“{name}"
async def _ensure_study_exists(db: AsyncSession, study_id: uuid.UUID): async def _ensure_study_exists(db: AsyncSession, study_id: uuid.UUID):
study = await study_crud.get(db, study_id) study = await study_crud.get(db, study_id)
if not study: if not study:
@@ -65,7 +70,7 @@ async def create_shipment(
entity_id=shipment.id, entity_id=shipment.id,
action="CREATE_DRUG_SHIPMENT", action="CREATE_DRUG_SHIPMENT",
detail=json.dumps( detail=json.dumps(
{"targetName": _shipment_audit_name(shipment), "description": f"药品运输 {_shipment_audit_name(shipment)} 已创建"}, {"targetName": _shipment_audit_name(shipment), "description": _shipment_audit_description("创建", shipment)},
ensure_ascii=False, ensure_ascii=False,
), ),
operator_id=current_user.id, operator_id=current_user.id,
@@ -178,7 +183,7 @@ async def update_shipment(
entity_id=shipment_id, entity_id=shipment_id,
action="UPDATE_DRUG_SHIPMENT", action="UPDATE_DRUG_SHIPMENT",
detail=json.dumps( detail=json.dumps(
{"targetName": _shipment_audit_name(shipment), "description": f"药品运输 {_shipment_audit_name(shipment)} 已更新"}, {"targetName": _shipment_audit_name(shipment), "description": _shipment_audit_description("更新", shipment)},
ensure_ascii=False, ensure_ascii=False,
), ),
operator_id=current_user.id, operator_id=current_user.id,
@@ -214,7 +219,7 @@ async def delete_shipment(
entity_type="drug_shipment", entity_type="drug_shipment",
entity_id=shipment_id, entity_id=shipment_id,
action="DELETE_DRUG_SHIPMENT", action="DELETE_DRUG_SHIPMENT",
detail=json.dumps({"targetName": shipment_name, "description": f"药品运输 {shipment_name} 已删除"}, ensure_ascii=False), detail=json.dumps({"targetName": shipment_name, "description": f"删除药品流向“{shipment_name}"}, ensure_ascii=False),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
+3 -3
View File
@@ -53,7 +53,7 @@ async def create_category(
entity_type="faq_category", entity_type="faq_category",
entity_id=category.id, entity_id=category.id,
action="CREATE_FAQ_CATEGORY", action="CREATE_FAQ_CATEGORY",
detail=json.dumps({"targetName": category.name, "description": f"FAQ 分类 {category.name} 已创建"}, ensure_ascii=False), detail=json.dumps({"targetName": category.name, "description": f"创建“{category.name}”分类"}, ensure_ascii=False),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, payload.study_id, current_user), operator_role=await get_operator_role_label(db, payload.study_id, current_user),
) )
@@ -118,7 +118,7 @@ async def update_category(
entity_type="faq_category", entity_type="faq_category",
entity_id=category_id, entity_id=category_id,
action="UPDATE_FAQ_CATEGORY", action="UPDATE_FAQ_CATEGORY",
detail=json.dumps({"targetName": updated.name, "description": f"FAQ 分类 {updated.name} 已更新"}, ensure_ascii=False), detail=json.dumps({"targetName": updated.name, "description": f"更新“{updated.name}”分类"}, ensure_ascii=False),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, updated.study_id, current_user), operator_role=await get_operator_role_label(db, updated.study_id, current_user),
) )
@@ -157,7 +157,7 @@ async def delete_category(
entity_type="faq_category", entity_type="faq_category",
entity_id=category_id, entity_id=category_id,
action="DELETE_FAQ_CATEGORY", action="DELETE_FAQ_CATEGORY",
detail=json.dumps({"targetName": category_name, "description": f"FAQ 分类 {category_name} 已删除"}, ensure_ascii=False), detail=json.dumps({"targetName": category_name, "description": f"删除“{category_name}”分类"}, ensure_ascii=False),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, category.study_id, current_user), operator_role=await get_operator_role_label(db, category.study_id, current_user),
) )
+18 -5
View File
@@ -1,3 +1,4 @@
import json
import uuid import uuid
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, HTTPException, status
@@ -30,6 +31,13 @@ def _is_system_admin(current_user) -> bool:
return is_system_admin(current_user) return is_system_admin(current_user)
def _compact_text(value: str | None, max_length: int = 40) -> str:
text = " ".join(str(value or "").split())
if len(text) <= max_length:
return text
return f"{text[:max_length]}..."
@router.post( @router.post(
"/", "/",
response_model=FaqRead, response_model=FaqRead,
@@ -66,13 +74,14 @@ async def create_faq(
reply_in=FaqReplyCreate(content=payload.answer), reply_in=FaqReplyCreate(content=payload.answer),
) )
await faq_crud.set_status(db, item.id, "PROCESSING") await faq_crud.set_status(db, item.id, "PROCESSING")
question_name = _compact_text(item.question)
await audit_crud.log_action( await audit_crud.log_action(
db, db,
study_id=payload.study_id, study_id=payload.study_id,
entity_type="faq_item", entity_type="faq_item",
entity_id=item.id, entity_id=item.id,
action="CREATE_FAQ_ITEM", action="CREATE_FAQ_ITEM",
detail="FAQ 已创建", detail=json.dumps({"targetName": question_name, "description": f"创建医学咨询问题“{question_name}"}, ensure_ascii=False),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, payload.study_id, current_user), operator_role=await get_operator_role_label(db, payload.study_id, current_user),
) )
@@ -170,7 +179,8 @@ async def update_faq(
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="FAQ 不存在") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="FAQ 不存在")
updated = await faq_crud.update_item(db, item, payload) updated = await faq_crud.update_item(db, item, payload)
action = "UPDATE_FAQ_ITEM" action = "UPDATE_FAQ_ITEM"
detail = "FAQ updated" question_name = _compact_text(updated.question)
detail = json.dumps({"targetName": question_name, "description": f"更新医学咨询问题“{question_name}"}, ensure_ascii=False)
await audit_crud.log_action( await audit_crud.log_action(
db, db,
study_id=item.study_id, study_id=item.study_id,
@@ -327,13 +337,14 @@ async def create_reply(
if item.status != "RESOLVED": if item.status != "RESOLVED":
await faq_crud.set_status(db, item.id, "PROCESSING") await faq_crud.set_status(db, item.id, "PROCESSING")
await faq_crud.touch_item(db, item.id) await faq_crud.touch_item(db, item.id)
question_name = _compact_text(item.question)
await audit_crud.log_action( await audit_crud.log_action(
db, db,
study_id=item.study_id, study_id=item.study_id,
entity_type="faq_reply", entity_type="faq_reply",
entity_id=reply.id, entity_id=reply.id,
action="CREATE_FAQ_REPLY", action="CREATE_FAQ_REPLY",
detail="FAQ 已回复", detail=json.dumps({"targetName": question_name, "description": f"回复医学咨询问题“{question_name}"}, ensure_ascii=False),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, item.study_id, current_user), operator_role=await get_operator_role_label(db, item.study_id, current_user),
) )
@@ -369,6 +380,7 @@ async def delete_faq(
item = await faq_crud.get_item(db, item_id) item = await faq_crud.get_item(db, item_id)
if not item: if not item:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="FAQ 不存在") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="FAQ 不存在")
question_name = _compact_text(item.question)
await reply_crud.delete_replies_by_faq_id(db, item.id) await reply_crud.delete_replies_by_faq_id(db, item.id)
await db.delete(item) await db.delete(item)
await db.commit() await db.commit()
@@ -378,7 +390,7 @@ async def delete_faq(
entity_type="faq_item", entity_type="faq_item",
entity_id=item_id, entity_id=item_id,
action="DELETE_FAQ_ITEM", action="DELETE_FAQ_ITEM",
detail="FAQ 已删除", detail=json.dumps({"targetName": question_name, "description": f"删除医学咨询问题“{question_name}"}, ensure_ascii=False),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, item.study_id, current_user), operator_role=await get_operator_role_label(db, item.study_id, current_user),
) )
@@ -403,6 +415,7 @@ async def delete_reply(
item = await faq_crud.get_item(db, item_id) item = await faq_crud.get_item(db, item_id)
if not item: if not item:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="FAQ 不存在") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="FAQ 不存在")
question_name = _compact_text(item.question)
reply = await reply_crud.get_reply(db, reply_id) reply = await reply_crud.get_reply(db, reply_id)
if not reply or reply.faq_id != item.id: if not reply or reply.faq_id != item.id:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="回复不存在") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="回复不存在")
@@ -431,7 +444,7 @@ async def delete_reply(
entity_type="faq_reply", entity_type="faq_reply",
entity_id=reply_id, entity_id=reply_id,
action="DELETE_FAQ_REPLY", action="DELETE_FAQ_REPLY",
detail="FAQ 回复已删除", detail=json.dumps({"targetName": question_name, "description": f"删除医学咨询问题“{question_name}”的回复"}, ensure_ascii=False),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, item.study_id, current_user), operator_role=await get_operator_role_label(db, item.study_id, current_user),
) )
+37 -6
View File
@@ -1,4 +1,5 @@
import uuid import uuid
import json
from decimal import Decimal from decimal import Decimal
from typing import Any from typing import Any
@@ -52,6 +53,34 @@ async def _ensure_center_active(db: AsyncSession, study_id: uuid.UUID, center_id
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="中心已停用") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="中心已停用")
async def _contract_audit_name(db: AsyncSession, contract) -> str:
site = await site_crud.get_site(db, contract.center_id)
center_name = site.name if site else "中心"
contract_no = str(contract.contract_no or "").strip()
return f"{center_name} / {contract_no}" if contract_no else center_name
async def _contract_audit_detail(db: AsyncSession, action: str, contract) -> str:
name = await _contract_audit_name(db, contract)
return json.dumps({"targetName": name, "description": f"{action}合同费用“{name}"}, ensure_ascii=False)
async def _payment_audit_name(db: AsyncSession, contract, payment) -> str:
contract_name = await _contract_audit_name(db, contract)
seq = getattr(payment, "seq", None)
amount = getattr(payment, "amount", None)
if seq:
return f"{contract_name} / 第{seq}"
if amount is not None:
return f"{contract_name} / {amount}"
return contract_name
async def _payment_audit_detail(db: AsyncSession, action: str, contract, payment) -> str:
name = await _payment_audit_name(db, contract, payment)
return json.dumps({"targetName": name, "description": f"{action}合同费用分期“{name}"}, ensure_ascii=False)
def _parse_optional_uuid(value: str | None, detail: str) -> uuid.UUID | None: def _parse_optional_uuid(value: str | None, detail: str) -> uuid.UUID | None:
if not value: if not value:
return None return None
@@ -261,7 +290,7 @@ async def create_contract_fee(
entity_type="contract_fee", entity_type="contract_fee",
entity_id=contract.id, entity_id=contract.id,
action="CREATE_CONTRACT_FEE", action="CREATE_CONTRACT_FEE",
detail="合同费用已创建", detail=await _contract_audit_detail(db, "创建", contract),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, contract.study_id, current_user), operator_role=await get_operator_role_label(db, contract.study_id, current_user),
) )
@@ -291,7 +320,7 @@ async def update_contract_fee(
entity_type="contract_fee", entity_type="contract_fee",
entity_id=contract_id, entity_id=contract_id,
action="UPDATE_CONTRACT_FEE", action="UPDATE_CONTRACT_FEE",
detail="合同费用已更新", detail=await _contract_audit_detail(db, "更新", contract),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, contract.study_id, current_user), operator_role=await get_operator_role_label(db, contract.study_id, current_user),
) )
@@ -313,6 +342,7 @@ async def delete_contract_fee(
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="合同费用不存在") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="合同费用不存在")
await _ensure_study_access(db, contract.study_id, current_user, "fees_contracts:delete") await _ensure_study_access(db, contract.study_id, current_user, "fees_contracts:delete")
await _ensure_center_active(db, contract.study_id, contract.center_id) await _ensure_center_active(db, contract.study_id, contract.center_id)
contract_detail = await _contract_audit_detail(db, "删除", contract)
await contract_fee_crud.delete_contract_fee(db, contract) await contract_fee_crud.delete_contract_fee(db, contract)
await audit_crud.log_action( await audit_crud.log_action(
db, db,
@@ -320,7 +350,7 @@ async def delete_contract_fee(
entity_type="contract_fee", entity_type="contract_fee",
entity_id=contract_id, entity_id=contract_id,
action="DELETE_CONTRACT_FEE", action="DELETE_CONTRACT_FEE",
detail="合同费用已删除", detail=contract_detail,
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, contract.study_id, current_user), operator_role=await get_operator_role_label(db, contract.study_id, current_user),
) )
@@ -350,7 +380,7 @@ async def create_contract_payment(
entity_type="contract_fee_payment", entity_type="contract_fee_payment",
entity_id=payment.id, entity_id=payment.id,
action="CREATE_CONTRACT_FEE_PAYMENT", action="CREATE_CONTRACT_FEE_PAYMENT",
detail="合同费用分期已创建", detail=await _payment_audit_detail(db, "创建", contract, payment),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, contract.study_id, current_user), operator_role=await get_operator_role_label(db, contract.study_id, current_user),
) )
@@ -391,7 +421,7 @@ async def update_contract_payment(
entity_type="contract_fee_payment", entity_type="contract_fee_payment",
entity_id=payment_id, entity_id=payment_id,
action="UPDATE_CONTRACT_FEE_PAYMENT", action="UPDATE_CONTRACT_FEE_PAYMENT",
detail="合同费用分期已更新", detail=await _payment_audit_detail(db, "更新", contract, payment),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, contract.study_id, current_user), operator_role=await get_operator_role_label(db, contract.study_id, current_user),
) )
@@ -415,6 +445,7 @@ async def delete_contract_payment(
if not contract: if not contract:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="合同费用不存在") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="合同费用不存在")
await _ensure_study_access(db, contract.study_id, current_user, "fees_contracts:update") await _ensure_study_access(db, contract.study_id, current_user, "fees_contracts:update")
payment_detail = await _payment_audit_detail(db, "删除", contract, payment)
await payment_crud.delete_payment(db, payment) await payment_crud.delete_payment(db, payment)
await payment_crud.resequence_payments(db, contract.id) await payment_crud.resequence_payments(db, contract.id)
await audit_crud.log_action( await audit_crud.log_action(
@@ -423,7 +454,7 @@ async def delete_contract_payment(
entity_type="contract_fee_payment", entity_type="contract_fee_payment",
entity_id=payment_id, entity_id=payment_id,
action="DELETE_CONTRACT_FEE_PAYMENT", action="DELETE_CONTRACT_FEE_PAYMENT",
detail="合同费用分期已删除", detail=payment_detail,
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, contract.study_id, current_user), operator_role=await get_operator_role_label(db, contract.study_id, current_user),
) )
+17 -3
View File
@@ -1,4 +1,5 @@
import uuid import uuid
import json
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
@@ -24,6 +25,18 @@ def _validate_calibration(need_calibration: bool, calibration_cycle_days: int |
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="需要校准时校准周期必须大于0") raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="需要校准时校准周期必须大于0")
def _equipment_audit_name(item) -> str:
parts = [item.name, item.spec_model, item.brand]
return " / ".join(str(part).strip() for part in parts if str(part or "").strip()) or "设备记录"
def _equipment_audit_detail(action: str, item) -> str:
return json.dumps(
{"targetName": _equipment_audit_name(item), "description": f"{action}设备“{_equipment_audit_name(item)}"},
ensure_ascii=False,
)
@router.post( @router.post(
"/equipment", "/equipment",
response_model=MaterialEquipmentRead, response_model=MaterialEquipmentRead,
@@ -47,7 +60,7 @@ async def create_equipment(
entity_type="material_equipment", entity_type="material_equipment",
entity_id=item.id, entity_id=item.id,
action="CREATE_MATERIAL_EQUIPMENT", action="CREATE_MATERIAL_EQUIPMENT",
detail=f"设备 {item.id} 已创建", detail=_equipment_audit_detail("创建", item),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -118,7 +131,7 @@ async def update_equipment(
entity_type="material_equipment", entity_type="material_equipment",
entity_id=equipment_id, entity_id=equipment_id,
action="UPDATE_MATERIAL_EQUIPMENT", action="UPDATE_MATERIAL_EQUIPMENT",
detail=f"设备 {equipment_id} 已更新", detail=_equipment_audit_detail("更新", item),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -140,6 +153,7 @@ async def delete_equipment(
item = await equipment_crud.get_equipment(db, equipment_id) item = await equipment_crud.get_equipment(db, equipment_id)
if not item or item.study_id != study_id: if not item or item.study_id != study_id:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="设备记录不存在") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="设备记录不存在")
equipment_detail = _equipment_audit_detail("删除", item)
await equipment_crud.delete_equipment(db, item) await equipment_crud.delete_equipment(db, item)
await audit_crud.log_action( await audit_crud.log_action(
db, db,
@@ -147,7 +161,7 @@ async def delete_equipment(
entity_type="material_equipment", entity_type="material_equipment",
entity_id=equipment_id, entity_id=equipment_id,
action="DELETE_MATERIAL_EQUIPMENT", action="DELETE_MATERIAL_EQUIPMENT",
detail=f"设备 {equipment_id} 已删除", detail=equipment_detail,
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
+29 -4
View File
@@ -43,6 +43,20 @@ def _role_rank(role: str | None) -> int:
return PROJECT_ROLE_RANK.get(role or "", 0) return PROJECT_ROLE_RANK.get(role or "", 0)
def _user_audit_name(user, user_id: uuid.UUID) -> str:
if user:
for attr in ("full_name", "username", "email"):
value = getattr(user, attr, None)
if value:
return str(value)
return str(user_id)
async def _member_audit_name(db: AsyncSession, user_id: uuid.UUID) -> str:
user = await user_crud.get_by_id(db, user_id)
return _user_audit_name(user, user_id)
async def _ensure_member_mutation_allowed( async def _ensure_member_mutation_allowed(
db: AsyncSession, db: AsyncSession,
study_id: uuid.UUID, study_id: uuid.UUID,
@@ -116,7 +130,7 @@ async def add_member(
entity_id=updated.id, entity_id=updated.id,
action="PROJECT_MEMBER_UPDATED", action="PROJECT_MEMBER_UPDATED",
detail=json.dumps({ detail=json.dumps({
"targetName": str(updated.user_id), "targetName": await _member_audit_name(db, updated.user_id),
"before": {"is_active": existing.is_active, "role_in_study": existing.role_in_study}, "before": {"is_active": existing.is_active, "role_in_study": existing.role_in_study},
"after": {"is_active": updated.is_active, "role_in_study": updated.role_in_study}, "after": {"is_active": updated.is_active, "role_in_study": updated.role_in_study},
}, ensure_ascii=False), }, ensure_ascii=False),
@@ -132,7 +146,10 @@ async def add_member(
entity_type="study_member", entity_type="study_member",
entity_id=member.id, entity_id=member.id,
action="PROJECT_MEMBER_ADDED", action="PROJECT_MEMBER_ADDED",
detail=json.dumps({"targetName": str(member.user_id), "after": {"role_in_study": member.role_in_study, "is_active": member.is_active}}, ensure_ascii=False), detail=json.dumps({
"targetName": await _member_audit_name(db, member.user_id),
"after": {"role_in_study": member.role_in_study, "is_active": member.is_active},
}, ensure_ascii=False),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await _operator_project_role(db, study_id, current_user), operator_role=await _operator_project_role(db, study_id, current_user),
) )
@@ -247,7 +264,11 @@ async def update_member(
entity_type="study_member", entity_type="study_member",
entity_id=updated.id, entity_id=updated.id,
action="PROJECT_MEMBER_UPDATED", action="PROJECT_MEMBER_UPDATED",
detail=json.dumps({"targetName": str(updated.user_id), "before": before_data, "after": after_data}, ensure_ascii=False), detail=json.dumps({
"targetName": await _member_audit_name(db, updated.user_id),
"before": before_data,
"after": after_data,
}, ensure_ascii=False),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await _operator_project_role(db, study_id, current_user), operator_role=await _operator_project_role(db, study_id, current_user),
) )
@@ -288,7 +309,11 @@ async def remove_member(
entity_type="study_member", entity_type="study_member",
entity_id=removed.id, entity_id=removed.id,
action="PROJECT_MEMBER_REMOVED", action="PROJECT_MEMBER_REMOVED",
detail=json.dumps({"targetName": str(removed.user_id), "before": before_data, "after": {"is_active": removed.is_active}}, ensure_ascii=False), detail=json.dumps({
"targetName": _user_audit_name(target_user, removed.user_id),
"before": before_data,
"after": {"is_active": removed.is_active},
}, ensure_ascii=False),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await _operator_project_role(db, study_id, current_user), operator_role=await _operator_project_role(db, study_id, current_user),
) )
+102 -18
View File
@@ -14,13 +14,13 @@ from fastapi import APIRouter, Depends, HTTPException, Query, status
from sqlalchemy import func, select, desc from sqlalchemy import func, select, desc
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.core.deps import get_current_user, get_db_session, is_system_admin, list_active_pm_study_ids from app.core.deps import get_current_user, get_db_session, is_system_admin
from app.core.permission_monitor import get_permission_monitor from app.core.permission_monitor import get_permission_monitor
from app.models.permission_access_log import PermissionAccessLog from app.models.permission_access_log import PermissionAccessLog
from app.models.permission_metric_snapshot import PermissionMetricSnapshot from app.models.permission_metric_snapshot import PermissionMetricSnapshot
from app.models.security_access_log import SecurityAccessLog from app.models.security_access_log import SecurityAccessLog
from app.models.user import User from app.models.user import User
from app.services.ip_location import resolve_ip_location from app.services.ip_location import IpLocation, resolve_ip_location
router = APIRouter(prefix="/permission-monitoring", tags=["permission-monitoring"]) router = APIRouter(prefix="/permission-monitoring", tags=["permission-monitoring"])
@@ -39,10 +39,7 @@ class MonitoringScope:
async def resolve_monitoring_scope(db: AsyncSession, current_user) -> MonitoringScope: async def resolve_monitoring_scope(db: AsyncSession, current_user) -> MonitoringScope:
if is_system_admin(current_user): if is_system_admin(current_user):
return MonitoringScope(is_admin=True, study_ids=set()) return MonitoringScope(is_admin=True, study_ids=set())
return MonitoringScope( raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
is_admin=False,
study_ids=await list_active_pm_study_ids(db, current_user.id),
)
def _apply_monitoring_scope_to_log_query(query, scope: MonitoringScope): def _apply_monitoring_scope_to_log_query(query, scope: MonitoringScope):
@@ -269,17 +266,13 @@ async def get_access_logs(
).select_from(PermissionAccessLog) ).select_from(PermissionAccessLog)
user_stats_query = ( user_stats_query = (
select( select(
PermissionAccessLog.user_id,
User.full_name,
PermissionAccessLog.role,
PermissionAccessLog.ip_address.label("sample_ip_address"), PermissionAccessLog.ip_address.label("sample_ip_address"),
func.count().label("total_count"), func.count().label("total_count"),
func.count().filter(PermissionAccessLog.allowed.is_(False)).label("denied_count"), func.count().filter(PermissionAccessLog.allowed.is_(False)).label("denied_count"),
func.count(func.distinct(PermissionAccessLog.ip_address)).label("unique_ip_count"), func.count(func.distinct(PermissionAccessLog.ip_address)).label("unique_ip_count"),
func.max(PermissionAccessLog.created_at).label("last_seen_at"), func.max(PermissionAccessLog.created_at).label("last_seen_at"),
) )
.outerjoin(User, PermissionAccessLog.user_id == User.id) .group_by(PermissionAccessLog.ip_address)
.group_by(PermissionAccessLog.ip_address, PermissionAccessLog.user_id, User.full_name, PermissionAccessLog.role)
.order_by(desc("total_count"), desc("last_seen_at")) .order_by(desc("total_count"), desc("last_seen_at"))
.limit(10) .limit(10)
) )
@@ -295,6 +288,26 @@ async def get_access_logs(
summary_row = summary_result.one() summary_row = summary_result.one()
user_stats_result = await db.execute(user_stats_query) user_stats_result = await db.execute(user_stats_query)
user_stats_rows = user_stats_result.all() user_stats_rows = user_stats_result.all()
latest_user_by_ip = {}
if user_stats_rows:
ranked_ips = [stat.sample_ip_address for stat in user_stats_rows if stat.sample_ip_address]
latest_user_query = (
select(
PermissionAccessLog.ip_address,
PermissionAccessLog.user_id,
User.full_name,
PermissionAccessLog.role,
PermissionAccessLog.created_at,
)
.outerjoin(User, PermissionAccessLog.user_id == User.id)
.where(PermissionAccessLog.ip_address.in_(ranked_ips))
.order_by(PermissionAccessLog.ip_address, desc(PermissionAccessLog.created_at))
)
for condition in conditions:
latest_user_query = latest_user_query.where(condition)
latest_user_result = await db.execute(latest_user_query)
for row in latest_user_result.all():
latest_user_by_ip.setdefault(row.ip_address, row)
query = query.order_by(desc(PermissionAccessLog.created_at)) query = query.order_by(desc(PermissionAccessLog.created_at))
query = query.offset((page - 1) * page_size).limit(page_size) query = query.offset((page - 1) * page_size).limit(page_size)
@@ -326,11 +339,12 @@ async def get_access_logs(
user_stats = [] user_stats = []
for stat in user_stats_rows: for stat in user_stats_rows:
sample_location = resolve_ip_location(stat.sample_ip_address) sample_location = resolve_ip_location(stat.sample_ip_address)
latest_user = latest_user_by_ip.get(stat.sample_ip_address)
user_stats.append( user_stats.append(
{ {
"user_id": str(stat.user_id), "user_id": str(latest_user.user_id) if latest_user else "",
"user_name": stat.full_name or "未知用户", "user_name": latest_user.full_name if latest_user and latest_user.full_name else "未知用户",
"role": stat.role, "role": latest_user.role if latest_user else "",
"total_count": stat.total_count, "total_count": stat.total_count,
"denied_count": stat.denied_count, "denied_count": stat.denied_count,
"unique_ip_count": stat.unique_ip_count, "unique_ip_count": stat.unique_ip_count,
@@ -364,6 +378,43 @@ def _security_account_label(auth_status: str, user_identifier: str | None, user_
return "未知账号" return "未知账号"
SENSITIVE_PROBE_MARKERS = (
"/.env",
".env",
"/.git",
".git/config",
"backup",
"config.php",
"wp-config",
"database.yml",
)
CHINA_IP_COUNTRY_LABELS = {"中国", "China", "Mainland China", "中国香港", "中国澳门", "中国台湾"}
def _is_non_china_ip(ip_location: IpLocation) -> bool:
country = (ip_location.country or "").strip()
return bool(country) and country not in CHINA_IP_COUNTRY_LABELS and not country.startswith("中国")
def _classify_security_access_log(log: SecurityAccessLog, ip_location: IpLocation | None = None) -> dict[str, str]:
path = (log.path or "").lower()
if any(marker in path for marker in SENSITIVE_PROBE_MARKERS):
return {"category": "PROBE", "severity": "CRITICAL"}
if ip_location and _is_non_china_ip(ip_location):
return {"category": "ABNORMAL_IP", "severity": "HIGH"}
if log.status_code >= 500:
return {"category": "SERVER_ERROR", "severity": "HIGH"}
if log.auth_status == "INVALID_TOKEN":
return {"category": "INVALID_TOKEN", "severity": "MEDIUM"}
if log.auth_status == "ANONYMOUS" and log.status_code in {401, 403}:
return {"category": "ANONYMOUS_API", "severity": "MEDIUM"}
if log.status_code == 404:
return {"category": "NOT_FOUND_NOISE", "severity": "LOW"}
return {"category": "OTHER", "severity": "LOW"}
@router.get("/security-logs", status_code=status.HTTP_200_OK) @router.get("/security-logs", status_code=status.HTTP_200_OK)
async def get_security_access_logs( async def get_security_access_logs(
db: AsyncSession = Depends(get_db_session), db: AsyncSession = Depends(get_db_session),
@@ -421,6 +472,7 @@ async def get_security_access_logs(
items = [] items = []
for log in logs: for log in logs:
ip_location = resolve_ip_location(log.client_ip)
items.append( items.append(
{ {
"id": str(log.id), "id": str(log.id),
@@ -429,10 +481,16 @@ async def get_security_access_logs(
"status_code": log.status_code, "status_code": log.status_code,
"elapsed_ms": round(log.elapsed_ms, 2), "elapsed_ms": round(log.elapsed_ms, 2),
"client_ip": log.client_ip, "client_ip": log.client_ip,
"ip_location": ip_location.location,
"ip_country": ip_location.country,
"ip_province": ip_location.province,
"ip_city": ip_location.city,
"ip_isp": ip_location.isp,
"user_agent": log.user_agent, "user_agent": log.user_agent,
"auth_status": log.auth_status, "auth_status": log.auth_status,
"user_identifier": log.user_identifier, "user_identifier": log.user_identifier,
"account_label": _security_account_label(log.auth_status, log.user_identifier, user_names), "account_label": _security_account_label(log.auth_status, log.user_identifier, user_names),
**_classify_security_access_log(log, ip_location),
"created_at": log.created_at.isoformat(), "created_at": log.created_at.isoformat(),
} }
) )
@@ -626,14 +684,26 @@ async def get_ip_locations(
) )
) )
result = await db.execute(_apply_monitoring_scope_to_log_query(query, scope)) result = await db.execute(_apply_monitoring_scope_to_log_query(query, scope))
security_result = await db.execute(
select(
SecurityAccessLog.client_ip,
SecurityAccessLog.user_identifier,
SecurityAccessLog.auth_status,
SecurityAccessLog.status_code,
).where(
SecurityAccessLog.created_at >= start_time,
SecurityAccessLog.client_ip.is_not(None),
)
)
buckets: dict[tuple[str, str, str], dict] = {} buckets: dict[tuple[str, str, str], dict] = {}
all_ip_addresses: set[str] = set() all_ip_addresses: set[str] = set()
all_user_ids: set[uuid.UUID] = set() all_user_ids: set[str] = set()
total_count = 0 total_count = 0
allowed_count = 0 allowed_count = 0
denied_count = 0 denied_count = 0
for ip_address, user_id, allowed in result.all(): def add_ip_location_row(ip_address: str, user_id: str | uuid.UUID | None, allowed: bool) -> None:
nonlocal total_count, allowed_count, denied_count
ip_info = resolve_ip_location(ip_address) ip_info = resolve_ip_location(ip_address)
key = (ip_info.country, ip_info.province, ip_info.city) key = (ip_info.country, ip_info.province, ip_info.city)
location = " / ".join(part for part in [ip_info.country, ip_info.province, ip_info.city] if part) or ip_info.location or "未知" location = " / ".join(part for part in [ip_info.country, ip_info.province, ip_info.city] if part) or ip_info.location or "未知"
@@ -655,14 +725,28 @@ async def get_ip_locations(
bucket["total_count"] += 1 bucket["total_count"] += 1
bucket["allowed_count" if allowed else "denied_count"] += 1 bucket["allowed_count" if allowed else "denied_count"] += 1
bucket["ip_addresses"].add(ip_address) bucket["ip_addresses"].add(ip_address)
bucket["user_ids"].add(user_id) if user_id:
bucket["user_ids"].add(str(user_id))
total_count += 1 total_count += 1
if allowed: if allowed:
allowed_count += 1 allowed_count += 1
else: else:
denied_count += 1 denied_count += 1
all_ip_addresses.add(ip_address) all_ip_addresses.add(ip_address)
all_user_ids.add(user_id) if user_id:
all_user_ids.add(str(user_id))
for ip_address, user_id, allowed in result.all():
add_ip_location_row(ip_address, user_id, allowed)
for client_ip, user_identifier, auth_status, status_code in security_result.all():
user_id = None
if auth_status == "AUTHENTICATED" and user_identifier:
try:
user_id = uuid.UUID(user_identifier)
except ValueError:
user_id = user_identifier
add_ip_location_row(client_ip, user_id, status_code < 400)
items = sorted(buckets.values(), key=lambda item: item["total_count"], reverse=True)[:limit] items = sorted(buckets.values(), key=lambda item: item["total_count"], reverse=True)[:limit]
return { return {
+10 -3
View File
@@ -1,4 +1,5 @@
import uuid import uuid
import json
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
@@ -28,6 +29,11 @@ async def _ensure_site_name_active(db: AsyncSession, study_id: uuid.UUID, site_n
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="中心已停用") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="中心已停用")
def _precaution_audit_detail(action: str, precaution) -> str:
title = str(precaution.title or "").strip() or "注意事项"
return json.dumps({"targetName": title, "description": f"{action}注意事项“{title}"}, ensure_ascii=False)
@router.post( @router.post(
"/precautions", "/precautions",
response_model=PrecautionRead, response_model=PrecautionRead,
@@ -49,7 +55,7 @@ async def create_precaution(
entity_type="precaution", entity_type="precaution",
entity_id=precaution.id, entity_id=precaution.id,
action="CREATE_PRECAUTION", action="CREATE_PRECAUTION",
detail=f"注意事项 {precaution.title} 已创建", detail=_precaution_audit_detail("创建", precaution),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -115,7 +121,7 @@ async def update_precaution(
entity_type="precaution", entity_type="precaution",
entity_id=precaution_id, entity_id=precaution_id,
action="UPDATE_PRECAUTION", action="UPDATE_PRECAUTION",
detail=f"注意事项 {precaution_id} 已更新", detail=_precaution_audit_detail("更新", precaution),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -138,6 +144,7 @@ async def delete_precaution(
if not precaution or precaution.study_id != study_id: if not precaution or precaution.study_id != study_id:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="注意事项不存在") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="注意事项不存在")
await _ensure_site_name_active(db, study_id, precaution.site_name) await _ensure_site_name_active(db, study_id, precaution.site_name)
precaution_detail = _precaution_audit_detail("删除", precaution)
await precaution_crud.delete_precaution(db, precaution) await precaution_crud.delete_precaution(db, precaution)
await audit_crud.log_action( await audit_crud.log_action(
db, db,
@@ -145,7 +152,7 @@ async def delete_precaution(
entity_type="precaution", entity_type="precaution",
entity_id=precaution_id, entity_id=precaution_id,
action="DELETE_PRECAUTION", action="DELETE_PRECAUTION",
detail=f"注意事项 {precaution_id} 已删除", detail=precaution_detail,
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
+50 -11
View File
@@ -1,4 +1,5 @@
import uuid import uuid
import json
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
@@ -49,6 +50,41 @@ async def _ensure_site_active(db: AsyncSession, site_id: uuid.UUID | None):
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="中心已停用") raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="中心已停用")
async def _site_label(db: AsyncSession, site_id: uuid.UUID | None, fallback: str = "项目级") -> str:
if not site_id:
return fallback
site = await site_crud.get_site(db, site_id)
return site.name if site else fallback
async def _feasibility_audit_detail(db: AsyncSession, action: str, record) -> str:
site_name = await _site_label(db, record.site_id)
project_no = str(record.project_no or "").strip()
name = f"{site_name} / {project_no}" if project_no else site_name
return json.dumps({"targetName": name, "description": f"{action}立项记录“{name}"}, ensure_ascii=False)
async def _ethics_audit_detail(db: AsyncSession, action: str, record) -> str:
site_name = await _site_label(db, record.site_id)
approval_no = str(record.approval_no or "").strip()
name = f"{site_name} / {approval_no}" if approval_no else site_name
return json.dumps({"targetName": name, "description": f"{action}伦理记录“{name}"}, ensure_ascii=False)
async def _kickoff_audit_detail(db: AsyncSession, action: str, meeting) -> str:
site_name = await _site_label(db, meeting.site_id)
date_text = meeting.kickoff_date.isoformat() if meeting.kickoff_date else ""
name = f"{site_name} / {date_text}" if date_text else site_name
return json.dumps({"targetName": name, "description": f"{action}启动会记录“{name}"}, ensure_ascii=False)
def _training_audit_detail(action: str, record) -> str:
name = str(record.name or "").strip() or "培训授权人员"
site_name = str(record.site_name or "").strip()
target_name = f"{name} / {site_name}" if site_name else name
return json.dumps({"targetName": target_name, "description": f"{action}培训授权人员“{target_name}"}, ensure_ascii=False)
@router.post( @router.post(
"/feasibility", "/feasibility",
response_model=StartupFeasibilityRead, response_model=StartupFeasibilityRead,
@@ -73,7 +109,7 @@ async def create_feasibility(
entity_type="startup_feasibility", entity_type="startup_feasibility",
entity_id=record.id, entity_id=record.id,
action="CREATE_STARTUP_FEASIBILITY", action="CREATE_STARTUP_FEASIBILITY",
detail="立项记录已创建", detail=await _feasibility_audit_detail(db, "创建", record),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -147,7 +183,7 @@ async def update_feasibility(
entity_type="startup_feasibility", entity_type="startup_feasibility",
entity_id=record_id, entity_id=record_id,
action="UPDATE_STARTUP_FEASIBILITY", action="UPDATE_STARTUP_FEASIBILITY",
detail="立项记录已更新", detail=await _feasibility_audit_detail(db, "更新", record),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -173,6 +209,7 @@ async def delete_feasibility(
if cra_scope and record.site_id not in cra_scope[0]: if cra_scope and record.site_id not in cra_scope[0]:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足") raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
await _ensure_site_active(db, record.site_id) await _ensure_site_active(db, record.site_id)
record_detail = await _feasibility_audit_detail(db, "删除", record)
await startup_crud.delete_feasibility(db, record) await startup_crud.delete_feasibility(db, record)
await audit_crud.log_action( await audit_crud.log_action(
db, db,
@@ -180,7 +217,7 @@ async def delete_feasibility(
entity_type="startup_feasibility", entity_type="startup_feasibility",
entity_id=record_id, entity_id=record_id,
action="DELETE_STARTUP_FEASIBILITY", action="DELETE_STARTUP_FEASIBILITY",
detail="立项记录已删除", detail=record_detail,
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -210,7 +247,7 @@ async def create_ethics(
entity_type="startup_ethics", entity_type="startup_ethics",
entity_id=record.id, entity_id=record.id,
action="CREATE_STARTUP_ETHICS", action="CREATE_STARTUP_ETHICS",
detail="伦理记录已创建", detail=await _ethics_audit_detail(db, "创建", record),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -284,7 +321,7 @@ async def update_ethics(
entity_type="startup_ethics", entity_type="startup_ethics",
entity_id=record_id, entity_id=record_id,
action="UPDATE_STARTUP_ETHICS", action="UPDATE_STARTUP_ETHICS",
detail="伦理记录已更新", detail=await _ethics_audit_detail(db, "更新", record),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -310,6 +347,7 @@ async def delete_ethics(
if cra_scope and record.site_id not in cra_scope[0]: if cra_scope and record.site_id not in cra_scope[0]:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足") raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
await _ensure_site_active(db, record.site_id) await _ensure_site_active(db, record.site_id)
record_detail = await _ethics_audit_detail(db, "删除", record)
await startup_crud.delete_ethics(db, record) await startup_crud.delete_ethics(db, record)
await audit_crud.log_action( await audit_crud.log_action(
db, db,
@@ -317,7 +355,7 @@ async def delete_ethics(
entity_type="startup_ethics", entity_type="startup_ethics",
entity_id=record_id, entity_id=record_id,
action="DELETE_STARTUP_ETHICS", action="DELETE_STARTUP_ETHICS",
detail="伦理记录已删除", detail=record_detail,
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -347,7 +385,7 @@ async def create_kickoff(
entity_type="startup_kickoff", entity_type="startup_kickoff",
entity_id=meeting.id, entity_id=meeting.id,
action="CREATE_KICKOFF_MEETING", action="CREATE_KICKOFF_MEETING",
detail="启动会记录已创建", detail=await _kickoff_audit_detail(db, "创建", meeting),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -421,7 +459,7 @@ async def update_kickoff(
entity_type="startup_kickoff", entity_type="startup_kickoff",
entity_id=meeting_id, entity_id=meeting_id,
action="UPDATE_KICKOFF_MEETING", action="UPDATE_KICKOFF_MEETING",
detail="启动会记录已更新", detail=await _kickoff_audit_detail(db, "更新", meeting),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -452,7 +490,7 @@ async def create_training_authorization(
entity_type="training_authorization", entity_type="training_authorization",
entity_id=record.id, entity_id=record.id,
action="CREATE_TRAINING_AUTH", action="CREATE_TRAINING_AUTH",
detail="培训授权人员已创建", detail=_training_audit_detail("创建", record),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -527,7 +565,7 @@ async def update_training_authorization(
entity_type="training_authorization", entity_type="training_authorization",
entity_id=record_id, entity_id=record_id,
action="UPDATE_TRAINING_AUTH", action="UPDATE_TRAINING_AUTH",
detail="培训授权人员已更新", detail=_training_audit_detail("更新", record),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -553,6 +591,7 @@ async def delete_training_authorization(
if cra_scope and record.site_name not in cra_scope[1]: if cra_scope and record.site_name not in cra_scope[1]:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足") raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
await _ensure_site_name_active(db, study_id, record.site_name) await _ensure_site_name_active(db, study_id, record.site_name)
record_detail = _training_audit_detail("删除", record)
await startup_crud.delete_training_authorization(db, record) await startup_crud.delete_training_authorization(db, record)
await audit_crud.log_action( await audit_crud.log_action(
db, db,
@@ -560,7 +599,7 @@ async def delete_training_authorization(
entity_type="training_authorization", entity_type="training_authorization",
entity_id=record_id, entity_id=record_id,
action="DELETE_TRAINING_AUTH", action="DELETE_TRAINING_AUTH",
detail="培训授权人员已删除", detail=record_detail,
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
+5 -6
View File
@@ -610,7 +610,6 @@ _SETUP_MODULE_LABELS = {
} }
_SETUP_FIELD_LABELS = { _SETUP_FIELD_LABELS = {
"id": "ID",
"name": "名称", "name": "名称",
"milestone": "里程碑", "milestone": "里程碑",
"planDate": "计划日期", "planDate": "计划日期",
@@ -634,6 +633,8 @@ _SETUP_FIELD_LABELS = {
"confirmDate": "确认日期", "confirmDate": "确认日期",
} }
_SETUP_TECHNICAL_FIELDS = {"id"}
def _setup_value_text(value: Any) -> str: def _setup_value_text(value: Any) -> str:
if value is None: if value is None:
@@ -690,6 +691,8 @@ def _setup_collect_diff_lines(
new_dict = new_value if isinstance(new_value, dict) else {} new_dict = new_value if isinstance(new_value, dict) else {}
keys = sorted(set(old_dict.keys()) | set(new_dict.keys())) keys = sorted(set(old_dict.keys()) | set(new_dict.keys()))
for key in keys: for key in keys:
if key in _SETUP_TECHNICAL_FIELDS:
continue
_setup_collect_diff_lines(module_key, old_dict.get(key), new_dict.get(key), [*path, key], lines) _setup_collect_diff_lines(module_key, old_dict.get(key), new_dict.get(key), [*path, key], lines)
return return
@@ -718,11 +721,7 @@ def _top_level_diff_summary(old: dict | None, new: dict | None) -> str:
if not detail_lines: if not detail_lines:
return module_summary return module_summary
max_lines = 20 return f"{module_summary}; 变更明细:" + "".join(detail_lines)
visible_lines = detail_lines[:max_lines]
if len(detail_lines) > max_lines:
visible_lines.append(f"其余 {len(detail_lines) - max_lines} 项变更已省略")
return f"{module_summary}; 变更明细:" + "".join(visible_lines)
@router.post("/", response_model=StudyRead, status_code=status.HTTP_201_CREATED, dependencies=[Depends(require_roles(["ADMIN"]))]) @router.post("/", response_model=StudyRead, status_code=status.HTTP_201_CREATED, dependencies=[Depends(require_roles(["ADMIN"]))])
+25 -3
View File
@@ -1,4 +1,5 @@
import uuid import uuid
import json
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
@@ -14,6 +15,26 @@ from app.schemas.subject_history import SubjectHistoryCreate, SubjectHistoryRead
router = APIRouter() router = APIRouter()
def _compact_text(value: str | None, max_length: int = 30) -> str:
text = " ".join(str(value or "").split())
if len(text) <= max_length:
return text
return f"{text[:max_length]}..."
def _history_audit_name(subject, history) -> str:
subject_no = getattr(subject, "subject_no", None) or "参与者"
record_date = getattr(history, "record_date", None)
content = _compact_text(getattr(history, "content", None))
suffix = record_date.isoformat() if record_date else content
return f"{subject_no} / {suffix}" if suffix else subject_no
def _history_audit_detail(action: str, subject, history) -> str:
name = _history_audit_name(subject, history)
return json.dumps({"targetName": name, "description": f"{action}病史记录“{name}"}, ensure_ascii=False)
async def _ensure_study_exists(db: AsyncSession, study_id: uuid.UUID): async def _ensure_study_exists(db: AsyncSession, study_id: uuid.UUID):
study = await study_crud.get(db, study_id) study = await study_crud.get(db, study_id)
if not study: if not study:
@@ -56,7 +77,7 @@ async def create_history(
entity_type="subject_history", entity_type="subject_history",
entity_id=history.id, entity_id=history.id,
action="CREATE_SUBJECT_HISTORY", action="CREATE_SUBJECT_HISTORY",
detail="病史记录已创建", detail=_history_audit_detail("创建", subject, history),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -131,7 +152,7 @@ async def update_history(
entity_type="subject_history", entity_type="subject_history",
entity_id=history_id, entity_id=history_id,
action="UPDATE_SUBJECT_HISTORY", action="UPDATE_SUBJECT_HISTORY",
detail="病史记录已更新", detail=_history_audit_detail("更新", subject, history),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -157,6 +178,7 @@ async def delete_history(
history = await history_crud.get_history(db, history_id) history = await history_crud.get_history(db, history_id)
if not history or history.study_id != study_id or history.subject_id != subject_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="病史记录不存在") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="病史记录不存在")
history_detail = _history_audit_detail("删除", subject, history)
await history_crud.delete_history(db, history) await history_crud.delete_history(db, history)
await audit_crud.log_action( await audit_crud.log_action(
db, db,
@@ -164,7 +186,7 @@ async def delete_history(
entity_type="subject_history", entity_type="subject_history",
entity_id=history_id, entity_id=history_id,
action="DELETE_SUBJECT_HISTORY", action="DELETE_SUBJECT_HISTORY",
detail="病史记录已删除", detail=history_detail,
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
+58 -7
View File
@@ -1,3 +1,4 @@
import json
import uuid import uuid
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, HTTPException, status
@@ -15,6 +16,29 @@ from app.schemas.subject import SubjectCreate, SubjectRead, SubjectUpdate
router = APIRouter() router = APIRouter()
def _format_date(value):
return value.isoformat() if value else None
async def _subject_audit_snapshot(db: AsyncSession, subject) -> dict:
site_name = None
if subject.site_id:
site = await site_crud.get_site(db, subject.site_id)
site_name = site.name if site else None
return {
"subject_no": subject.subject_no,
"site_name": site_name,
"status": subject.status,
"screening_date": _format_date(subject.screening_date),
"consent_date": _format_date(subject.consent_date),
"enrollment_date": _format_date(subject.enrollment_date),
"baseline_date": _format_date(subject.baseline_date),
"completion_date": _format_date(subject.completion_date),
"actual_medication_count": subject.actual_medication_count,
"drop_reason": subject.drop_reason,
}
async def _ensure_study_exists(db: AsyncSession, study_id: uuid.UUID): async def _ensure_study_exists(db: AsyncSession, study_id: uuid.UUID):
study = await study_crud.get(db, study_id) study = await study_crud.get(db, study_id)
if not study: if not study:
@@ -50,13 +74,22 @@ async def create_subject(
subject = await subject_crud.create_subject(db, study_id, subject_in) subject = await subject_crud.create_subject(db, study_id, subject_in)
except ValueError as exc: except ValueError as exc:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
after_snapshot = await _subject_audit_snapshot(db, subject)
await audit_crud.log_action( await audit_crud.log_action(
db, db,
study_id=study_id, study_id=study_id,
entity_type="subject", entity_type="subject",
entity_id=subject.id, entity_id=subject.id,
action="CREATE_SUBJECT", action="CREATE_SUBJECT",
detail=f"参与者 {subject.subject_no} 已创建", detail=json.dumps(
{
"targetName": subject.subject_no,
"description": f"创建参与者 {subject.subject_no}",
"before": None,
"after": after_snapshot,
},
ensure_ascii=False,
),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -172,21 +205,29 @@ async def update_subject(
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足") raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
await _ensure_subject_active(db, subject) await _ensure_subject_active(db, subject)
old_status = subject.status old_status = subject.status
before_snapshot = await _subject_audit_snapshot(db, subject)
updated = await subject_crud.update_subject(db, subject, subject_in) updated = await subject_crud.update_subject(db, subject, subject_in)
# 基线/治疗日期是访视计划的唯一推算基准,不能用入组日期替代。 # 基线/治疗日期是访视计划的唯一推算基准,不能用入组日期替代。
if updated.baseline_date: if updated.baseline_date:
await subject_crud.sync_visits_from_baseline(db, updated) await subject_crud.sync_visits_from_baseline(db, updated)
updated = await subject_crud.sync_subject_status(db, updated) updated = await subject_crud.sync_subject_status(db, updated)
detail = None after_snapshot = await _subject_audit_snapshot(db, updated)
if updated.status != old_status: description = f"变更参与者 {updated.subject_no} 状态" if updated.status != old_status else f"更新参与者 {updated.subject_no}"
detail = f"参与者 {updated.subject_no} 状态 {old_status} -> {updated.status}"
await audit_crud.log_action( await audit_crud.log_action(
db, db,
study_id=study_id, study_id=study_id,
entity_type="subject", entity_type="subject",
entity_id=subject_id, entity_id=subject_id,
action="SUBJECT_STATUS_CHANGE" if detail else "UPDATE_SUBJECT", action="SUBJECT_STATUS_CHANGE" if updated.status != old_status else "UPDATE_SUBJECT",
detail=detail or "参与者已更新", detail=json.dumps(
{
"targetName": updated.subject_no,
"description": description,
"before": before_snapshot,
"after": after_snapshot,
},
ensure_ascii=False,
),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -211,6 +252,8 @@ async def delete_subject(
cra_scope = await get_cra_site_scope(db, study_id, current_user) cra_scope = await get_cra_site_scope(db, study_id, current_user)
if cra_scope and subject.site_id not in cra_scope[0]: if cra_scope and subject.site_id not in cra_scope[0]:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足") raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
before_snapshot = await _subject_audit_snapshot(db, subject)
target_name = subject.subject_no
await subject_crud.delete_subject(db, subject) await subject_crud.delete_subject(db, subject)
await audit_crud.log_action( await audit_crud.log_action(
db, db,
@@ -218,7 +261,15 @@ async def delete_subject(
entity_type="subject", entity_type="subject",
entity_id=subject_id, entity_id=subject_id,
action="DELETE_SUBJECT", action="DELETE_SUBJECT",
detail=f"参与者 {subject_id} 已删除", detail=json.dumps(
{
"targetName": target_name,
"description": f"删除参与者 {target_name}",
"before": before_snapshot,
"after": None,
},
ensure_ascii=False,
),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
+30 -5
View File
@@ -1,4 +1,5 @@
import uuid import uuid
import json
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
@@ -15,6 +16,17 @@ from app.schemas.visit import EarlyTerminationCreate, VisitCreate, VisitRead, Vi
router = APIRouter() router = APIRouter()
def _visit_audit_name(subject, visit) -> str:
subject_no = getattr(subject, "subject_no", None) or "参与者"
visit_code = getattr(visit, "visit_code", None) or "访视"
return f"{subject_no} / {visit_code}"
def _visit_audit_detail(action: str, subject, visit) -> str:
name = _visit_audit_name(subject, visit)
return json.dumps({"targetName": name, "description": f"{action}访视“{name}"}, ensure_ascii=False)
async def _ensure_subject(db: AsyncSession, study_id: uuid.UUID, subject_id: uuid.UUID): async def _ensure_subject(db: AsyncSession, study_id: uuid.UUID, subject_id: uuid.UUID):
subject = await subject_crud.get_subject(db, subject_id) subject = await subject_crud.get_subject(db, subject_id)
if not subject or subject.study_id != study_id: if not subject or subject.study_id != study_id:
@@ -105,7 +117,7 @@ async def create_visit(
entity_type="visit", entity_type="visit",
entity_id=visit.id, entity_id=visit.id,
action="CREATE_VISIT", action="CREATE_VISIT",
detail=f"访视 {visit.visit_code} 已创建", detail=_visit_audit_detail("创建", subject, visit),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -158,7 +170,10 @@ async def create_early_termination(
entity_type="visit", entity_type="visit",
entity_id=visit.id, entity_id=visit.id,
action="CREATE_EARLY_TERMINATION", action="CREATE_EARLY_TERMINATION",
detail=f"参与者 {subject.subject_no} 已提前终止", detail=json.dumps(
{"targetName": subject.subject_no, "description": f"创建参与者 {subject.subject_no} 提前终止访视"},
ensure_ascii=False,
),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -191,18 +206,27 @@ async def update_visit(
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="实际访视日期不能早于访视窗口开始日期") raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="实际访视日期不能早于访视窗口开始日期")
if visit_in.actual_date and visit.window_end and visit_in.actual_date > visit.window_end: if visit_in.actual_date and visit.window_end and visit_in.actual_date > visit.window_end:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="实际访视日期不能晚于访视窗口结束日期") raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="实际访视日期不能晚于访视窗口结束日期")
old_status = visit.status
updated = await visit_crud.update_visit(db, visit, visit_in) updated = await visit_crud.update_visit(db, visit, visit_in)
await subject_crud.sync_subject_status(db, subject) await subject_crud.sync_subject_status(db, subject)
detail = None detail = None
if visit_in.status: if visit_in.status:
detail = f"访视 {visit.visit_code} {visit.status} -> {visit_in.status}" detail = json.dumps(
{
"targetName": _visit_audit_name(subject, updated),
"description": f"变更访视“{_visit_audit_name(subject, updated)}”状态",
"before": {"status": old_status},
"after": {"status": visit_in.status},
},
ensure_ascii=False,
)
await audit_crud.log_action( await audit_crud.log_action(
db, db,
study_id=study_id, study_id=study_id,
entity_type="visit", entity_type="visit",
entity_id=visit_id, entity_id=visit_id,
action="VISIT_STATUS_CHANGE" if detail else "UPDATE_VISIT", action="VISIT_STATUS_CHANGE" if detail else "UPDATE_VISIT",
detail=detail or "访视已更新", detail=detail or _visit_audit_detail("更新", subject, updated),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
@@ -226,6 +250,7 @@ async def delete_visit(
visit = await visit_crud.get_visit(db, visit_id) visit = await visit_crud.get_visit(db, visit_id)
if not visit or visit.subject_id != subject_id or visit.study_id != study_id: if not visit or visit.subject_id != subject_id or visit.study_id != study_id:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="访视不存在") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="访视不存在")
visit_detail = _visit_audit_detail("删除", subject, visit)
await visit_crud.delete_visit(db, visit) await visit_crud.delete_visit(db, visit)
await subject_crud.sync_subject_status(db, subject) await subject_crud.sync_subject_status(db, subject)
await audit_crud.log_action( await audit_crud.log_action(
@@ -234,7 +259,7 @@ async def delete_visit(
entity_type="visit", entity_type="visit",
entity_id=visit_id, entity_id=visit_id,
action="DELETE_VISIT", action="DELETE_VISIT",
detail=f"访视 {visit_id} 已删除", detail=visit_detail,
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, study_id, current_user), operator_role=await get_operator_role_label(db, study_id, current_user),
) )
+12 -12
View File
@@ -969,54 +969,54 @@ SYSTEM_PERMISSIONS: dict[str, dict] = {
"description": "管理权限模板", "description": "管理权限模板",
"roles": ["ADMIN"], "roles": ["ADMIN"],
}, },
# 权限监控 # 系统监测
"system:monitoring:metrics": { "system:monitoring:metrics": {
"module": "system_monitoring", "module": "system_monitoring",
"action": "read", "action": "read",
"description": "查看权限监控指标", "description": "查看系统监测指标",
"roles": ["ADMIN", "PM"], "roles": ["ADMIN"],
}, },
"system:monitoring:cache_stats": { "system:monitoring:cache_stats": {
"module": "system_monitoring", "module": "system_monitoring",
"action": "read", "action": "read",
"description": "查看缓存统计", "description": "查看缓存统计",
"roles": ["ADMIN", "PM"], "roles": ["ADMIN"],
}, },
"system:monitoring:alerts": { "system:monitoring:alerts": {
"module": "system_monitoring", "module": "system_monitoring",
"action": "read", "action": "read",
"description": "查看权限告警", "description": "查看权限告警",
"roles": ["ADMIN", "PM"], "roles": ["ADMIN"],
}, },
"system:monitoring:health": { "system:monitoring:health": {
"module": "system_monitoring", "module": "system_monitoring",
"action": "read", "action": "read",
"description": "查看权限系统健康状态", "description": "查看权限系统健康状态",
"roles": ["ADMIN", "PM"], "roles": ["ADMIN"],
}, },
"system:monitoring:access_logs": { "system:monitoring:access_logs": {
"module": "system_monitoring", "module": "system_monitoring",
"action": "read", "action": "read",
"description": "查看权限访问日志", "description": "查看权限访问日志",
"roles": ["ADMIN", "PM"], "roles": ["ADMIN"],
}, },
"system:monitoring:trends": { "system:monitoring:trends": {
"module": "system_monitoring", "module": "system_monitoring",
"action": "read", "action": "read",
"description": "查看权限趋势数据", "description": "查看权限趋势数据",
"roles": ["ADMIN", "PM"], "roles": ["ADMIN"],
}, },
"system:monitoring:reset_metrics": { "system:monitoring:reset_metrics": {
"module": "system_monitoring", "module": "system_monitoring",
"action": "update", "action": "update",
"description": "重置监控指标", "description": "重置系统监测指标",
"roles": ["ADMIN", "PM"], "roles": ["ADMIN"],
}, },
"system:monitoring:clear_alerts": { "system:monitoring:clear_alerts": {
"module": "system_monitoring", "module": "system_monitoring",
"action": "update", "action": "update",
"description": "清除告警", "description": "清除告警",
"roles": ["ADMIN", "PM"], "roles": ["ADMIN"],
}, },
"system:monitoring:security_logs": { "system:monitoring:security_logs": {
"module": "system_monitoring", "module": "system_monitoring",
@@ -1044,6 +1044,6 @@ SYSTEM_MODULE_LABELS: dict[str, str] = {
"system_users": "账号管理", "system_users": "账号管理",
"system_projects": "项目管理", "system_projects": "项目管理",
"system_permissions": "权限管理", "system_permissions": "权限管理",
"system_monitoring": "权限监控", "system_monitoring": "系统监测",
"system_audit": "审计日志", "system_audit": "审计日志",
} }
+2 -5
View File
@@ -1,3 +1,5 @@
from __future__ import annotations
import uuid import uuid
from typing import Sequence from typing import Sequence
@@ -65,8 +67,3 @@ async def list_logs(
async def get_log(db: AsyncSession, log_id: uuid.UUID) -> AuditLog | None: async def get_log(db: AsyncSession, log_id: uuid.UUID) -> AuditLog | None:
result = await db.execute(select(AuditLog).where(AuditLog.id == log_id)) result = await db.execute(select(AuditLog).where(AuditLog.id == log_id))
return result.scalar_one_or_none() return result.scalar_one_or_none()
async def delete_log(db: AsyncSession, log: AuditLog) -> None:
await db.delete(log)
await db.commit()
+3 -4
View File
@@ -121,11 +121,10 @@ async def delete(db: AsyncSession, study_id: uuid.UUID) -> None:
from app.models.study_monitoring_strategy import StudyMonitoringStrategy from app.models.study_monitoring_strategy import StudyMonitoringStrategy
from app.models.study_center_confirm import StudyCenterConfirm from app.models.study_center_confirm import StudyCenterConfirm
# 按依赖关系顺序删除关联数据 # 按依赖关系顺序删除关联数据。审计日志必须保留,只解除项目外键,避免硬删除项目时破坏审计链。
# 1. 删除审计日志 await db.execute(sa_update(AuditLog).where(AuditLog.study_id == study_id).values(study_id=None))
await db.execute(sa_delete(AuditLog).where(AuditLog.study_id == study_id))
# 2. 删除FAQ相关(可能有依赖关系) # 1. 删除FAQ相关(可能有依赖关系)
await db.execute(sa_delete(FaqReply).where(FaqReply.study_id == study_id)) await db.execute(sa_delete(FaqReply).where(FaqReply.study_id == study_id))
await db.execute(sa_delete(FaqItem).where(FaqItem.study_id == study_id)) await db.execute(sa_delete(FaqItem).where(FaqItem.study_id == study_id))
await db.execute(sa_delete(FaqCategory).where(FaqCategory.study_id == study_id)) await db.execute(sa_delete(FaqCategory).where(FaqCategory.study_id == study_id))
+4 -2
View File
@@ -56,23 +56,25 @@ def _audit_detail(before: dict | None, after: dict | None) -> str:
def _doc_snapshot(doc: Document) -> dict: def _doc_snapshot(doc: Document) -> dict:
status = doc.status.value if hasattr(doc.status, "value") else str(doc.status)
return { return {
"id": str(doc.id), "id": str(doc.id),
"trial_id": str(doc.trial_id), "trial_id": str(doc.trial_id),
"site_id": str(doc.site_id) if doc.site_id else None, "site_id": str(doc.site_id) if doc.site_id else None,
"etmf_node_id": str(doc.etmf_node_id) if doc.etmf_node_id else None, "etmf_node_id": str(doc.etmf_node_id) if doc.etmf_node_id else None,
"doc_no": doc.doc_no, "doc_no": doc.doc_no,
"status": str(doc.status), "status": status,
"current_effective_version_id": str(doc.current_effective_version_id) if doc.current_effective_version_id else None, "current_effective_version_id": str(doc.current_effective_version_id) if doc.current_effective_version_id else None,
} }
def _version_snapshot(version: DocumentVersion) -> dict: def _version_snapshot(version: DocumentVersion) -> dict:
status = version.status.value if hasattr(version.status, "value") else str(version.status)
return { return {
"id": str(version.id), "id": str(version.id),
"document_id": str(version.document_id), "document_id": str(version.document_id),
"version_no": version.version_no, "version_no": version.version_no,
"status": str(version.status), "status": status,
"file_hash": version.file_hash, "file_hash": version.file_hash,
} }
+23 -2
View File
@@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
import uuid import uuid
import json
from collections import defaultdict from collections import defaultdict
from typing import Iterable from typing import Iterable
@@ -18,6 +19,17 @@ from app.schemas.document import DocumentCreate, DocumentSummary
from app.schemas.etmf import EtmfNodeCreate, EtmfNodeRead, EtmfNodeStatus, EtmfTreeNode, EtmfNodeUpdate from app.schemas.etmf import EtmfNodeCreate, EtmfNodeRead, EtmfNodeStatus, EtmfTreeNode, EtmfNodeUpdate
def _node_audit_snapshot(node: EtmfNode) -> dict[str, object | None]:
return {
"code": node.code,
"name": node.name,
"parent_id": str(node.parent_id) if node.parent_id else None,
"scope_type": node.scope_type,
"required": node.required,
"is_active": node.is_active,
}
def calculate_node_status(node: EtmfNode, documents: Iterable[Document]) -> EtmfNodeStatus: def calculate_node_status(node: EtmfNode, documents: Iterable[Document]) -> EtmfNodeStatus:
docs = list(documents) docs = list(documents)
if not node.is_active: if not node.is_active:
@@ -102,7 +114,10 @@ async def create_node(db: AsyncSession, payload: EtmfNodeCreate, current_user) -
entity_type="ETMF_NODE", entity_type="ETMF_NODE",
entity_id=node.id, entity_id=node.id,
action="ETMF_NODE_CREATED", action="ETMF_NODE_CREATED",
detail=f'{{"code":"{node.code}","name":"{node.name}"}}', detail=json.dumps(
{"targetName": node.name, "after": _node_audit_snapshot(node)},
ensure_ascii=False,
),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, payload.study_id, current_user), operator_role=await get_operator_role_label(db, payload.study_id, current_user),
) )
@@ -124,14 +139,20 @@ async def update_node(db: AsyncSession, node_id: uuid.UUID, payload: EtmfNodeUpd
parent = await etmf_crud.get_node(db, values["parent_id"]) parent = await etmf_crud.get_node(db, values["parent_id"])
if not parent or parent.study_id != node.study_id or parent.id == node.id: if not parent or parent.study_id != node.study_id or parent.id == node.id:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="父级eTMF目录不合法") raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="父级eTMF目录不合法")
before = _node_audit_snapshot(node)
await etmf_crud.update_node(db, node_id, values, commit=False) await etmf_crud.update_node(db, node_id, values, commit=False)
updated_snapshot = {**before, **{key: value for key, value in values.items() if key in before}}
db.add( db.add(
AuditLog( AuditLog(
study_id=node.study_id, study_id=node.study_id,
entity_type="ETMF_NODE", entity_type="ETMF_NODE",
entity_id=node.id, entity_id=node.id,
action="ETMF_NODE_UPDATED", action="ETMF_NODE_UPDATED",
detail="{}", detail=json.dumps(
{"targetName": updated_snapshot.get("name") or before.get("name"), "before": before, "after": updated_snapshot},
ensure_ascii=False,
default=str,
),
operator_id=current_user.id, operator_id=current_user.id,
operator_role=await get_operator_role_label(db, node.study_id, current_user), operator_role=await get_operator_role_label(db, node.study_id, current_user),
) )
+97 -2
View File
@@ -1,4 +1,5 @@
import uuid import uuid
import json
from dataclasses import dataclass from dataclasses import dataclass
import pytest import pytest
@@ -7,11 +8,11 @@ from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.api.v1.api_permissions import get_my_study_api_permissions, update_study_api_permissions from app.api.v1.api_permissions import get_my_study_api_permissions, update_study_api_permissions
from app.api.v1.members import update_member from app.api.v1.members import add_member, update_member
from app.api.v1.permission_monitoring import resolve_monitoring_scope from app.api.v1.permission_monitoring import resolve_monitoring_scope
from app.api.v1.system_permissions import list_system_permissions from app.api.v1.system_permissions import list_system_permissions
from app.core.deps import require_admin_or_any_project_pm, require_system_permission from app.core.deps import require_admin_or_any_project_pm, require_system_permission
from app.schemas.member import StudyMemberUpdate from app.schemas.member import StudyMemberCreate, StudyMemberUpdate
@dataclass @dataclass
@@ -40,6 +41,26 @@ async def _seed_user(db: AsyncSession, user_id: uuid.UUID, *, is_admin: bool = F
) )
async def _seed_named_user(db: AsyncSession, user_id: uuid.UUID, full_name: str, *, is_admin: bool = False) -> None:
await db.execute(
text(
"""
INSERT INTO users (id, email, password_hash, full_name, clinical_department, is_admin, status)
VALUES (:id, :email, :password_hash, :full_name, :clinical_department, :is_admin, :status)
"""
),
{
"id": str(user_id),
"email": f"{user_id.hex}@example.com",
"password_hash": "hash",
"full_name": full_name,
"clinical_department": "Clinical",
"is_admin": is_admin,
"status": "ACTIVE",
},
)
async def _seed_study(db: AsyncSession, study_id: uuid.UUID, code: str) -> None: async def _seed_study(db: AsyncSession, study_id: uuid.UUID, code: str) -> None:
await db.execute( await db.execute(
text( text(
@@ -289,3 +310,77 @@ async def test_project_pm_cannot_grant_peer_pm_role(db_session: AsyncSession):
) )
assert exc_info.value.status_code == 403 assert exc_info.value.status_code == 403
@pytest.mark.asyncio
async def test_project_member_add_audit_uses_member_display_name(db_session: AsyncSession):
study_id = uuid.uuid4()
admin_id = uuid.uuid4()
target_user_id = uuid.uuid4()
await _seed_study(db_session, study_id, "MEMBER-AUDIT-NAME")
await _seed_user(db_session, admin_id, is_admin=True)
await _seed_named_user(db_session, target_user_id, "张三")
await db_session.commit()
await add_member(
study_id=study_id,
member_in=StudyMemberCreate(user_id=target_user_id, role_in_study="CRA"),
current_user=UserStub(id=admin_id, is_admin=True),
db=db_session,
)
row = (
await db_session.execute(
text(
"""
SELECT detail
FROM audit_logs
WHERE study_id = :study_id AND action = 'PROJECT_MEMBER_ADDED'
ORDER BY created_at DESC
LIMIT 1
"""
),
{"study_id": str(study_id)},
)
).one()
detail = json.loads(row.detail)
assert detail["targetName"] == "张三"
assert str(target_user_id) not in detail["targetName"]
@pytest.mark.asyncio
async def test_project_member_update_audit_uses_member_display_name(db_session: AsyncSession):
study_id = uuid.uuid4()
admin_id = uuid.uuid4()
target_user_id = uuid.uuid4()
await _seed_study(db_session, study_id, "MEMBER-UPDATE-AUDIT-NAME")
await _seed_user(db_session, admin_id, is_admin=True)
await _seed_named_user(db_session, target_user_id, "李四")
member_id = await _seed_member_return_id(db_session, study_id, target_user_id, "CRA")
await db_session.commit()
await update_member(
study_id=study_id,
member_id=member_id,
member_in=StudyMemberUpdate(role_in_study="CTA"),
current_user=UserStub(id=admin_id, is_admin=True),
db=db_session,
)
row = (
await db_session.execute(
text(
"""
SELECT detail
FROM audit_logs
WHERE study_id = :study_id AND action = 'PROJECT_MEMBER_UPDATED'
ORDER BY created_at DESC
LIMIT 1
"""
),
{"study_id": str(study_id)},
)
).one()
detail = json.loads(row.detail)
assert detail["targetName"] == "李四"
assert str(target_user_id) not in detail["targetName"]
-11
View File
@@ -1,11 +0,0 @@
from pathlib import Path
def test_new_ae_records_default_to_follow_up_status():
crud_source = Path("app/crud/ae.py").read_text(encoding="utf-8")
model_source = Path("app/models/ae.py").read_text(encoding="utf-8")
assert 'status="FOLLOW_UP"' in crud_source
assert 'default="FOLLOW_UP"' in model_source
assert 'status="NEW"' not in crud_source
assert 'default="NEW"' not in model_source
+149
View File
@@ -588,6 +588,155 @@ def test_audit_export_event_uses_export_permission():
assert "require_study_member()" not in event_chunk assert "require_study_member()" not in event_chunk
def test_audit_export_event_does_not_trust_client_detail():
"""导出审计事件的业务描述应由后端生成,不能信任客户端 detail。"""
route_path = Path(__file__).resolve().parents[1] / "app" / "api" / "v1" / "audit_logs.py"
source = route_path.read_text()
event_chunk = source[source.index("async def create_audit_event") :]
assert "detail=payload.detail" not in event_chunk
assert "entity_type=payload.entity_type" not in event_chunk
assert "entity_id=payload.entity_id" not in event_chunk
assert "_build_export_audit_detail" in event_chunk
def test_audit_logs_do_not_expose_delete_endpoint():
"""审计日志不能提供单条删除接口,避免破坏审计不可篡改性。"""
route_path = Path(__file__).resolve().parents[1] / "app" / "api" / "v1" / "audit_logs.py"
source = route_path.read_text()
assert "@router.delete" not in source
assert "delete_audit_log" not in source
def test_etmf_audit_details_are_structured_json():
"""eTMF 审计详情不能手拼 JSON 或写空对象。"""
service_path = Path(__file__).resolve().parents[1] / "app" / "services" / "etmf_service.py"
source = service_path.read_text()
assert "json.dumps" in source
assert "detail=f'{{" not in source
assert 'detail="{}"' not in source
def test_setup_config_diff_summary_keeps_all_business_changes_and_skips_ids():
"""立项配置审计明细应完整保留业务字段,跳过行内技术 ID。"""
route_path = Path(__file__).resolve().parents[1] / "app" / "api" / "v1" / "studies.py"
source = route_path.read_text()
summary_chunk = source[source.index("def _setup_collect_diff_lines") : source.index("@router.post")]
assert '_SETUP_TECHNICAL_FIELDS = {"id"}' in source
assert "if key in _SETUP_TECHNICAL_FIELDS:" in summary_chunk
assert "已省略" not in summary_chunk
assert "max_lines" not in summary_chunk
def test_subject_audit_uses_structured_business_snapshot():
"""参与者审计应把编号放入对象,把业务变化放入详情。"""
route_path = Path(__file__).resolve().parents[1] / "app" / "api" / "v1" / "subjects.py"
source = route_path.read_text()
create_subject_source = source[source.index("async def create_subject") : source.index("@router.get")]
update_subject_source = source[source.index("async def update_subject") : source.index("@router.delete")]
delete_subject_source = source[source.index("async def delete_subject") :]
assert "_subject_audit_snapshot" in source
assert '"targetName": subject.subject_no' in create_subject_source
assert '"description": f"创建参与者 {subject.subject_no}"' in create_subject_source
assert '"targetName": updated.subject_no' in update_subject_source
assert '"description": description' in update_subject_source
assert '"targetName": target_name' in delete_subject_source
assert '"description": f"删除参与者 {target_name}"' in delete_subject_source
assert '"before": before_snapshot' in delete_subject_source
assert 'detail=f"参与者 {subject.subject_no} 已创建"' not in source
assert 'f"参与者 {updated.subject_no} 状态' not in source
assert 'detail=f"参与者 {subject_id} 已删除"' not in delete_subject_source
def test_faq_audit_uses_business_descriptions():
"""医学咨询审计详情应使用业务描述,避免 FAQ 技术文案。"""
api_dir = Path(__file__).resolve().parents[1] / "app" / "api" / "v1"
category_source = (api_dir / "faq_categories.py").read_text()
item_source = (api_dir / "faqs.py").read_text()
assert 'description": f"创建“{category.name}”分类"' in category_source
assert 'description": f"更新“{updated.name}”分类"' in category_source
assert 'description": f"删除“{category_name}”分类"' in category_source
assert 'f"创建医学咨询问题“{question_name}"' in item_source
assert 'f"更新医学咨询问题“{question_name}"' in item_source
assert 'f"回复医学咨询问题“{question_name}"' in item_source
assert 'f"删除医学咨询问题“{question_name}"' in item_source
assert 'f"删除医学咨询问题“{question_name}”的回复"' in item_source
assert "FAQ 分类 {category.name} 已创建" not in category_source
assert 'detail="FAQ 已创建"' not in item_source
assert 'detail = "FAQ updated"' not in item_source
assert '"description": "创建医学咨询问题"' not in item_source
def test_domain_audits_use_business_object_names():
"""药品、设备、访视、AE、费用、立项等审计应记录具体业务对象。"""
api_dir = Path(__file__).resolve().parents[1] / "app" / "api" / "v1"
sources = {
name: (api_dir / name).read_text()
for name in [
"drug_shipments.py",
"material_equipments.py",
"visits.py",
"aes.py",
"precautions.py",
"fees_contracts.py",
"startup.py",
"subject_histories.py",
]
}
assert "_shipment_audit_description" in sources["drug_shipments.py"]
assert "_equipment_audit_detail" in sources["material_equipments.py"]
assert "_visit_audit_detail" in sources["visits.py"]
assert "_ae_audit_detail" in sources["aes.py"]
assert "_precaution_audit_detail" in sources["precautions.py"]
assert "_contract_audit_detail" in sources["fees_contracts.py"]
assert "_payment_audit_detail" in sources["fees_contracts.py"]
assert "_feasibility_audit_detail" in sources["startup.py"]
assert "_ethics_audit_detail" in sources["startup.py"]
assert "_kickoff_audit_detail" in sources["startup.py"]
assert "_training_audit_detail" in sources["startup.py"]
assert "_history_audit_detail" in sources["subject_histories.py"]
forbidden_fragments = [
'detail=f"访视 {visit_id} 已删除"',
'detail=f"AE {ae.id} 已创建"',
'detail=f"AE {ae_id} 已删除"',
'detail=f"设备 {item.id} 已创建"',
'detail=f"设备 {equipment_id} 已更新"',
'detail=f"设备 {equipment_id} 已删除"',
'detail=f"注意事项 {precaution_id} 已更新"',
'detail=f"注意事项 {precaution_id} 已删除"',
'detail="合同费用已创建"',
'detail="合同费用已更新"',
'detail="合同费用已删除"',
'detail="合同费用分期已创建"',
'detail="合同费用分期已更新"',
'detail="合同费用分期已删除"',
'detail="立项记录已创建"',
'detail="立项记录已更新"',
'detail="立项记录已删除"',
'detail="伦理记录已创建"',
'detail="伦理记录已更新"',
'detail="伦理记录已删除"',
'detail="启动会记录已创建"',
'detail="启动会记录已更新"',
'detail="培训授权人员已创建"',
'detail="培训授权人员已更新"',
'detail="培训授权人员已删除"',
'detail="病史记录已创建"',
'detail="病史记录已更新"',
'detail="病史记录已删除"',
]
combined_source = "\n".join(sources.values())
for fragment in forbidden_fragments:
assert fragment not in combined_source
def test_user_role_contains_current_project_roles(): def test_user_role_contains_current_project_roles():
assert {"QA", "CTA"} <= set(PROJECT_PERMISSION_ROLES) assert {"QA", "CTA"} <= set(PROJECT_PERMISSION_ROLES)
@@ -1,86 +0,0 @@
"""合同费用基础字段测试"""
from datetime import date
from decimal import Decimal
import uuid
from starlette.requests import Request
from app.api.v1 import fees_contracts
from app.models.contract_fee import ContractFee
from app.schemas.contract_fee import ContractFeeCreate, ContractFeeRead, ContractFeeUpdate
def _request_with_query(query: str) -> Request:
return Request(
{
"type": "http",
"method": "GET",
"path": "/api/v1/fees/contracts",
"query_string": query.encode(),
"headers": [],
}
)
def test_contract_fee_list_query_accepts_legacy_project_params():
"""合同费用列表应兼容旧 projectId/centerId 查询参数。"""
study_id = uuid.uuid4()
center_id = uuid.uuid4()
resolver = getattr(fees_contracts, "_resolve_contract_fee_list_query", None)
assert resolver is not None
resolved_study_id, resolved_center_id = resolver(
_request_with_query(f"projectId={study_id}&centerId={center_id}"),
None,
None,
)
assert resolved_study_id == study_id
assert resolved_center_id == center_id
def test_contract_fee_schema_includes_contract_basic_fields():
"""合同费用应包含合同基础信息字段。"""
study_id = uuid.uuid4()
center_id = uuid.uuid4()
payload = ContractFeeCreate(
study_id=study_id,
center_id=center_id,
contract_no="CT-001",
signed_date=date(2026, 5, 27),
contract_amount=Decimal("120000.00"),
currency="CNY",
remark="首版合同",
contract_cases=12,
)
assert payload.contract_no == "CT-001"
assert payload.signed_date == date(2026, 5, 27)
assert payload.currency == "CNY"
assert payload.remark == "首版合同"
assert payload.study_id == study_id
assert "project_id" not in ContractFeeCreate.model_fields
update_payload = ContractFeeUpdate(contract_no="CT-002", currency="USD", remark="变更合同信息")
assert update_payload.model_dump(exclude_unset=True) == {
"contract_no": "CT-002",
"currency": "USD",
"remark": "变更合同信息",
}
for field in ("contract_no", "signed_date", "currency", "remark"):
assert field in ContractFeeRead.model_fields
assert "study_id" in ContractFeeRead.model_fields
assert "project_id" not in ContractFeeRead.model_fields
def test_contract_fee_model_includes_contract_basic_columns():
"""合同费用模型应持久化合同基础信息。"""
columns = ContractFee.__table__.columns
assert "contract_no" in columns
assert "signed_date" in columns
assert "currency" in columns
assert "remark" in columns
@@ -1,81 +0,0 @@
import uuid
from datetime import date
import pytest
from pydantic import ValidationError
from app.schemas.drug_shipment import DrugShipmentCreate
def shipment_payload(**overrides):
payload = {
"direction": "SEND",
"center_id": uuid.uuid4(),
"ship_date": date(2026, 6, 4),
"receive_date": None,
"quantity": 1,
"batch_no": "DP-001",
"carrier": "顺丰",
"tracking_no": "SF100003",
"status": "IN_TRANSIT",
"remark": None,
}
payload.update(overrides)
return payload
def test_create_allows_pending_shipment_execution_fields_to_be_empty():
shipment = DrugShipmentCreate.model_validate(
shipment_payload(
status="PENDING",
ship_date=None,
quantity=None,
batch_no=None,
carrier=None,
tracking_no=None,
receive_date=None,
remark=None,
)
)
assert shipment.ship_date is None
assert shipment.quantity is None
assert shipment.batch_no is None
assert shipment.carrier is None
assert shipment.tracking_no is None
@pytest.mark.parametrize("field_name", ["ship_date", "quantity", "batch_no", "carrier", "tracking_no"])
def test_create_requires_shipment_execution_fields_after_pending(field_name):
with pytest.raises(ValidationError) as exc_info:
DrugShipmentCreate.model_validate(shipment_payload(status="IN_TRANSIT", **{field_name: None}))
assert "发运信息" in str(exc_info.value)
def test_create_allows_pending_receipt_fields_to_be_empty():
shipment = DrugShipmentCreate.model_validate(shipment_payload())
assert shipment.receive_date is None
assert shipment.remark is None
def test_create_requires_receive_date_when_signed():
with pytest.raises(ValidationError) as exc_info:
DrugShipmentCreate.model_validate(shipment_payload(status="SIGNED", receive_date=None))
assert "接收日期" in str(exc_info.value)
def test_create_rejects_removed_returned_status():
with pytest.raises(ValidationError) as exc_info:
DrugShipmentCreate.model_validate(shipment_payload(status="RETURNED", receive_date=date(2026, 6, 5)))
assert "无效状态" in str(exc_info.value)
def test_create_requires_remark_when_exceptional():
with pytest.raises(ValidationError) as exc_info:
DrugShipmentCreate.model_validate(shipment_payload(status="EXCEPTION", remark=""))
assert "备注" in str(exc_info.value)
-39
View File
@@ -1,39 +0,0 @@
import uuid
from sqlalchemy import text
import pytest
from app.crud.faq_category import create_category
from app.crud.faq_item import create_item
from app.schemas.faq import CategoryCreate, FaqCreate
@pytest.mark.asyncio
async def test_create_category_defaults_to_active(db_session):
result = await db_session.execute(text("SELECT id FROM studies LIMIT 1"))
study_id = result.scalar_one()
category = await create_category(
db_session,
CategoryCreate(study_id=study_id, name="AE", sort_order=0),
)
assert category.is_active is True
@pytest.mark.asyncio
async def test_create_item_defaults_to_active(db_session):
result = await db_session.execute(text("SELECT id FROM studies LIMIT 1"))
study_id = result.scalar_one()
category = await create_category(
db_session,
CategoryCreate(study_id=study_id, name=f"AE-{uuid.uuid4().hex[:8]}", sort_order=0),
)
item = await create_item(
db_session,
FaqCreate(study_id=study_id, category_id=category.id, question="AE 如何记录?"),
created_by=uuid.uuid4(),
)
assert item.is_active is True
-37
View File
@@ -1,37 +0,0 @@
"""单元测试:IP 属地解析服务"""
from app.services.ip_location import Ip2RegionResolver
class FakeSearcher:
def search(self, _ip: str) -> str:
return "中国|广东省|深圳市|电信|CN"
def test_ip_location_handles_special_addresses():
resolver = Ip2RegionResolver(db_path="/not-exists/ip2region.xdb")
assert resolver.lookup(None).location == "未知"
assert resolver.lookup("not-an-ip").location == "未知"
assert resolver.lookup("127.0.0.1").location == "本机"
assert resolver.lookup("192.168.1.1").location == "局域网"
def test_ip2region_result_parses_province_city_isp():
resolver = Ip2RegionResolver(db_path="/not-exists/ip2region.xdb")
resolver._searchers[4] = FakeSearcher()
result = resolver.lookup("8.8.8.8")
assert result.country == "中国"
assert result.province == "广东省"
assert result.city == "深圳市"
assert result.isp == "电信"
assert result.location == "中国 / 广东省 / 深圳市 / 电信"
def test_default_resolver_can_use_packaged_xdb():
resolver = Ip2RegionResolver()
result = resolver.lookup("8.8.8.8")
assert result.location not in {"公网", "未知"}
@@ -1,272 +0,0 @@
"""
第2批模块迁移测试members sites 模块
测试接口级权限系统在 members sites 模块中的应用
"""
import uuid
import pytest
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.project_permissions import role_has_api_permission
from app.models.api_endpoint_permission import ApiEndpointPermission
@pytest.mark.asyncio
async def test_add_member_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以添加项目成员"""
study_id = uuid.uuid4()
# 创建权限
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="project_members:create",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
# 验证权限检查
allowed = await role_has_api_permission(db_session, study_id, "PM", "project_members:create")
assert allowed is True
@pytest.mark.asyncio
async def test_add_member_without_permission(db_session: AsyncSession):
"""验证无权限的CRA无法添加项目成员"""
study_id = uuid.uuid4()
# 创建权限(拒绝)
perm = ApiEndpointPermission(
study_id=study_id,
role="CRA",
endpoint_key="project_members:create",
allowed=False,
)
db_session.add(perm)
await db_session.commit()
# 验证权限检查
allowed = await role_has_api_permission(db_session, study_id, "CRA", "project_members:create")
assert allowed is False
@pytest.mark.asyncio
async def test_list_members_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以查询项目成员列表"""
study_id = uuid.uuid4()
# 创建权限
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="project_members:list",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
# 验证权限检查
allowed = await role_has_api_permission(db_session, study_id, "PM", "project_members:list")
assert allowed is True
@pytest.mark.asyncio
async def test_update_member_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以更新项目成员"""
study_id = uuid.uuid4()
# 创建权限
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="project_members:update",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
# 验证权限检查
allowed = await role_has_api_permission(db_session, study_id, "PM", "project_members:update")
assert allowed is True
@pytest.mark.asyncio
async def test_delete_member_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以删除项目成员"""
study_id = uuid.uuid4()
# 创建权限
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="project_members:delete",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
# 验证权限检查
allowed = await role_has_api_permission(db_session, study_id, "PM", "project_members:delete")
assert allowed is True
@pytest.mark.asyncio
async def test_list_member_candidates_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以查询项目成员候选人"""
study_id = uuid.uuid4()
# 创建权限
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="project_members:candidates",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
# 验证权限检查
allowed = await role_has_api_permission(db_session, study_id, "PM", "project_members:candidates")
assert allowed is True
@pytest.mark.asyncio
async def test_create_site_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以创建中心"""
study_id = uuid.uuid4()
# 创建权限
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="sites:create",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
# 验证权限检查
allowed = await role_has_api_permission(db_session, study_id, "PM", "sites:create")
assert allowed is True
@pytest.mark.asyncio
async def test_list_sites_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以查询中心列表"""
study_id = uuid.uuid4()
# 创建权限
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="sites:list",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
# 验证权限检查
allowed = await role_has_api_permission(db_session, study_id, "PM", "sites:list")
assert allowed is True
@pytest.mark.asyncio
async def test_list_sites_cra_with_permission(db_session: AsyncSession):
"""验证有权限的CRA可以查询中心列表"""
study_id = uuid.uuid4()
# 创建权限
perm = ApiEndpointPermission(
study_id=study_id,
role="CRA",
endpoint_key="sites:list",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
# 验证权限检查
allowed = await role_has_api_permission(db_session, study_id, "CRA", "sites:list")
assert allowed is True
@pytest.mark.asyncio
async def test_update_site_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以更新中心"""
study_id = uuid.uuid4()
# 创建权限
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="sites:update",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
# 验证权限检查
allowed = await role_has_api_permission(db_session, study_id, "PM", "sites:update")
assert allowed is True
@pytest.mark.asyncio
async def test_delete_site_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以删除中心"""
study_id = uuid.uuid4()
# 创建权限
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="sites:delete",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
# 验证权限检查
allowed = await role_has_api_permission(db_session, study_id, "PM", "sites:delete")
assert allowed is True
@pytest.mark.asyncio
async def test_members_permission_denied_for_cra(db_session: AsyncSession):
"""验证CRA无法执行成员管理操作"""
study_id = uuid.uuid4()
# 创建权限(拒绝)
perm = ApiEndpointPermission(
study_id=study_id,
role="CRA",
endpoint_key="project_members:create",
allowed=False,
)
db_session.add(perm)
await db_session.commit()
# 验证权限检查
allowed = await role_has_api_permission(db_session, study_id, "CRA", "project_members:create")
assert allowed is False
@pytest.mark.asyncio
async def test_sites_permission_denied_for_cra_write(db_session: AsyncSession):
"""验证CRA无法执行中心写操作"""
study_id = uuid.uuid4()
# 创建权限(拒绝)
perm = ApiEndpointPermission(
study_id=study_id,
role="CRA",
endpoint_key="sites:create",
allowed=False,
)
db_session.add(perm)
await db_session.commit()
# 验证权限检查
allowed = await role_has_api_permission(db_session, study_id, "CRA", "sites:create")
assert allowed is False
@@ -1,489 +0,0 @@
"""
第3批模块迁移测试12个模块63个端点
测试接口级权限系统在所有第3批模块中的应用
"""
import uuid
import pytest
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.project_permissions import role_has_api_permission
from app.models.api_endpoint_permission import ApiEndpointPermission
# ============================================================================
# 启动管理 (startup)
# ============================================================================
@pytest.mark.asyncio
async def test_startup_ethics_create_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以创建伦理记录"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="startup_ethics:create",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "PM", "startup_ethics:create")
assert allowed is True
@pytest.mark.asyncio
async def test_startup_ethics_list_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以查询伦理记录列表"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="startup_ethics:list",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "PM", "startup_ethics:list")
assert allowed is True
@pytest.mark.asyncio
async def test_startup_initiation_create_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以创建立项记录"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="startup_initiation:create",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "PM", "startup_initiation:create")
assert allowed is True
@pytest.mark.asyncio
async def test_startup_auth_create_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以创建启动会或培训授权"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="startup_auth:create",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "PM", "startup_auth:create")
assert allowed is True
@pytest.mark.asyncio
async def test_startup_auth_read_with_permission(db_session: AsyncSession):
"""验证有权限的CRA可以查询启动会或培训授权"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="CRA",
endpoint_key="startup_auth:read",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "CRA", "startup_auth:read")
assert allowed is True
# ============================================================================
# 项目权限管理已迁移为系统级权限
# ============================================================================
def test_project_permissions_are_not_project_matrix_permissions():
"""项目权限配置由 system:permissions:project_config 控制,不再进入项目矩阵。"""
from app.core.api_permissions import API_ENDPOINT_PERMISSIONS, SYSTEM_PERMISSIONS
assert "permissions:read" not in API_ENDPOINT_PERMISSIONS
assert "permissions:update" not in API_ENDPOINT_PERMISSIONS
assert "system:permissions:project_config" in SYSTEM_PERMISSIONS
# ============================================================================
# 项目概览 (overview) - 1个端点
# ============================================================================
@pytest.mark.asyncio
async def test_overview_get_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以查询项目概览"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="project_overview:read",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "PM", "project_overview:read")
assert allowed is True
# ============================================================================
# 监查访视问题汇总 (monitoring_visit_issues)
# ============================================================================
@pytest.mark.asyncio
async def test_monitoring_issues_list_with_permission(db_session: AsyncSession):
"""验证有权限的CRA可以查询监查访视问题列表"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="CRA",
endpoint_key="monitoring_issues:list",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "CRA", "monitoring_issues:list")
assert allowed is True
# ============================================================================
# 药物发货 (drug_shipments) - 5个端点
# ============================================================================
@pytest.mark.asyncio
async def test_drug_shipments_create_with_permission(db_session: AsyncSession):
"""验证有权限的CTA可以创建药物发货"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="CTA",
endpoint_key="drug_shipments:create",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "CTA", "drug_shipments:create")
assert allowed is True
@pytest.mark.asyncio
async def test_drug_shipments_list_with_permission(db_session: AsyncSession):
"""验证有权限的CTA可以查询药物发货列表"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="CTA",
endpoint_key="drug_shipments:list",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "CTA", "drug_shipments:list")
assert allowed is True
# ============================================================================
# 设备管理 (material_equipments) - 5个端点
# ============================================================================
@pytest.mark.asyncio
async def test_material_equipments_create_with_permission(db_session: AsyncSession):
"""验证有权限的CRA可以创建设备"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="CRA",
endpoint_key="material_equipments:create",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "CRA", "material_equipments:create")
assert allowed is True
@pytest.mark.asyncio
async def test_material_equipments_list_with_permission(db_session: AsyncSession):
"""验证有权限的CTA可以查询设备列表"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="CTA",
endpoint_key="material_equipments:list",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "CTA", "material_equipments:list")
assert allowed is True
# ============================================================================
# 参与者PD (subject_pds) - 4个端点
# ============================================================================
@pytest.mark.asyncio
async def test_subject_pds_create_with_permission(db_session: AsyncSession):
"""验证有权限的CRA可以创建参与者PD"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="CRA",
endpoint_key="subject_pds:create",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "CRA", "subject_pds:create")
assert allowed is True
@pytest.mark.asyncio
async def test_subject_pds_list_with_permission(db_session: AsyncSession):
"""验证有权限的CRA可以查询参与者PD列表"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="CRA",
endpoint_key="subject_pds:list",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "CRA", "subject_pds:list")
assert allowed is True
# ============================================================================
# 审计日志 (audit_logs) - 3个端点
# ============================================================================
@pytest.mark.asyncio
async def test_audit_logs_list_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以查询审计日志列表"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="audit_logs:list",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "PM", "audit_logs:list")
assert allowed is True
@pytest.mark.asyncio
async def test_audit_logs_export_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以导出审计日志"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="audit_logs:export",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "PM", "audit_logs:export")
assert allowed is True
# ============================================================================
# 访视管理 (visits) - 5个端点
# ============================================================================
@pytest.mark.asyncio
async def test_visits_create_with_permission(db_session: AsyncSession):
"""验证有权限的PV可以创建访视"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="PV",
endpoint_key="visits:create",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "PV", "visits:create")
assert allowed is True
@pytest.mark.asyncio
async def test_visits_list_with_permission(db_session: AsyncSession):
"""验证有权限的PV可以查询访视列表"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="PV",
endpoint_key="visits:list",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "PV", "visits:list")
assert allowed is True
# ============================================================================
# 注意事项 (precautions) - 5个端点
# ============================================================================
@pytest.mark.asyncio
async def test_precautions_create_with_permission(db_session: AsyncSession):
"""验证有权限的QA可以创建注意事项"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="QA",
endpoint_key="precautions:create",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "QA", "precautions:create")
assert allowed is True
@pytest.mark.asyncio
async def test_precautions_list_with_permission(db_session: AsyncSession):
"""验证有权限的QA可以查询注意事项列表"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="QA",
endpoint_key="precautions:list",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "QA", "precautions:list")
assert allowed is True
# ============================================================================
# 病史记录 (subject_histories)
# ============================================================================
@pytest.mark.asyncio
async def test_subject_histories_list_with_permission(db_session: AsyncSession):
"""验证有权限的CRA可以查询病史记录列表"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="CRA",
endpoint_key="subject_histories:list",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "CRA", "subject_histories:list")
assert allowed is True
@pytest.mark.asyncio
async def test_subject_histories_read_with_permission(db_session: AsyncSession):
"""验证有权限的CRA可以查询病史记录详情"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="CRA",
endpoint_key="subject_histories:read",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "CRA", "subject_histories:read")
assert allowed is True
# ============================================================================
# 项目里程碑 (project_milestones) - 2个端点
# ============================================================================
@pytest.mark.asyncio
async def test_milestones_list_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以查询项目里程碑列表"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="project_milestones:read",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "PM", "project_milestones:read")
assert allowed is True
@pytest.mark.asyncio
async def test_milestones_update_with_permission(db_session: AsyncSession):
"""验证有权限的PM可以更新项目里程碑"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="PM",
endpoint_key="project_milestones:update",
allowed=True,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "PM", "project_milestones:update")
assert allowed is True
# ============================================================================
# 权限拒绝场景
# ============================================================================
@pytest.mark.asyncio
async def test_startup_permission_denied_for_cra(db_session: AsyncSession):
"""验证CRA无法执行启动管理操作"""
study_id = uuid.uuid4()
perm = ApiEndpointPermission(
study_id=study_id,
role="CRA",
endpoint_key="startup_ethics:create",
allowed=False,
)
db_session.add(perm)
await db_session.commit()
allowed = await role_has_api_permission(db_session, study_id, "CRA", "startup_ethics:create")
assert allowed is False
@pytest.mark.asyncio
async def test_removed_project_permissions_are_denied_for_project_roles(db_session: AsyncSession):
"""项目角色不能再通过 permissions:update 这种残留 key 获得权限管理能力。"""
study_id = uuid.uuid4()
allowed = await role_has_api_permission(db_session, study_id, "CRA", "permissions:update")
assert allowed is False
-107
View File
@@ -1,107 +0,0 @@
from pathlib import Path
def test_is_locked_migration_is_idempotent():
source = Path("alembic/versions/20260116_01_add_is_locked_to_studies.py").read_text(encoding="utf-8")
assert 'if "is_locked" not in columns:' in source
assert 'if "is_locked" in columns:' in source
def test_removed_workflow_tables_are_dropped_conditionally():
source = Path("alembic/versions/20260116_05_remove_document_workflows.py").read_text(encoding="utf-8")
assert 'if "workflow_actions" in tables:' in source
assert 'if "version_workflows" in tables:' in source
assert 'if "workflow_nodes" in tables:' in source
assert 'if "workflow_templates" in tables:' in source
def test_migration_state_check_script_exists():
source = Path("scripts/check_migration_state.py").read_text(encoding="utf-8")
assert "missing alembic_version table" in source
assert "studies" in source
assert "subjects" in source
assert "monitoring_visit_issues" in source
def test_remove_qa_role_migration_casts_json_permissions_for_key_lookup():
source = Path("alembic/versions/20260521_01_remove_qa_role.py").read_text(encoding="utf-8")
assert "permissions ? 'QA'" not in source
assert "permissions::jsonb ? 'QA'" in source
def test_role_template_copy_migration_updates_display_copy_for_current_role_keys():
source = Path("alembic/versions/20260522_01_update_role_template_copy.py").read_text(encoding="utf-8")
assert "项目负责人,统筹项目全局,协调进度、资源与关键决策。" in source
assert "负责各中心临床监查执行,跟进现场质量、数据和问题闭环。" in source
assert "负责合同、药品及相关项目事务管理,保障执行支持与物资协同。" in source
assert "负责医学审核与稽查,关注质量风险、合规性和医学一致性。" in source
assert "负责药物警戒相关工作,跟踪安全性事件并支持风险评估。" in source
assert '"IMP": ("CTA"' in source
assert '"MEDICAL_REVIEW": ("QA"' in source
assert "category = 'QA'" not in source
def test_permission_template_migrations_do_not_seed_stale_startup_permissions():
stale_keys = (
"budget:create",
"budget:list",
"budget:read",
"budget:update",
"budget:delete",
"timeline:create",
"timeline:list",
"timeline:read",
"timeline:update",
"timeline:delete",
)
for path in Path("alembic/versions").glob("*.py"):
if path.name == "20260527_04_remove_stale_startup_permissions.py":
continue
source = path.read_text(encoding="utf-8")
for key in stale_keys:
assert key not in source, f"{key} should not be seeded by {path}"
def test_legacy_startup_ethics_permission_keys_are_removed_by_followup_migration():
source = Path("alembic/versions/20260527_05_remove_legacy_startup_ethics_permission_keys.py").read_text(encoding="utf-8")
assert '"feasibility:create"' in source
assert '"feasibility:list"' in source
assert '"feasibility:read"' in source
assert '"feasibility:update"' in source
assert '"feasibility:delete"' in source
assert '"ethics:create"' in source
assert '"ethics:list"' in source
assert '"ethics:read"' in source
assert '"ethics:update"' in source
assert '"ethics:delete"' in source
assert "startup_initiation:" not in source
assert "startup_ethics:" not in source
assert "DELETE FROM api_endpoint_permissions" in source
assert "api_endpoint_permissions" in source
assert "permission_templates" in source
assert "permission_template_versions" in source
def test_precautions_migration_renames_table_and_attachment_entity_type():
source = Path("alembic/versions/20260527_08_rename_knowledge_notes_to_precautions.py").read_text(encoding="utf-8")
assert 'rename_table("knowledge_notes", "precautions")' in source
assert "entity_type = 'precaution'" in source
assert "entity_type = 'knowledge_note'" in source
assert "api_endpoint_permissions" in source
assert "permission_templates" in source
def test_etmf_migration_reuses_existing_document_scope_type():
source = Path("alembic/versions/20260527_09_add_etmf_nodes.py").read_text(encoding="utf-8")
assert "postgresql.ENUM(" in source
assert 'name="document_scope_type"' in source
assert "create_type=False" in source
assert "scope_type = sa.Enum" not in source
@@ -1,72 +0,0 @@
import uuid
from datetime import datetime, timezone
import pytest
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from app.crud import monitoring_visit_issue as issue_crud
from app.models.monitoring_visit_issue import MonitoringVisitIssue
from app.schemas.monitoring_visit_issue import MonitoringVisitIssueCreate
@pytest.mark.asyncio
async def test_monitoring_visit_issue_template_fields_can_be_filtered(tmp_path):
db_path = tmp_path / "monitoring-issues.db"
engine = create_async_engine(f"sqlite+aiosqlite:///{db_path}", future=True)
SessionLocal = async_sessionmaker(engine, expire_on_commit=False)
async with engine.begin() as conn:
await conn.run_sync(MonitoringVisitIssue.__table__.create)
study_id = uuid.UUID("11111111-1111-1111-1111-111111111111")
site_id = uuid.UUID("22222222-2222-2222-2222-222222222222")
async with SessionLocal() as session:
created = await issue_crud.create_issue(
session,
study_id,
MonitoringVisitIssueCreate(
issue_no="MV-001",
site_id=site_id,
category="原始记录",
subject_code="SUBJ-001",
status="OPEN",
severity="严重",
mark="SDV",
visit_cycle="V1",
center_query="请补充签名日期",
center_latest_reply="待中心回复",
rectification_completed=False,
due_at=datetime(2026, 5, 1, tzinfo=timezone.utc),
),
created_by=None,
)
assert created.site_id == site_id
assert created.severity == "严重"
assert created.mark == "SDV"
assert created.visit_cycle == "V1"
assert created.center_query == "请补充签名日期"
assert created.center_latest_reply == "待中心回复"
assert created.rectification_completed is False
matched = await issue_crud.list_issues(
session,
study_id,
site_id=site_id,
severity="严重",
mark="SDV",
visit_cycle="V1",
rectification_completed=False,
due_from=datetime(2026, 5, 1, tzinfo=timezone.utc).date(),
due_to=datetime(2026, 5, 1, tzinfo=timezone.utc).date(),
)
unmatched = await issue_crud.list_issues(
session,
study_id,
site_id=uuid.UUID("33333333-3333-3333-3333-333333333333"),
)
await engine.dispose()
assert [item.issue_no for item in matched] == ["MV-001"]
assert unmatched == []
+401 -4
View File
@@ -1,27 +1,59 @@
"""监控API测试:权限系统监控API端点验证。""" """监控API测试:权限系统监控API端点验证。"""
import inspect
import uuid import uuid
import pytest import pytest
from fastapi import HTTPException
from sqlalchemy import text from sqlalchemy import text
from app.core.permission_monitor import set_permission_monitor, PermissionMonitor from app.core.permission_monitor import set_permission_monitor, PermissionMonitor
from app.api.v1 import permission_monitoring from app.api.v1 import permission_monitoring
from app.api.v1.system_permissions import list_system_permissions
class FakeIpInfo: class FakeIpInfo:
def __init__(self, province: str, city: str, isp: str = "电信") -> None: def __init__(self, province: str, city: str, isp: str = "电信", country: str = "中国") -> None:
self.country = "中国" self.country = country
self.province = province self.province = province
self.city = city self.city = city
self.isp = isp self.isp = isp
self.location = f"中国 / {province} / {city} / {isp}" self.location = " / ".join(part for part in [country, province, city, isp] if part)
class AdminUserStub: class AdminUserStub:
is_admin = True is_admin = True
class ProjectPmUserStub:
id = uuid.uuid4()
is_admin = False
@pytest.mark.asyncio
async def test_resolve_monitoring_scope_rejects_project_pm(db_session):
"""系统监测模块仅允许系统管理员访问,项目 PM 不再具备监测范围。"""
with pytest.raises(HTTPException) as exc_info:
await permission_monitoring.resolve_monitoring_scope(db_session, ProjectPmUserStub())
assert exc_info.value.status_code == 403
@pytest.mark.asyncio
async def test_system_permission_monitoring_definitions_are_admin_only():
"""系统级权限定义中的系统监测模块不应再标注 PM 角色。"""
data = await list_system_permissions()
monitoring_items = [
item for item in data["permissions"]
if item["module"] == "system_monitoring"
]
assert monitoring_items
assert data["module_labels"]["system_monitoring"] == "系统监测"
assert all(item["roles"] == ["ADMIN"] for item in monitoring_items)
assert all("PM" not in item["roles"] for item in monitoring_items)
async def _seed_permission_log(db_session, study_id: uuid.UUID, user_id: uuid.UUID, *, allowed: bool, elapsed_ms: float) -> None: async def _seed_permission_log(db_session, study_id: uuid.UUID, user_id: uuid.UUID, *, allowed: bool, elapsed_ms: float) -> None:
study_exists = ( study_exists = (
await db_session.execute(text("SELECT id FROM studies WHERE id = :id"), {"id": str(study_id)}) await db_session.execute(text("SELECT id FROM studies WHERE id = :id"), {"id": str(study_id)})
@@ -495,6 +527,117 @@ async def test_ip_locations_keeps_private_network_location_label(db_session, mon
assert result["items"][0]["city"] == "" assert result["items"][0]["city"] == ""
@pytest.mark.asyncio
async def test_ip_locations_includes_abnormal_security_ips(db_session, monkeypatch):
"""来源分析应同时统计安全事件中的异常来源 IP。"""
await db_session.execute(text("DELETE FROM permission_access_logs"))
await db_session.execute(text("DELETE FROM security_access_logs"))
await db_session.commit()
study_id = "00000000-0000-0000-0000-000000000801"
user_id = "00000000-0000-0000-0000-000000000901"
await db_session.execute(
text(
"""
INSERT INTO studies (id, code, name, status, is_locked, visit_schedule, active_roles)
VALUES (:id, :code, :name, :status, :is_locked, :visit_schedule, :active_roles)
"""
),
{
"id": study_id,
"code": "IP-LOCATION-SECURITY-STUDY",
"name": "IP Location Security Study",
"status": "ACTIVE",
"is_locked": False,
"visit_schedule": "[]",
"active_roles": "[]",
},
)
await db_session.execute(
text(
"""
INSERT INTO users (id, email, password_hash, full_name, clinical_department, is_admin, status)
VALUES (:id, :email, :password_hash, :full_name, :clinical_department, :is_admin, :status)
"""
),
{
"id": user_id,
"email": "source-analysis@example.com",
"password_hash": "hash",
"full_name": "来源分析用户",
"clinical_department": "临床运营",
"is_admin": False,
"status": "ACTIVE",
},
)
await db_session.execute(
text(
"""
INSERT INTO permission_access_logs
(id, study_id, user_id, endpoint_key, role, allowed, elapsed_ms, ip_address, created_at)
VALUES
(:id, :study_id, :user_id, :endpoint_key, :role, :allowed, :elapsed_ms, :ip_address, CURRENT_TIMESTAMP)
"""
),
{
"id": str(uuid.uuid4()),
"study_id": study_id,
"user_id": user_id,
"endpoint_key": "admin.permissions.read",
"role": "PM",
"allowed": True,
"elapsed_ms": 3.2,
"ip_address": "10.3.1.1",
},
)
for ip_address in ["8.8.8.8", "1.1.1.1"]:
await db_session.execute(
text(
"""
INSERT INTO security_access_logs
(id, method, path, status_code, elapsed_ms, client_ip, user_agent, auth_status, user_identifier, created_at)
VALUES
(:id, :method, :path, :status_code, :elapsed_ms, :client_ip, :user_agent, :auth_status, :user_identifier, CURRENT_TIMESTAMP)
"""
),
{
"id": str(uuid.uuid4()),
"method": "GET",
"path": "/api/v1/admin",
"status_code": 403,
"elapsed_ms": 8.5,
"client_ip": ip_address,
"user_agent": "pytest",
"auth_status": "ANONYMOUS",
"user_identifier": None,
},
)
await db_session.commit()
ip_info_by_address = {
"10.3.1.1": FakeIpInfo("", "", "", "局域网"),
"8.8.8.8": FakeIpInfo("California", "Mountain View", "Google", "United States"),
"1.1.1.1": FakeIpInfo("Queensland", "Brisbane", "Cloudflare", "Australia"),
}
monkeypatch.setattr(
permission_monitoring,
"resolve_ip_location",
lambda ip: ip_info_by_address[ip],
)
result = await permission_monitoring.get_ip_locations(db=db_session, _=AdminUserStub(), days=7, limit=10)
locations = {item["country"]: item for item in result["items"]}
assert result["summary"]["total_count"] == 3
assert result["summary"]["denied_count"] == 2
assert result["summary"]["unique_ip_count"] == 3
assert result["summary"]["unique_user_count"] == 1
assert locations["United States"]["total_count"] == 1
assert locations["United States"]["denied_count"] == 1
assert locations["Australia"]["total_count"] == 1
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_access_logs_include_user_behavior_summary(db_session): async def test_access_logs_include_user_behavior_summary(db_session):
"""访问日志应返回用户行为审计汇总和用户排行。""" """访问日志应返回用户行为审计汇总和用户排行。"""
@@ -606,10 +749,121 @@ async def test_access_logs_include_user_behavior_summary(db_session):
assert user_a_ips == {"10.1.1.1", "10.1.1.2"} assert user_a_ips == {"10.1.1.1", "10.1.1.2"}
def test_access_logs_user_stats_query_avoids_postgresql_uuid_max():
"""访问日志用户统计不能对 UUID 字段使用 maxPostgreSQL 不支持 max(uuid)。"""
source = inspect.getsource(permission_monitoring.get_access_logs)
assert "func.max(PermissionAccessLog.user_id)" not in source
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_security_access_logs_include_anonymous_ip_attempts(db_session): async def test_access_logs_ip_ranking_aggregates_by_ip_total(db_session):
"""IP 访问排行应按同一 IP 的整体访问量聚合排序,而不是按用户/IP/角色拆分。"""
await db_session.execute(text("DELETE FROM permission_access_logs"))
await db_session.commit()
study_id = "00000000-0000-0000-0000-000000001201"
user_a = "00000000-0000-0000-0000-000000001301"
user_b = "00000000-0000-0000-0000-000000001302"
await db_session.execute(
text(
"""
INSERT INTO studies (id, code, name, status, is_locked, visit_schedule, active_roles)
VALUES (:id, :code, :name, :status, :is_locked, :visit_schedule, :active_roles)
"""
),
{
"id": study_id,
"code": "ACCESS-IP-RANK-STUDY",
"name": "Access IP Rank Study",
"status": "ACTIVE",
"is_locked": False,
"visit_schedule": "[]",
"active_roles": "[]",
},
)
for user_id, email, name in [
(user_a, "ip-rank-a@example.com", "IP排行用户A"),
(user_b, "ip-rank-b@example.com", "IP排行用户B"),
]:
await db_session.execute(
text(
"""
INSERT INTO users (id, email, password_hash, full_name, clinical_department, is_admin, status)
VALUES (:id, :email, :password_hash, :full_name, :clinical_department, :is_admin, :status)
"""
),
{
"id": user_id,
"email": email,
"password_hash": "hash",
"full_name": name,
"clinical_department": "临床运营",
"is_admin": False,
"status": "ACTIVE",
},
)
rows = [
(user_a, "183.230.169.20", True),
(user_a, "183.230.169.20", True),
(user_b, "183.230.169.20", False),
(user_a, "45.148.10.95", False),
(user_b, "52.53.218.145", False),
]
for user, ip_address, allowed in rows:
await db_session.execute(
text(
"""
INSERT INTO permission_access_logs
(id, study_id, user_id, endpoint_key, role, allowed, elapsed_ms, ip_address, created_at)
VALUES
(:id, :study_id, :user_id, :endpoint_key, :role, :allowed, :elapsed_ms, :ip_address, CURRENT_TIMESTAMP)
"""
),
{
"id": str(uuid.uuid4()),
"study_id": study_id,
"user_id": user,
"endpoint_key": "admin.permissions.read",
"role": "CRA",
"allowed": allowed,
"elapsed_ms": 4.0,
"ip_address": ip_address,
},
)
await db_session.commit()
result = await permission_monitoring.get_access_logs(
db=db_session,
_=AdminUserStub(),
study_id=None,
user_id=None,
endpoint_key=None,
role=None,
allowed=None,
start_time=None,
end_time=None,
page=1,
page_size=50,
)
assert result["user_stats"][0]["sample_ip_address"] == "183.230.169.20"
assert result["user_stats"][0]["total_count"] == 3
assert result["user_stats"][0]["denied_count"] == 1
assert {stat["sample_ip_address"] for stat in result["user_stats"][1:]} == {"52.53.218.145", "45.148.10.95"}
@pytest.mark.asyncio
async def test_security_access_logs_include_anonymous_ip_attempts(db_session, monkeypatch):
"""安全访问日志应覆盖未登录或未知账号的底层访问尝试。""" """安全访问日志应覆盖未登录或未知账号的底层访问尝试。"""
await db_session.execute(text("DELETE FROM security_access_logs")) await db_session.execute(text("DELETE FROM security_access_logs"))
monkeypatch.setattr(
permission_monitoring,
"resolve_ip_location",
lambda ip: FakeIpInfo("上海市", "上海市", "联通"),
)
await db_session.execute( await db_session.execute(
text( text(
""" """
@@ -636,5 +890,148 @@ async def test_security_access_logs_include_anonymous_ip_attempts(db_session):
assert result["summary"]["anonymous_count"] == 1 assert result["summary"]["anonymous_count"] == 1
assert result["summary"]["error_count"] == 1 assert result["summary"]["error_count"] == 1
assert result["items"][0]["client_ip"] == "203.0.113.10" assert result["items"][0]["client_ip"] == "203.0.113.10"
assert result["items"][0]["ip_location"] == "中国 / 上海市 / 上海市 / 联通"
assert result["items"][0]["ip_country"] == "中国"
assert result["items"][0]["ip_province"] == "上海市"
assert result["items"][0]["ip_city"] == "上海市"
assert result["items"][0]["ip_isp"] == "联通"
assert result["items"][0]["account_label"] == "未知账号" assert result["items"][0]["account_label"] == "未知账号"
assert result["items"][0]["status_code"] == 401 assert result["items"][0]["status_code"] == 401
@pytest.mark.asyncio
async def test_security_access_logs_classify_sensitive_path_probe(db_session):
"""安全中心应把敏感路径探测识别为严重安全事件。"""
await db_session.execute(text("DELETE FROM security_access_logs"))
await db_session.execute(
text(
"""
INSERT INTO security_access_logs
(id, method, path, status_code, elapsed_ms, client_ip, user_agent, auth_status, user_identifier, created_at)
VALUES
('00000000-0000-4000-8000-000000000502', 'GET', '/api/.git/config', 404, 0.7,
'198.51.100.20', 'probe-bot/1.0', 'ANONYMOUS', NULL, CURRENT_TIMESTAMP)
"""
)
)
await db_session.commit()
result = await permission_monitoring.get_security_access_logs(
db=db_session,
_=AdminUserStub(),
status_min=400,
auth_status=None,
page=1,
page_size=20,
)
assert result["items"][0]["category"] == "PROBE"
assert result["items"][0]["severity"] == "CRITICAL"
@pytest.mark.asyncio
async def test_security_access_logs_prioritize_sensitive_probe_over_foreign_ip(db_session, monkeypatch):
"""敏感路径探测应优先于海外 IP 分类,避免降低风险等级。"""
await db_session.execute(text("DELETE FROM security_access_logs"))
monkeypatch.setattr(
permission_monitoring,
"resolve_ip_location",
lambda ip: FakeIpInfo("South Holland", "", "", "Netherlands"),
)
await db_session.execute(
text(
"""
INSERT INTO security_access_logs
(id, method, path, status_code, elapsed_ms, client_ip, user_agent, auth_status, user_identifier, created_at)
VALUES
('00000000-0000-4000-8000-000000000505', 'GET', '/api/.git/config', 404, 0.7,
'45.148.10.95', 'probe-bot/1.0', 'ANONYMOUS', NULL, CURRENT_TIMESTAMP)
"""
)
)
await db_session.commit()
result = await permission_monitoring.get_security_access_logs(
db=db_session,
_=AdminUserStub(),
status_min=400,
auth_status=None,
page=1,
page_size=20,
)
assert result["items"][0]["category"] == "PROBE"
assert result["items"][0]["severity"] == "CRITICAL"
@pytest.mark.asyncio
async def test_security_access_logs_classify_non_china_ip_as_abnormal(db_session, monkeypatch):
"""安全事件明细应把非中国公网 IP 归类为异常 IP。"""
await db_session.execute(text("DELETE FROM security_access_logs"))
monkeypatch.setattr(
permission_monitoring,
"resolve_ip_location",
lambda ip: FakeIpInfo("California", "San Jose", "Google", "United States"),
)
await db_session.execute(
text(
"""
INSERT INTO security_access_logs
(id, method, path, status_code, elapsed_ms, client_ip, user_agent, auth_status, user_identifier, created_at)
VALUES
('00000000-0000-4000-8000-000000000504', 'GET', '/api/v1/studies/', 401, 8.4,
'52.53.218.145', 'foreign-client/1.0', 'INVALID_TOKEN', NULL, CURRENT_TIMESTAMP)
"""
)
)
await db_session.commit()
result = await permission_monitoring.get_security_access_logs(
db=db_session,
_=AdminUserStub(),
status_min=400,
auth_status=None,
page=1,
page_size=20,
)
item = result["items"][0]
assert item["category"] == "ABNORMAL_IP"
assert item["severity"] == "HIGH"
assert item["ip_location"] == "United States / California / San Jose / Google"
assert item["ip_country"] == "United States"
@pytest.mark.asyncio
async def test_security_access_logs_resolve_public_ip_with_packaged_ip2region(db_session):
"""安全中心公网 IP 应复用 ip2region 解析结果,避免前端只能显示未知位置。"""
await db_session.execute(text("DELETE FROM security_access_logs"))
await db_session.execute(
text(
"""
INSERT INTO security_access_logs
(id, method, path, status_code, elapsed_ms, client_ip, user_agent, auth_status, user_identifier, created_at)
VALUES
('00000000-0000-4000-8000-000000000503', 'GET', '/api/v1/auth/login', 404, 1.2,
'52.53.218.145', 'probe-bot/1.0', 'ANONYMOUS', NULL, CURRENT_TIMESTAMP)
"""
)
)
await db_session.commit()
result = await permission_monitoring.get_security_access_logs(
db=db_session,
_=AdminUserStub(),
status_min=400,
auth_status=None,
page=1,
page_size=20,
)
item = result["items"][0]
assert item["client_ip"] == "52.53.218.145"
assert item["ip_location"] not in {"", "公网", "未知"}
assert item["ip_country"] == "United States"
assert item["ip_province"] == "California"
assert item["ip_city"] == "San Jose"
assert item["category"] == "ABNORMAL_IP"
+21 -13
View File
@@ -274,6 +274,27 @@ async def test_dev_login_allows_plaintext_only_in_development(client_and_db):
assert resp.json()["access_token"] assert resp.json()["access_token"]
@pytest.mark.asyncio
async def test_avatar_upload_rejects_non_image_files(client_and_db):
client, _ = client_and_db
original_env = settings.ENV
settings.ENV = "development"
try:
login_resp = await client.post("/api/v1/auth/dev-login", json={"email": "admin@test.com", "password": "admin123"})
finally:
settings.ENV = original_env
token = login_resp.json()["access_token"]
resp = await client.post(
"/api/v1/auth/me/avatar",
files={"file": ("avatar.txt", b"not an image", "text/plain")},
headers={"Authorization": f"Bearer {token}"},
)
assert resp.status_code == 400
assert resp.json()["detail"] == "头像仅支持图片格式"
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_dev_login_is_disabled_outside_development(client_and_db): async def test_dev_login_is_disabled_outside_development(client_and_db):
client, _ = client_and_db client, _ = client_and_db
@@ -357,16 +378,3 @@ async def test_login_challenge_cannot_be_reused(client_and_db):
assert first.status_code == 200 assert first.status_code == 200
assert second.status_code == 401 assert second.status_code == 401
@pytest.mark.asyncio
async def test_unlock_requires_encrypted_password(client_and_db):
client, _ = client_and_db
plaintext = await client.post("/api/v1/auth/unlock", json={"email": "admin@test.com", "password": "admin123"})
encrypted = await client.post(
"/api/v1/auth/unlock",
json=await encrypted_auth_payload(client, "admin@test.com", "admin123"),
)
assert plaintext.status_code == 422
assert encrypted.status_code == 200
@@ -1,25 +0,0 @@
"""旧合同基础信息模块移除测试"""
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def test_api_router_no_longer_registers_legacy_finance_contracts():
router_source = (ROOT / "app" / "api" / "v1" / "router.py").read_text()
assert "finance_contracts" not in router_source
assert "finance-contracts" not in router_source
def test_active_backend_code_no_longer_imports_finance_contract_model():
checked_paths = [
ROOT / "app" / "db" / "base.py",
ROOT / "app" / "crud" / "study.py",
ROOT / "app" / "crud" / "site.py",
ROOT / "app" / "crud" / "overview.py",
]
for path in checked_paths:
assert "FinanceContract" not in path.read_text()
@@ -1,40 +0,0 @@
import uuid
import pytest
from app.models.user import User
from app.services.site_contact_display import build_contact_display
def make_user(user_id: uuid.UUID, full_name: str, email: str) -> User:
return User(
id=user_id,
email=email,
password_hash="hash",
full_name=full_name,
clinical_department="PMO",
)
def test_build_contact_display_resolves_comma_separated_user_ids():
first_id = uuid.uuid4()
second_id = uuid.uuid4()
users = {
first_id: make_user(first_id, "张三", "zhangsan@example.com"),
second_id: make_user(second_id, "李四", "lisi@example.com"),
}
display = build_contact_display(f"{first_id}, {second_id}", users)
assert display == "张三、李四"
@pytest.mark.parametrize("role", ["PM", "CRA", "PV", "QA", "CTA"])
def test_build_contact_display_is_role_independent_for_site_read_roles(role: str):
user_id = uuid.uuid4()
users = {user_id: make_user(user_id, "周成", "zhoucheng@example.com")}
display = build_contact_display(str(user_id), users)
assert role
assert display == "周成"
+12
View File
@@ -8,12 +8,14 @@ from app.crud import study as study_crud
class _FakeDb: class _FakeDb:
def __init__(self): def __init__(self):
self.tables = [] self.tables = []
self.operations = []
self.committed = False self.committed = False
async def execute(self, stmt): async def execute(self, stmt):
table = getattr(getattr(stmt, "table", None), "name", None) table = getattr(getattr(stmt, "table", None), "name", None)
if table: if table:
self.tables.append(table) self.tables.append(table)
self.operations.append((getattr(stmt, "__visit_name__", ""), table))
async def commit(self): async def commit(self):
self.committed = True self.committed = True
@@ -29,3 +31,13 @@ async def test_delete_study_removes_contract_fee_payments_before_contract_fees()
assert "contract_fees" in db.tables assert "contract_fees" in db.tables
assert db.tables.index("contract_fee_payments") < db.tables.index("contract_fees") assert db.tables.index("contract_fee_payments") < db.tables.index("contract_fees")
assert db.committed is True assert db.committed is True
@pytest.mark.asyncio
async def test_delete_study_preserves_audit_logs():
db = _FakeDb()
await study_crud.delete(db, uuid.uuid4())
assert ("delete", "audit_logs") not in db.operations
assert ("update", "audit_logs") in db.operations
@@ -1,54 +0,0 @@
from datetime import date, datetime
from types import SimpleNamespace
import uuid
from app.crud.subject import _validate_actual_medication_count
from app.schemas.subject import SubjectRead, SubjectUpdate
def test_subject_update_accepts_actual_medication_count():
payload = SubjectUpdate(actual_medication_count=12)
assert payload.actual_medication_count == 12
def test_subject_update_accepts_screening_date():
payload = SubjectUpdate(screening_date=date(2026, 5, 25))
assert payload.screening_date == date(2026, 5, 25)
def test_subject_update_does_not_expose_status_input():
assert "status" not in SubjectUpdate.model_fields
def test_subject_read_includes_actual_medication_count():
subject = SimpleNamespace(
id=uuid.UUID("00000000-0000-0000-0000-000000000001"),
study_id=uuid.UUID("00000000-0000-0000-0000-000000000002"),
site_id=uuid.UUID("00000000-0000-0000-0000-000000000003"),
subject_no="S001",
status="ENROLLED",
screening_date=None,
consent_date=None,
enrollment_date=None,
baseline_date=None,
completion_date=None,
actual_medication_count=10,
drop_reason=None,
created_at=datetime(2026, 5, 9, 0, 0, 0),
updated_at=datetime(2026, 5, 9, 0, 0, 0),
)
data = SubjectRead.model_validate(subject)
assert data.actual_medication_count == 10
def test_actual_medication_count_cannot_be_negative():
try:
_validate_actual_medication_count(-1)
except ValueError as exc:
assert "实际用药次数不能小于0" in str(exc)
else:
raise AssertionError("negative actual medication count should be rejected")
+23 -3
View File
@@ -19,10 +19,28 @@ services:
container_name: ctms_frontend_dev container_name: ctms_frontend_dev
restart: always restart: always
working_dir: /app working_dir: /app
command: sh -c "npm ci && npm run dev -- --host 0.0.0.0" command:
- sh
- -c
- |
set -e
lock_hash="$$(sha256sum package-lock.json | awk '{print $$1}')"
marker="node_modules/.ctms-package-lock.sha256"
if [ ! -f "$$marker" ] || [ "$$(cat "$$marker")" != "$$lock_hash" ]; then
npm ci
printf '%s' "$$lock_hash" > "$$marker"
fi
npm run dev -- --host 0.0.0.0
environment: environment:
NPM_CONFIG_REGISTRY: ${NPM_CONFIG_REGISTRY:-https://registry.npmmirror.com}
VITE_RUNTIME_ENV: ${ENV:-development} VITE_RUNTIME_ENV: ${ENV:-development}
VITE_ALLOW_INSECURE_DEV_LOGIN: ${VITE_ALLOW_INSECURE_DEV_LOGIN:-true} VITE_ALLOW_INSECURE_DEV_LOGIN: ${VITE_ALLOW_INSECURE_DEV_LOGIN:-true}
healthcheck:
test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:5173/"]
interval: 2s
timeout: 2s
retries: 90
start_period: 5s
volumes: volumes:
- ./frontend:/app - ./frontend:/app
- frontend_node_modules:/app/node_modules - frontend_node_modules:/app/node_modules
@@ -42,8 +60,10 @@ services:
- ./nginx/certs:/etc/nginx/certs:ro - ./nginx/certs:/etc/nginx/certs:ro
- ./frontend/public/favicon.ico:/usr/share/nginx/html/favicon.ico:ro - ./frontend/public/favicon.ico:/usr/share/nginx/html/favicon.ico:ro
depends_on: depends_on:
- backend backend:
- frontend-dev condition: service_started
frontend-dev:
condition: service_healthy
networks: networks:
- ctms_net - ctms_net
+5
View File
@@ -1,6 +1,7 @@
services: services:
db: db:
image: postgres:15-alpine image: postgres:15-alpine
pull_policy: if_not_present
container_name: ctms_db container_name: ctms_db
restart: always restart: always
environment: environment:
@@ -22,6 +23,7 @@ services:
build: build:
context: ./backend context: ./backend
dockerfile: Dockerfile dockerfile: Dockerfile
pull: false
container_name: ctms_backend container_name: ctms_backend
restart: always restart: always
environment: environment:
@@ -40,6 +42,7 @@ services:
build: build:
context: ./backend context: ./backend
dockerfile: Dockerfile dockerfile: Dockerfile
pull: false
command: python scripts/init_production.py command: python scripts/init_production.py
environment: environment:
DATABASE_URL: postgresql+asyncpg://ctms_user:secret_password@db/ctms_db DATABASE_URL: postgresql+asyncpg://ctms_user:secret_password@db/ctms_db
@@ -58,7 +61,9 @@ services:
build: build:
context: . context: .
dockerfile: nginx/Dockerfile dockerfile: nginx/Dockerfile
pull: false
args: args:
NPM_CONFIG_REGISTRY: ${NPM_CONFIG_REGISTRY:-https://registry.npmmirror.com}
VITE_RUNTIME_ENV: ${ENV:-production} VITE_RUNTIME_ENV: ${ENV:-production}
VITE_ALLOW_INSECURE_DEV_LOGIN: ${VITE_ALLOW_INSECURE_DEV_LOGIN:-false} VITE_ALLOW_INSECURE_DEV_LOGIN: ${VITE_ALLOW_INSECURE_DEV_LOGIN:-false}
container_name: ctms_nginx container_name: ctms_nginx
+609
View File
@@ -0,0 +1,609 @@
# Git 仓库同步与在线更新指南
本文档包含两部分:
1. **在线更新功能**:使用 `scripts/update.sh` 从远程仓库拉取代码并自动部署
2. **双仓库推送配置**:配置 CTMS 项目的 GitHub 和 Gitea 双仓库同步
---
# 第一部分:在线更新功能
## 功能特性
### 🔐 安全保障
- **项目身份验证**:自动检测项目特征文件,防止拉取错误仓库
- **自动备份**:拉取前创建 Git tag 备份点,支持一键回滚
- **工作目录保护**:检测未提交更改,提供暂存或放弃选项
- **多种认证方式**:支持 SSH 密钥、Personal Access Token、用户名密码
### ✨ 易用性
- **交互式操作**:友好的提示和参数预览
- **自动化支持**`--yes` 模式适配 CI/CD 流程
- **灵活配置**:支持命令行参数和环境变量
---
## 快速开始
### 基础用法
```bash
# 交互式更新开发环境(会提示输入仓库地址和凭证)
bash scripts/update.sh dev
# 更新测试环境并指定仓库
bash scripts/update.sh main --repo-url https://github.com/user/ctms-dev.git
# 更新生产环境(指定分支)
bash scripts/update.sh release --repo-url https://gitea.example.com/team/ctms.git --branch main
```
### 认证方式
#### 1. SSH 密钥(推荐,无需输入密码)
```bash
# 前提:已配置 SSH 公钥到 GitHub/Gitea
bash scripts/update.sh dev --repo-url git@github.com:user/ctms-dev.git
```
#### 2. Personal Access Token(推荐用于 HTTPS
```bash
# GitHub 生成 TokenSettings → Developer settings → Personal access tokens
# Gitea 生成 TokenSettings → Applications → Generate New Token
# 交互式输入(用户名输入 git 用户名,密码输入 Token)
bash scripts/update.sh main --repo-url https://github.com/user/ctms-dev.git
# 或通过环境变量(适合自动化)
export GIT_USERNAME="your-username"
export GIT_PASSWORD="ghp_xxxxxxxxxxxxxxxxxxxx" # GitHub Token
bash scripts/update.sh main --repo-url https://github.com/user/ctms-dev.git --yes
```
#### 3. 用户名密码(不推荐,安全性低)
```bash
# 交互式输入
bash scripts/update.sh dev --repo-url https://gitea.example.com/user/ctms.git
# 按提示输入用户名和密码
```
---
## 命令选项
### 环境参数(必需)
- `dev` - 本地开发环境
- `main` - 内网测试/预发布环境
- `release` - 生产环境
### 可选参数
| 参数 | 说明 | 示例 |
|------|------|------|
| `--repo-url <url>` | Git 仓库地址(HTTPS 或 SSH | `--repo-url https://github.com/user/repo.git` |
| `--branch <name>` | 指定拉取的分支(默认当前分支) | `--branch main` |
| `--yes` | 跳过所有交互确认 | `--yes` |
| `--force` | 强制覆盖本地更改(`git reset --hard` | `--force` |
| `--skip-backup` | 跳过自动备份 tag(不推荐) | `--skip-backup` |
| `--skip-build` | 透传给 install.sh:跳过镜像构建 | `--skip-build` |
| `--skip-migrate` | 透传给 install.sh:跳过数据库迁移 | `--skip-migrate` |
| `--verbose` | 透传给 install.sh:显示详细输出 | `--verbose` |
---
## 使用场景
### 场景 1:开发环境日常更新
```bash
# 拉取最新代码并重新部署
bash scripts/update.sh dev --repo-url https://github.com/team/ctms-dev.git
```
**流程**
1. 检查工作目录状态(如有未提交更改,提示处理方式)
2. 创建备份 tag(如 `pre-update-20260623-143022`
3. 拉取远程代码
4. 验证项目身份
5. 自动调用 `install.sh` 重新构建容器和迁移数据库
### 场景 2:生产环境版本升级
```bash
# 使用 Personal Access Token 自动化更新
export GIT_USERNAME="deploy-bot"
export GIT_PASSWORD="ghp_xxxxxxxxxxxxxxxxxxxx"
bash scripts/update.sh release \
--repo-url https://github.com/company/ctms.git \
--branch v1.2.0 \
--yes \
--base-url https://ctms.example.com
```
**注意**:生产环境更新建议:
- 提前在测试环境验证
- 使用 `--branch` 指定稳定版本标签
- 备份数据库(脚本会自动创建 Git tag)
- 准备回滚方案
### 场景 3:强制覆盖本地更改
```bash
# 场景:本地有修改但需要强制同步远程版本
bash scripts/update.sh main --repo-url https://gitea.internal/team/ctms.git --force
```
**警告**`--force` 会执行 `git reset --hard`,所有本地未提交更改将丢失。
### 场景 4CI/CD 自动部署
```yaml
# GitHub Actions 示例
name: Deploy to Main
on:
push:
branches: [main]
jobs:
deploy:
runs-on: self-hosted
steps:
- name: Update CTMS
env:
GIT_USERNAME: ${{ secrets.DEPLOY_USERNAME }}
GIT_PASSWORD: ${{ secrets.DEPLOY_TOKEN }}
run: |
cd /opt/ctms
bash scripts/update.sh main \
--repo-url https://github.com/${{ github.repository }}.git \
--branch main \
--yes \
--skip-backup
```
---
## 故障排查
### 问题 1:认证失败
**症状**
```
✖ 出错 拉取失败,请检查网络连接和仓库权限
```
**解决方案**
1. 确认仓库地址正确
2. 检查凭证是否有效(Token 是否过期)
3. 确认账号有仓库访问权限
4. 对于私有仓库,确保 Token 有 `repo` 权限
### 问题 2:项目身份验证失败
**症状**
```
✖ 出错 项目身份验证失败:未检测到足够的 CTMS 项目特征文件
```
**原因**:拉取的仓库不是 CTMS 项目
**解决方案**
1. 检查 `--repo-url` 是否正确
2. 确认远程仓库确实是 CTMS 项目
3. 检查 `--branch` 参数是否指向正确分支
### 问题 3:合并冲突
**症状**
```
✖ 出错 合并失败,可能存在冲突
```
**解决方案**
**方法 1:手动解决冲突**
```bash
# 查看冲突文件
git status
# 手动编辑解决冲突
vim <conflict-file>
# 完成合并
git add .
git commit -m "Resolve merge conflict"
# 重新运行更新
bash scripts/update.sh dev
```
**方法 2:强制覆盖**
```bash
bash scripts/update.sh dev --force
```
### 问题 4:回滚到更新前版本
```bash
# 查看备份 tag
git tag | grep pre-update
# 回滚(示例)
git reset --hard pre-update-20260623-143022
# 重新部署
bash scripts/install.sh dev --skip-migrate
```
---
## 安全最佳实践
### 1. Personal Access Token 管理
**GitHub**
- 生成路径:Settings → Developer settings → Personal access tokens → Tokens (classic)
- 最小权限:`repo`(私有仓库)或 `public_repo`(公开仓库)
- 设置过期时间,定期轮换
**Gitea**
- 生成路径:User Settings → Applications → Manage Access Tokens
- 权限选择:`read:repository`
### 2. 环境变量保护
```bash
# 不要在脚本中硬编码密码
# ❌ 错误
export GIT_PASSWORD="my-secret-token"
# ✅ 正确:从加密的配置管理系统读取
export GIT_PASSWORD="$(vault kv get -field=token secret/git/deploy)"
```
### 3. 生产环境更新检查清单
- [ ] 已在测试环境验证更新
- [ ] 已备份数据库(除了 Git tag 备份)
- [ ] 已通知相关人员计划维护窗口
- [ ] 准备回滚脚本
- [ ] 确认 `--base-url` 参数正确
- [ ] 使用 `--branch` 指定稳定版本
---
## 常见问题
**Q: 更新会丢失 `.env` 配置吗?**
A: 不会。`.env``.gitignore` 中,不会被 Git 覆盖。
**Q: 更新会清空数据库吗?**
A: 不会。数据库数据持久化在 `pg_data` 目录,不受更新影响。更新只会执行增量迁移(`alembic upgrade head`)。
**Q: 如何跳过数据库迁移?**
A: 使用 `--skip-migrate` 参数:
```bash
bash scripts/update.sh dev --skip-migrate
```
**Q: 支持 GitLab 吗?**
A: 支持。GitLab 使用 HTTPS/SSH 认证方式与 GitHub/Gitea 相同。
**Q: 能否在更新前先查看远程有哪些更新?**
A: 可以手动执行:
```bash
git fetch origin
git log HEAD..origin/main # 查看即将合并的提交
```
---
# 第二部分:双仓库推送配置
本部分说明如何配置 CTMS 项目的 GitHub 和 Gitea 双仓库推送与拉取。
## 仓库信息
- **GitHub 仓库**: `https://github.com/chengchengzhou7/CTMS.git`
- **Gitea 仓库**: `http://119.29.208.238:8080/zhouchengcheng/ctms.git`
- **项目分支**: `main` (主分支)、`dev` (开发分支)、`release` (发布分支)
## 一、配置双推(同时推送到两个仓库)
### 1.1 查看当前配置
```bash
git remote -v
```
### 1.2 配置双推 URL
```bash
# 设置 fetch 源为 GitHub(用于拉取)
git remote set-url origin https://github.com/chengchengzhou7/CTMS.git
# 添加 GitHub 的 push URL
git remote set-url --add --push origin https://github.com/chengchengzhou7/CTMS.git
# 添加 Gitea 的 push URL
git remote set-url --add --push origin http://119.29.208.238:8080/zhouchengcheng/ctms.git
```
### 1.3 验证配置
```bash
git remote -v
```
预期输出:
```
origin https://github.com/chengchengzhou7/CTMS.git (fetch)
origin https://github.com/chengchengzhou7/CTMS.git (push)
origin http://119.29.208.238:8080/zhouchengcheng/ctms.git (push)
```
## 二、账号密码管理
### 2.1 保存凭据(推荐)
使用 Git 凭据管理器保存账号密码,避免每次都输入:
**macOS:**
```bash
git config --global credential.helper osxkeychain
```
**Linux:**
```bash
git config --global credential.helper store
```
**Windows:**
```bash
git config --global credential.helper manager
```
### 2.2 针对不同仓库配置不同凭据
如果 GitHub 和 Gitea 使用不同的账号密码,可以在 `.git/config` 中配置:
```bash
# 编辑 Git 配置
vim .git/config
```
添加凭据辅助配置:
```ini
[credential "https://github.com"]
username = chengchengzhou7
[credential "http://119.29.208.238:8080"]
username = zhouchengcheng
```
## 三、日常操作
### 3.1 推送代码(自动双推)
推送当前分支到两个仓库:
```bash
git push
```
推送指定分支:
```bash
# 推送 dev 分支
git push origin dev
# 推送 main 分支
git push origin main
# 推送 release 分支
git push origin release
```
首次推送新分支需要设置上游:
```bash
git push -u origin <branch-name>
```
### 3.2 拉取代码(从 GitHub
```bash
# 拉取当前分支
git pull
# 拉取指定分支
git pull origin dev
git pull origin main
git pull origin release
```
### 3.3 获取远程更新
```bash
# 获取所有远程分支的更新
git fetch origin
# 查看所有分支(包括远程)
git branch -a
```
## 四、分支管理
### 4.1 切换分支
```bash
# 切换到开发分支
git checkout dev
# 切换到主分支
git checkout main
# 切换到发布分支
git checkout release
```
### 4.2 创建新分支
```bash
# 基于 dev 创建新功能分支
git checkout -b feature/new-feature dev
# 推送到两个远程仓库
git push -u origin feature/new-feature
```
### 4.3 分支合并
```bash
# 将 dev 合并到 release
git checkout release
git merge dev
git push
# 将 release 合并到 main
git checkout main
git merge release
git push
```
## 五、常见问题
### 5.1 其中一个仓库推送失败怎么办?
如果双推时其中一个仓库失败,可以单独推送到失败的仓库:
```bash
# 单独推送到 GitHub
git push https://github.com/chengchengzhou7/CTMS.git dev
# 单独推送到 Gitea
git push http://119.29.208.238:8080/zhouchengcheng/ctms.git dev
```
### 5.2 如何只推送到一个仓库?
临时推送到指定仓库:
```bash
# 只推送到 GitHub
git push https://github.com/chengchengzhou7/CTMS.git
# 只推送到 Gitea
git push http://119.29.208.238:8080/zhouchengcheng/ctms.git
```
### 5.3 密码输入错误或需要更新
清除已保存的凭据:
```bash
# macOS
git credential-osxkeychain erase
host=github.com
protocol=https
[按两次回车]
# 或者使用
git credential reject
host=github.com
protocol=https
[按两次回车]
```
### 5.4 Gitea 首次推送需要创建仓库
如果 Gitea 仓库不存在,需要先在 Gitea 网页上创建空仓库:
1. 访问 `http://119.29.208.238:8080`
2. 登录后点击「新建仓库」
3. 仓库名设置为 `ctms`
4. 不要初始化 README
5. 创建后再执行 `git push`
## 六、配置文件示例
当前项目的 `.git/config` 配置示例:
```ini
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/chengchengzhou7/CTMS.git
fetch = +refs/heads/*:refs/remotes/origin/*
pushurl = https://github.com/chengchengzhou7/CTMS.git
pushurl = http://119.29.208.238:8080/zhouchengcheng/ctms.git
[branch "main"]
remote = origin
merge = refs/heads/main
[branch "dev"]
remote = origin
merge = refs/heads/dev
[branch "release"]
remote = origin
merge = refs/heads/release
```
## 七、工作流建议
### 7.1 日常开发流程
```bash
# 1. 切换到 dev 分支
git checkout dev
# 2. 拉取最新代码
git pull
# 3. 创建功能分支
git checkout -b feature/xxx
# 4. 开发并提交
git add .
git commit -m "feat: 添加新功能"
# 5. 推送功能分支
git push -u origin feature/xxx
# 6. 合并到 dev(通过 PR 或直接合并)
git checkout dev
git merge feature/xxx
git push # 自动推送到 GitHub 和 Gitea
```
### 7.2 发布流程
```bash
# 1. 将 dev 合并到 release
git checkout release
git pull
git merge dev
git push # 双推到两个仓库
# 2. 测试通过后,合并到 main
git checkout main
git pull
git merge release
git push # 双推到两个仓库
# 3. 打标签
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0 # 推送标签也会双推
```
---
**最后更新**: 2026-06-23
**维护者**: Cheng Zhou
+186
View File
@@ -0,0 +1,186 @@
╔══════════════════════════════════════════════════════════════╗
║ ║
║ ✅ 使用帮助优化完成 ║
║ ║
╚══════════════════════════════════════════════════════════════╝
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 优化对比
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【优化前】
用法:
./install.sh
说明:
install.sh 不接受命令行参数。
执行后通过键盘 ↑/↓ 选择安装、更新、卸载、状态等操作,按 Q 退出。
问题:
✗ 内容过于简单,缺少实用信息
✗ 没有说明底层脚本的使用方法
✗ 没有提供常见场景示例
✗ 没有提到新增的在线更新功能
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【优化后】
新增内容:
✓ 主入口说明(保留原有说明)
✓ 4 个底层脚本的详细使用方法
- 安装部署(install.sh
- 在线更新(update.sh)⭐ 重点新增
- 安全卸载(uninstall.sh
- 资源状态(status.sh
✓ 每个脚本的常用选项和参数说明
✓ 实用示例(交互式 + 自动化)
✓ 快速参考(文档链接)
✓ 4 个常见场景(首次部署/日常更新/自动化/回滚)
结构:
1. 主入口说明
2. 底层脚本详解
- 安装部署
- 在线更新(重点)
- 安全卸载
- 资源状态
3. 快速参考
4. 常见场景
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✨ 新增的在线更新说明(重点)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
2. 在线更新
bash scripts/update.sh <环境> [选项]
核心功能:
✓ 从 GitHub/Gitea 远程仓库拉取代码
✓ 自动验证项目身份(防止拉错仓库)
✓ 创建备份 tag(支持一键回滚)
✓ 支持 SSH/Token/密码三种认证方式
常用选项:
--repo-url <url> Git 仓库地址(HTTPS 或 SSH
--branch <name> 指定拉取的分支
--yes 跳过交互确认
--force 强制覆盖本地更改
--skip-backup 跳过自动备份
认证方式:
GIT_USERNAME 环境变量:Git 用户名
GIT_PASSWORD 环境变量:Git 密码或 Token
示例:
# 交互式更新(自动检测仓库地址)
bash scripts/update.sh dev
# 指定仓库地址
bash scripts/update.sh main --repo-url https://github.com/user/ctms.git
# 全自动模式(CI/CD)
export GIT_USERNAME="deploy-bot"
export GIT_PASSWORD="ghp_xxxxxxxxxxxxx"
bash scripts/update.sh release --repo-url <url> --yes
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📖 常见场景示例
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
场景 1:首次部署开发环境
./install.sh
→ 选择"安装部署"→ 选择"dev"
场景 2:更新开发环境到最新代码
./install.sh
→ 选择"滚动更新"→ 选择"dev"
→ 输入仓库地址和凭证
场景 3:生产环境自动化部署
export GIT_USERNAME="deploy-bot"
export GIT_PASSWORD="ghp_xxxxxxxxxxxxx"
bash scripts/update.sh release \
--repo-url https://github.com/company/ctms.git \
--branch v1.2.0 \
--yes \
--base-url https://ctms.example.com
场景 4:回滚到更新前版本
git tag | grep pre-update # 查看备份 tag
git reset --hard pre-update-<时间戳> # 回滚
bash scripts/install.sh dev --skip-migrate
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🎯 优化效果
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
优化前:5 行(简单说明)
优化后:130+ 行(完整指南)
改进维度:
✓ 信息完整性:增加了所有底层脚本的使用说明
✓ 实用性:提供了真实可用的命令示例
✓ 场景覆盖:涵盖交互式、自动化、回滚等场景
✓ 新功能突出:重点说明了在线更新功能
✓ 可读性:结构化分节,层次清晰
✓ 可维护性:与实际脚本功能保持一致
用户收益:
✓ 新手:通过主入口交互式操作即可使用
✓ 进阶:了解底层脚本,可进行精细控制
✓ DevOps:获取自动化部署所需的完整参数
✓ 紧急情况:快速找到回滚等关键操作方法
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📝 测试验证
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
测试命令:
bash install.sh --help
测试结果:
✓ 帮助信息完整显示
✓ 格式美观,层次清晰
✓ 色彩高亮正常工作
✓ 所有示例命令正确
✓ 文档链接路径准确
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🔗 相关文件
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
已修改:
✓ install.shusage 函数优化)
配套文档:
✓ docs/GIT_SYNC_GUIDE.md(在线更新完整指南)
✓ docs/UPDATE_OPTIMIZATION_SUMMARY.md(功能总结)
✓ docs/UPDATE_DEMO.txt(快速演示)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✅ 总结
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
本次优化成果:
1. ✅ 优化了主入口帮助信息(5 行 → 130+ 行)
2. ✅ 新增在线更新功能的完整说明
3. ✅ 提供了 4 个底层脚本的详细用法
4. ✅ 增加了常见场景的实用示例
5. ✅ 添加了快速参考和文档链接
用户体验:
- 交互式菜单中选择"使用帮助"即可查看
- 命令行执行 `bash install.sh --help` 也可查看
- 信息全面、结构清晰、易于理解
现在用户可以通过帮助信息快速了解:
✓ 如何使用主入口进行交互式操作
✓ 如何在自动化场景中调用底层脚本
✓ 如何使用新增的在线更新功能
✓ 如何处理常见场景(部署/更新/回滚)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🎉 优化完成!用户现在可以通过帮助信息快速上手所有功能!
+265
View File
@@ -0,0 +1,265 @@
#!/bin/bash
# CTMS 在线更新功能演示
# 此脚本演示如何使用新的更新功能
cat <<'EOF'
╔══════════════════════════════════════════════════════════════╗
║ ║
║ CTMS 在线更新功能 - 快速演示 ║
║ ║
╚══════════════════════════════════════════════════════════════╝
本演示将展示如何使用优化后的更新脚本从远程仓库拉取代码并自动部署。
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【场景 1】基础更新 - 交互式操作
命令:
bash scripts/update.sh dev
特点:
• 自动检测当前远程仓库地址
• 提示输入 Git 凭证
• 创建自动备份 tag
• 验证项目身份
• 自动部署
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【场景 2】指定仓库地址
命令:
bash scripts/update.sh main \
--repo-url https://github.com/user/ctms.git
特点:
• 明确指定远程仓库
• 适合多仓库环境
• 仍然交互式输入凭证
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【场景 3】全自动模式 - CI/CD 部署
命令:
export GIT_USERNAME="deploy-bot"
export GIT_PASSWORD="ghp_xxxxxxxxxxxxx"
bash scripts/update.sh release \
--repo-url https://github.com/company/ctms.git \
--branch v1.2.0 \
--yes
特点:
✓ 无交互确认
✓ 从环境变量读取凭证
✓ 指定特定分支/标签
✓ 适合自动化部署
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【场景 4】强制覆盖本地更改
命令:
bash scripts/update.sh dev --force
特点:
⚠ 执行 git reset --hard
⚠ 本地更改将丢失
• 适合纯部署环境
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【场景 5】SSH 密钥认证
命令:
bash scripts/update.sh dev \
--repo-url git@github.com:user/ctms.git
特点:
✓ 无需输入密码
✓ 前提:已配置 SSH 公钥
✓ 最安全的方式
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【安全特性】
1. 项目身份验证
• 检查 4 个特征文件
• 防止拉取错误仓库
• 拉取前后各验证一次
2. 自动备份
• 创建带时间戳的 Git tag
• 格式:pre-update-20260623-143022
• 支持一键回滚
3. 工作目录保护
• 检测未提交更改
• 提供 stash/reset/取消 三种选择
• 防止意外丢失代码
4. 凭证安全
• 临时凭证助手
• 自动清理(trap EXIT
• 不暴露在进程列表
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【实战演练】
让我们演示一个完整的更新流程:
步骤 1:查看当前状态
$ git status
$ git log --oneline -5
步骤 2:执行更新(使用当前仓库)
$ bash scripts/update.sh dev
步骤 3:脚本会依次执行:
✓ 检查工作目录状态
✓ 解析仓库地址(自动检测到当前 remote)
✓ 解析分支(使用当前分支:dev)
✓ 显示参数预览并确认
✓ 验证项目身份
✓ 创建备份 tagpre-update-20260623-151022
✓ 配置 Git 认证(提示输入用户名/密码)
✓ 拉取代码
✓ 再次验证项目身份
✓ 调用 install.sh 部署
✓ 显示成功信息
步骤 4:验证更新结果
$ git log --oneline -3
$ docker ps
$ curl http://127.0.0.1:8888/health
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【回滚演示】
如果更新后发现问题,可以快速回滚:
步骤 1:查看备份 tag
$ git tag | grep pre-update
输出示例:
pre-update-20260623-143022
pre-update-20260623-151022
步骤 2:回滚到备份点
$ git reset --hard pre-update-20260623-143022
步骤 3:重新部署
$ bash scripts/install.sh dev --skip-migrate
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【GitHub Actions 集成示例】
在 .github/workflows/deploy.yml 中配置:
name: Auto Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: self-hosted
steps:
- name: Update CTMS
env:
GIT_USERNAME: ${{ secrets.DEPLOY_USERNAME }}
GIT_PASSWORD: ${{ secrets.DEPLOY_TOKEN }}
run: |
cd /opt/ctms
bash scripts/update.sh main \
--repo-url https://github.com/${{ github.repository }}.git \
--branch main \
--yes \
--skip-backup
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【故障排查】
问题 1:认证失败
症状:✖ 出错 拉取失败,请检查网络连接和仓库权限
解决方案:
• 检查仓库地址是否正确
• 确认 Token 未过期
• 确保有仓库访问权限
问题 2:项目身份验证失败
症状:✖ 出错 项目身份验证失败
解决方案:
• 检查 --repo-url 是否指向正确的 CTMS 仓库
• 确认 --branch 参数正确
问题 3:合并冲突
症状:✖ 出错 合并失败,可能存在冲突
解决方案:
• 方法 1:手动解决冲突后重新运行
• 方法 2:使用 --force 强制覆盖
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【最佳实践】
生产环境更新检查清单:
□ 已在测试环境验证更新
□ 已备份数据库
□ 已通知相关人员维护窗口
□ 准备回滚脚本
□ 确认 --base-url 参数正确
□ 使用 --branch 指定稳定版本
Token 安全管理:
✓ 使用 Personal Access Token 而非密码
✓ 设置 Token 过期时间
✓ 最小权限原则(只给 repo 权限)
✓ 定期轮换 Token
✓ 不在代码中硬编码
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【性能优化】
跳过不必要的步骤:
• 跳过镜像构建(仅重启容器):
bash scripts/update.sh dev --skip-build
• 跳过数据库迁移(无 schema 变更):
bash scripts/update.sh dev --skip-migrate
• 跳过备份(信任更新):
bash scripts/update.sh dev --skip-backup
• 组合使用:
bash scripts/update.sh dev --skip-build --skip-migrate
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
【更多帮助】
查看完整文档:
• 使用指南:docs/GIT_SYNC_GUIDE.md
• 优化总结:docs/UPDATE_OPTIMIZATION_SUMMARY.md
• 命令帮助:bash scripts/update.sh --help
运行功能测试:
bash scripts/test_update.sh
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
演示完成!你现在可以开始使用新的更新功能了 🚀
EOF
+270
View File
@@ -0,0 +1,270 @@
# 更新脚本优化完成总结
## 📦 已完成的工作
### 1. 核心脚本优化
- ✅ **文件**: `scripts/update.sh`
- ✅ **大小**: ~600 行
- ✅ **权限**: 可执行(755
### 2. 新增功能
#### 🔐 安全机制
- [x] **项目身份验证**:检测 4 个特征文件(install.sh、docker-compose.yaml、main.py、README.md),防止拉错仓库
- [x] **自动备份**:拉取前创建带时间戳的 Git tag(如 `pre-update-20260623-143022`
- [x] **工作目录保护**:检测未提交更改,提供 3 种处理方式(stash/reset/取消)
- [x] **凭证清理**:使用 trap 确保临时凭证助手在退出时自动清理
#### 🔑 认证方式
- [x] **SSH 密钥**:支持 `git@...` 格式,无需额外配置
- [x] **Personal Access Token**:推荐方式,通过环境变量或交互输入
- [x] **用户名密码**HTTPS 基础认证(不推荐)
- [x] **环境变量支持**`GIT_USERNAME``GIT_PASSWORD`(适合 CI/CD
#### 📋 功能特性
- [x] **仓库地址解析**:自动检测当前 remote 或交互式输入
- [x] **分支指定**:支持 `--branch` 参数或使用当前分支
- [x] **强制模式**`--force` 参数执行 `git reset --hard`
- [x] **参数透传**:支持 `--skip-build``--skip-migrate``--verbose` 等传递给 `install.sh`
- [x] **自动化模式**`--yes` 跳过所有确认,适合 CI/CD
- [x] **美化 UI**:与 `install.sh` 风格一致的 Banner 和进度提示
### 3. 文档完善
- ✅ **文件**: `docs/GIT_SYNC_GUIDE.md`
- ✅ **内容**:
- 第一部分:在线更新功能完整指南(新增)
- 第二部分:双仓库推送配置(原有内容保留)
### 4. 测试脚本
- ✅ **文件**: `scripts/test_update.sh`
- ✅ **测试项**: 7 项功能检查
- ✅ **测试结果**: 6/7 通过
---
## 🎯 使用方法
### 场景 1:基础更新(交互式)
```bash
bash scripts/update.sh dev
```
**流程**
1. 提示输入仓库地址(或使用当前 remote)
2. 检查工作目录状态
3. 提示输入凭证(用户名/密码或 Token)
4. 创建备份 tag
5. 拉取代码
6. 验证项目身份
7. 调用 `install.sh` 部署
### 场景 2:指定仓库地址
```bash
bash scripts/update.sh main --repo-url https://github.com/user/ctms.git
```
### 场景 3:全自动模式(CI/CD)
```bash
export GIT_USERNAME="deploy-bot"
export GIT_PASSWORD="ghp_xxxxxxxxxxxxx"
bash scripts/update.sh release \
--repo-url https://github.com/company/ctms.git \
--branch main \
--yes
```
### 场景 4:强制覆盖本地更改
```bash
bash scripts/update.sh dev --force
```
### 场景 5SSH 认证
```bash
bash scripts/update.sh dev --repo-url git@github.com:user/ctms.git
```
---
## 📖 参数说明
### 必需参数
- `<环境>`: `dev` | `main` | `release`
### 可选参数
| 参数 | 说明 |
|------|------|
| `--repo-url <url>` | Git 仓库地址(HTTPS 或 SSH|
| `--branch <name>` | 指定分支(默认当前分支)|
| `--yes` | 跳过所有确认(自动化模式)|
| `--force` | 强制覆盖本地更改 |
| `--skip-backup` | 跳过备份 tag |
| `--skip-build` | 透传:跳过镜像构建 |
| `--skip-migrate` | 透传:跳过数据库迁移 |
| `--verbose` | 透传:显示详细输出 |
| `--base-url <url>` | 透传:健康检查地址 |
---
## 🛡️ 安全特性
### 1. 项目身份验证
拉取前后各验证一次,确保拉取的是 CTMS 项目:
- 检查 `scripts/install.sh`
- 检查 `docker-compose.yaml`
- 检查 `backend/app/main.py`
- 检查 `README.md`(并验证包含 "CTMS" 关键字)
**至少需要匹配 2 个文件才能通过验证**
### 2. 自动备份机制
```bash
# 拉取前自动创建
pre-update-20260623-143022
# 回滚方法
git reset --hard pre-update-20260623-143022
bash scripts/install.sh dev --skip-migrate
```
### 3. 工作目录保护
检测到未提交更改时,提供 3 种处理方式:
1. **暂存更改**`git stash`):可恢复
2. **放弃更改**`git reset --hard`):不可恢复
3. **取消更新**:安全退出
### 4. 凭证管理
- 临时凭证助手自动清理(trap EXIT)
- 不在进程列表中暴露密码
- 支持环境变量(避免交互式输入)
---
## 🔍 故障排查
### 问题 1:认证失败
```bash
✖ 出错 拉取失败,请检查网络连接和仓库权限
```
**解决**
1. 检查仓库地址是否正确
2. 确认 Token 未过期且有 `repo` 权限
3. 对于私有仓库,确认账号有访问权限
### 问题 2:项目身份验证失败
```bash
✖ 出错 项目身份验证失败:未检测到足够的 CTMS 项目特征文件
```
**解决**:检查 `--repo-url``--branch` 是否指向正确的 CTMS 仓库
### 问题 3:合并冲突
```bash
✖ 出错 合并失败,可能存在冲突
```
**解决**
- 方法 1:手动解决冲突后重新运行
- 方法 2:使用 `--force` 强制覆盖
### 问题 4:回滚
```bash
# 查看备份
git tag | grep pre-update
# 回滚到指定版本
git reset --hard pre-update-20260623-143022
bash scripts/install.sh dev
```
---
## 📊 测试结果
```
✓ 帮助信息正常
✓ Git 仓库检测正常
✓ 项目特征文件检测正常(找到 4 个)
✓ 更新脚本可执行
✓ 当前分支检测正常
✓ 远程仓库地址检测正常
```
---
## 🎨 改进亮点
### 1. 用户体验
- **美化 UI**:与 `install.sh` 统一的 Banner 和配色
- **清晰提示**:每个步骤都有图标和进度说明
- **参数预览**:更新前显示参数确认框
- **友好错误**:详细的错误信息和解决建议
### 2. 灵活性
- **多种认证**:SSH/Token/密码,适应不同场景
- **参数化**:所有配置都可通过参数或环境变量设置
- **透传机制**:支持将参数传递给 `install.sh`
### 3. 可靠性
- **双重验证**:拉取前后各验证项目身份
- **自动备份**:每次更新前创建 Git tag
- **凭证清理**:使用 trap 确保安全退出
- **冲突处理**:提供多种解决方案
### 4. 自动化友好
- **--yes 模式**:无交互确认
- **环境变量**:支持 CI/CD 注入凭证
- **退出码**:失败时返回非 0
---
## 📝 与旧版本对比
### 旧版本(41 行)
```bash
# 仅调用 install.sh,手动执行 git pull
bash scripts/install.sh "$env" "$@"
```
### 新版本(~600 行)
- ✅ 集成 Git 拉取功能
- ✅ 项目身份验证
- ✅ 多种认证方式
- ✅ 自动备份机制
- ✅ 工作目录保护
- ✅ 冲突处理
- ✅ CI/CD 支持
- ✅ 完整的文档
---
## 🚀 后续建议
### 可选增强
1. **Webhook 支持**:监听远程仓库 push 事件自动更新
2. **版本比对**:拉取前显示本地与远程的 commit 差异
3. **回滚历史**:记录所有备份 tag 到日志文件
4. **邮件通知**:更新成功/失败发送邮件
5. **灰度更新**:支持先更新一部分容器观察
### 集成建议
1. 在主入口 `install.sh` 菜单中添加"在线更新"选项
2. 配置 cron 定时任务实现自动更新
3. 集成到 CI/CD pipeline
---
## ✅ 验收检查清单
- [x] 脚本可执行且权限正确
- [x] 帮助信息完整显示
- [x] 支持 3 种认证方式
- [x] 项目身份验证有效
- [x] 自动备份功能正常
- [x] 工作目录保护生效
- [x] 参数透传到 install.sh
- [x] 文档完整且准确
- [x] 测试脚本通过
- [x] 错误处理友好
---
**优化完成时间**: 2026-06-23
**功能状态**: ✅ 生产就绪
**下一步**: 可以开始使用或集成到 CI/CD 流程
+8 -12
View File
@@ -15,21 +15,17 @@ Last Updated: `2026-03-30`
- 同时触发 10 个 API 请求 - 同时触发 10 个 API 请求
- 仅出现 1 次 /auth/extend,其余请求自动重放成功 - 仅出现 1 次 /auth/extend,其余请求自动重放成功
4) Idle lock at 30 minutes 4) Idle auto logout at 30 minutes
- 30 分钟无鼠标/键盘/触摸/滚动且无任何请求 - 30 分钟无鼠标/键盘/触摸/滚动且无任何请求
- 出现锁屏,背景虚化,原页面状态保留 - 自动退出登录并跳转登录页
5) Unlock success 5) Login notice after idle logout
- 锁屏输入正确密码,解锁成功并继续原页面 - 登录页展示 30 分钟无操作自动退出提示
6) Unlock failure threshold 6) Extend failure logout
- 连续错误密码 >= 5 次
- 强制登出并跳登录页
7) Extend failure to lock
- 人为让 token 彻底过期,/auth/extend 返回 401 - 人为让 token 彻底过期,/auth/extend 返回 401
- 不跳登录,进入锁屏并要求密码解锁 - 强制登出并跳登录页,提示登录状态失效
8) 403 permission vs disabled 7) 403 permission vs disabled
- 权限不足接口返回 403 时不跳登录 - 权限不足接口返回 403 时不跳登录
- /auth/extend 或 /auth/unlock 返回 403 时强制登出跳登录 - /auth/extend 返回 403 时强制登出跳登录
-1
View File
@@ -18,7 +18,6 @@
| high | business-draft | `frontend/src/views/admin/ProjectDetail.vue` | 2984 | `localStorage.removeItem` | `storageKey.value` | 立项配置草稿本地兜底,需显式提示未落库 | | high | business-draft | `frontend/src/views/admin/ProjectDetail.vue` | 2984 | `localStorage.removeItem` | `storageKey.value` | 立项配置草稿本地兜底,需显式提示未落库 |
| low | ui-preference | `frontend/src/components/Layout.vue` | 227 | `localStorage.getItem` | `"ctms_sidebar_collapsed"` | UI偏好/上下文缓存 | | low | ui-preference | `frontend/src/components/Layout.vue` | 227 | `localStorage.getItem` | `"ctms_sidebar_collapsed"` | UI偏好/上下文缓存 |
| low | ui-preference | `frontend/src/components/Layout.vue` | 390 | `localStorage.setItem` | `"ctms_sidebar_collapsed"` | UI偏好/上下文缓存 | | low | ui-preference | `frontend/src/components/Layout.vue` | 390 | `localStorage.setItem` | `"ctms_sidebar_collapsed"` | UI偏好/上下文缓存 |
| low | auth-session | `frontend/src/components/LockScreenModal.vue` | 92 | `localStorage.getItem` | `"ctms_last_login_email"` | 认证/会话数据(非业务主数据) |
| low | auth-session | `frontend/src/components/ThreadList.vue` | 72 | `localStorage.getItem` | `"ctms_token"` | 认证/会话数据(非业务主数据) | | low | auth-session | `frontend/src/components/ThreadList.vue` | 72 | `localStorage.getItem` | `"ctms_token"` | 认证/会话数据(非业务主数据) |
| low | auth-session | `frontend/src/components/attachments/AttachmentList.vue` | 132 | `localStorage.getItem` | `"ctms_token"` | 认证/会话数据(非业务主数据) | | low | auth-session | `frontend/src/components/attachments/AttachmentList.vue` | 132 | `localStorage.getItem` | `"ctms_token"` | 认证/会话数据(非业务主数据) |
| low | auth-session | `frontend/src/components/attachments/AttachmentList.vue` | 137 | `localStorage.getItem` | `"ctms_token"` | 认证/会话数据(非业务主数据) | | low | auth-session | `frontend/src/components/attachments/AttachmentList.vue` | 137 | `localStorage.getItem` | `"ctms_token"` | 认证/会话数据(非业务主数据) |
+32 -6
View File
@@ -94,18 +94,44 @@ grep -n "^\\.env\\.\\*$" .gitignore
## 推荐脚本安装 ## 推荐脚本安装
安装脚本入口: 推荐使用根目录交互入口:
```bash ```bash
bash scripts/install-ctms.sh dev|main|release [选项] ./install.sh
```
执行后通过键盘 `↑/↓` 选择安装、更新、卸载、资源状态等操作;`install.sh` 不接受命令行参数。菜单顶部会常驻显示当前 CTMS 容器部署状态,包括已检测到的环境、Compose 项目和容器运行数量。
底层脚本入口(适合自动化或排障时直接调用):
```text
scripts/install.sh 安装:依赖检查、配置准备、构建启动、迁移、健康检查
scripts/update.sh 更新:复用安装流程,不执行 git pull
scripts/uninstall.sh 卸载:默认仅停止并移除容器,不删除数据
scripts/status.sh 状态:使用 docker compose stats --no-stream 采集资源快照并美化展示
scripts/common.sh 公共函数:环境映射、输出样式、危险操作确认
```
安装脚本直接调用格式:
```bash
bash scripts/install.sh dev|main|release [选项]
```
更新、卸载、状态脚本直接调用格式:
```bash
bash scripts/update.sh dev|main|release [安装脚本选项]
bash scripts/uninstall.sh dev|main|release
bash scripts/status.sh dev|main|release
``` ```
常用示例: 常用示例:
```bash ```bash
bash scripts/install-ctms.sh dev bash scripts/install.sh dev
bash scripts/install-ctms.sh main --base-url http://127.0.0.1:8888 bash scripts/install.sh main --base-url http://127.0.0.1:8888
bash scripts/install-ctms.sh release --base-url https://ctms.example.com bash scripts/install.sh release --base-url https://ctms.example.com
``` ```
可选参数: 可选参数:
@@ -143,7 +169,7 @@ docker compose down -v
如果只想查看参数说明: 如果只想查看参数说明:
```bash ```bash
bash scripts/install-ctms.sh --help bash scripts/install.sh --help
``` ```
## 手工安装/排障:dev 分支 ## 手工安装/排障:dev 分支
-65
View File
@@ -84,71 +84,6 @@ async def test_admin_always_allowed(db_session, study_id):
assert result is True assert result is True
``` ```
### 2. 权限配置测试
**文件**: `backend/tests/test_api_permissions_config.py`
```python
import pytest
from app.core.api_permissions import API_ENDPOINT_PERMISSIONS, MODULE_TO_ENDPOINTS
def test_api_endpoint_permissions_structure():
"""测试API端点权限配置结构"""
for endpoint_key, config in API_ENDPOINT_PERMISSIONS.items():
assert "module" in config
assert "action" in config
assert "description" in config
assert "default_roles" in config
assert config["action"] in ["read", "write"]
assert isinstance(config["default_roles"], list)
def test_module_to_endpoints_mapping():
"""测试模块到端点的映射"""
for module, actions in MODULE_TO_ENDPOINTS.items():
assert "read" in actions
assert "write" in actions
assert isinstance(actions["read"], list)
assert isinstance(actions["write"], list)
# 验证所有端点都在API_ENDPOINT_PERMISSIONS中定义
for endpoint_key in actions["read"] + actions["write"]:
assert endpoint_key in API_ENDPOINT_PERMISSIONS
def test_subjects_endpoints_configured():
"""测试subjects模块的端点配置"""
expected_endpoints = [
"POST:/subjects",
"GET:/subjects",
"GET:/subjects/{id}",
"PATCH:/subjects/{id}",
"DELETE:/subjects/{id}",
]
for endpoint_key in expected_endpoints:
assert endpoint_key in API_ENDPOINT_PERMISSIONS
assert API_ENDPOINT_PERMISSIONS[endpoint_key]["module"] == "subjects"
def test_fees_endpoints_configured():
"""测试fees模块的端点配置"""
expected_endpoints = [
"POST:/fees/contracts",
"GET:/fees/contracts",
"GET:/fees/contracts/{id}",
"PATCH:/fees/contracts/{id}",
"DELETE:/fees/contracts/{id}",
"POST:/fees/contracts/{id}/payments",
"PATCH:/fees/payments/{id}",
"DELETE:/fees/payments/{id}",
"POST:/finance/contracts",
"GET:/finance/contracts",
"GET:/finance/contracts/{id}",
"PATCH:/finance/contracts/{id}",
"DELETE:/finance/contracts/{id}",
]
for endpoint_key in expected_endpoints:
assert endpoint_key in API_ENDPOINT_PERMISSIONS
assert API_ENDPOINT_PERMISSIONS[endpoint_key]["module"] == "fees"
```
## 集成测试 ## 集成测试
### 1. 权限管理API测试 ### 1. 权限管理API测试
@@ -37,7 +37,6 @@ Run: `npm run test:unit -- src/views/Login.test.ts`
**Files:** **Files:**
- Modify: `frontend/src/views/Register.vue` - Modify: `frontend/src/views/Register.vue`
- Test: `frontend/src/views/Register.test.ts`
**Step 1: Write failing test** **Step 1: Write failing test**
@@ -45,7 +44,7 @@ Run: `npm run test:unit -- src/views/Login.test.ts`
**Step 2: Run test to verify it fails** **Step 2: Run test to verify it fails**
Run: `npm run test:unit -- src/views/Register.test.ts` Run: `npm run test:unit`
**Step 3: Write minimal implementation** **Step 3: Write minimal implementation**
@@ -53,12 +52,12 @@ Run: `npm run test:unit -- src/views/Register.test.ts`
**Step 4: Run test to verify it passes** **Step 4: Run test to verify it passes**
Run: `npm run test:unit -- src/views/Register.test.ts` Run: `npm run test:unit`
### Task 3: Final verification ### Task 3: Final verification
Run: Run:
- `npm run test:unit -- src/views/Login.test.ts src/views/Register.test.ts` - `npm run test:unit -- src/views/Login.test.ts`
- `npm run type-check` - `npm run type-check`
不执行 git commit,遵循用户指令。 不执行 git commit,遵循用户指令。
+2 -3
View File
@@ -255,7 +255,6 @@ Expected: pass.
- Replace: `frontend/src/views/ia/EtmfPlaceholder.vue` - Replace: `frontend/src/views/ia/EtmfPlaceholder.vue`
- Modify: `frontend/src/locales/zh-CN.ts` - Modify: `frontend/src/locales/zh-CN.ts`
- Test: `frontend/src/views/ia/EtmfPlaceholder.test.ts`
**Step 1: Write failing page test** **Step 1: Write failing page test**
@@ -297,7 +296,7 @@ Run:
```bash ```bash
cd frontend cd frontend
npm test -- src/views/ia/EtmfPlaceholder.test.ts npm test -- src/api/etmf.test.ts
``` ```
Expected: pass. Expected: pass.
@@ -354,7 +353,7 @@ Expected: pass.
```bash ```bash
cd frontend cd frontend
npm test -- src/api/etmf.test.ts src/views/ia/EtmfPlaceholder.test.ts src/utils/projectRoutePermissions.test.ts npm test -- src/api/etmf.test.ts src/utils/projectRoutePermissions.test.ts
``` ```
Expected: pass. Expected: pass.
@@ -16,7 +16,6 @@
- Modify: `frontend/src/components/QuickActions.vue` - Modify: `frontend/src/components/QuickActions.vue`
- Modify: `frontend/src/views/ia/SubjectManagement.vue` - Modify: `frontend/src/views/ia/SubjectManagement.vue`
- Modify: `frontend/src/utils/permission.ts` - Modify: `frontend/src/utils/permission.ts`
- Test: `frontend/src/components/QuickActions.test.ts`
- Test: `frontend/src/views/ia/SubjectManagement.test.ts` - Test: `frontend/src/views/ia/SubjectManagement.test.ts`
- Test: `frontend/src/utils/permission.test.ts` - Test: `frontend/src/utils/permission.test.ts`
@@ -13,9 +13,7 @@
### Task 1: Route And Navigation Tests ### Task 1: Route And Navigation Tests
**Files:** **Files:**
- Modify: `frontend/src/views/detailNavigation.test.ts` - Modify: `frontend/src/router.test.ts`
- Modify: `frontend/src/views/detailBreadcrumbContext.test.ts`
- Modify: `frontend/src/views/ia/MaterialEquipment.test.ts`
**Steps:** **Steps:**
1. Add the future equipment detail view to detail-navigation expectations. 1. Add the future equipment detail view to detail-navigation expectations.
@@ -470,13 +470,7 @@ curl -H "Authorization: Bearer $TOKEN" \
**测试**: **测试**:
- `tests/test_api_permissions.py` - 权限检查测试 - `tests/test_api_permissions.py` - 权限检查测试
- `tests/test_api_permissions_endpoints.py` - 权限管理 API 测试 - `tests/test_api_permissions_endpoints.py` - 权限管理 API 测试
- `tests/test_api_permissions_config.py` - 权限配置测试
- `tests/test_migrated_endpoints.py` - 已迁移端点测试(第1批) - `tests/test_migrated_endpoints.py` - 已迁移端点测试(第1批)
- `tests/test_migrated_endpoints_batch2.py` - 已迁移端点测试(第2批)
- `tests/test_migrated_endpoints_batch3.py` - 已迁移端点测试(第3批)
- `tests/test_permission_performance.py` - 性能测试
- `tests/test_permission_security.py` - 安全测试
- `tests/test_permission_cache.py` - 缓存测试
- `tests/test_permission_monitoring.py` - 监控测试 - `tests/test_permission_monitoring.py` - 监控测试
- `tests/test_permission_monitoring_api.py` - 监控 API 测试 - `tests/test_permission_monitoring_api.py` - 监控 API 测试
+11 -1
View File
@@ -2,8 +2,18 @@ FROM node:20-alpine AS build
WORKDIR /app WORKDIR /app
ARG NPM_CONFIG_REGISTRY=https://registry.npmjs.org/
ENV NPM_CONFIG_REGISTRY=$NPM_CONFIG_REGISTRY
COPY package*.json ./ COPY package*.json ./
RUN npm ci RUN --mount=type=cache,target=/root/.npm \
npm ci \
--prefer-offline \
--no-audit \
--fetch-retries=5 \
--fetch-retry-mintimeout=20000 \
--fetch-retry-maxtimeout=120000 \
--replace-registry-host=npmjs
COPY . . COPY . .
RUN npm run build RUN npm run build
+3 -10
View File
@@ -1,18 +1,16 @@
<template> <template>
<div class="app-shell" :class="{ 'app-locked': session.locked }"> <div class="app-shell">
<router-view /> <router-view />
<LockScreenModal v-if="session.locked" /> <SessionTimeoutPrompt />
</div> </div>
<!-- TODO: 预留聚合接口页面/study/{id}/overview /subjects/{id}/detail /sites/{id}/summary --> <!-- TODO: 预留聚合接口页面/study/{id}/overview /subjects/{id}/detail /sites/{id}/summary -->
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { useSessionStore } from "./store/session";
import { initSessionManager } from "./session/sessionManager"; import { initSessionManager } from "./session/sessionManager";
import LockScreenModal from "./components/LockScreenModal.vue"; import SessionTimeoutPrompt from "./components/SessionTimeoutPrompt.vue";
initSessionManager(); initSessionManager();
const session = useSessionStore();
</script> </script>
<style scoped> <style scoped>
@@ -20,9 +18,4 @@ const session = useSessionStore();
min-height: 100vh; min-height: 100vh;
min-height: 100dvh; min-height: 100dvh;
} }
.app-locked {
pointer-events: none;
user-select: none;
}
</style> </style>
-32
View File
@@ -1,32 +0,0 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const apiGet = vi.fn();
const apiPost = vi.fn();
const apiDelete = vi.fn();
vi.mock("./axios", () => ({
apiGet,
apiPost,
apiDelete,
}));
describe("attachments api", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("uses canonical collection URLs with trailing slash", async () => {
const { fetchAttachments, uploadAttachment } = await import("./attachments");
const file = new File(["x"], "x.txt", { type: "text/plain" });
fetchAttachments("study-1", "startup_feasibility", "entity-1");
uploadAttachment("study-1", "startup_feasibility", "entity-1", file);
expect(apiGet).toHaveBeenCalledWith("/api/v1/studies/study-1/startup_feasibility/entity-1/attachments/");
expect(apiPost).toHaveBeenCalledWith(
"/api/v1/studies/study-1/startup_feasibility/entity-1/attachments/",
expect.any(FormData),
expect.objectContaining({ headers: { "Content-Type": "multipart/form-data" } })
);
});
});
+1 -4
View File
@@ -1,11 +1,8 @@
import { apiDelete, apiGet, apiPost } from "./axios"; import { apiGet, apiPost } from "./axios";
import type { ApiListResponse } from "../types/api"; import type { ApiListResponse } from "../types/api";
export const fetchAuditLogs = (studyId: string, params?: Record<string, any>) => export const fetchAuditLogs = (studyId: string, params?: Record<string, any>) =>
apiGet<ApiListResponse<any>>(`/api/v1/studies/${studyId}/audit-logs/`, { params }); apiGet<ApiListResponse<any>>(`/api/v1/studies/${studyId}/audit-logs/`, { params });
export const deleteAuditLog = (studyId: string, logId: string) =>
apiDelete(`/api/v1/studies/${studyId}/audit-logs/${logId}`);
export const createAuditEvent = (studyId: string, payload: Record<string, any>) => export const createAuditEvent = (studyId: string, payload: Record<string, any>) =>
apiPost(`/api/v1/studies/${studyId}/audit-logs/events`, payload); apiPost(`/api/v1/studies/${studyId}/audit-logs/events`, payload);
-8
View File
@@ -11,11 +11,6 @@ export type ExtendResponse = {
expiresAt: string; expiresAt: string;
}; };
export type UnlockResponse = {
accessToken: string;
expiresAt: string;
};
export type LoginKeyResponse = { export type LoginKeyResponse = {
key_id: string; key_id: string;
public_key: string; public_key: string;
@@ -43,7 +38,4 @@ export const extendToken = (token: string): Promise<AxiosResponse<ExtendResponse
export const getLoginKey = (): Promise<AxiosResponse<LoginKeyResponse>> => export const getLoginKey = (): Promise<AxiosResponse<LoginKeyResponse>> =>
authClient.get<LoginKeyResponse>("/api/v1/auth/login-key"); authClient.get<LoginKeyResponse>("/api/v1/auth/login-key");
export const unlockSession = (payload: EncryptedPasswordRequest): Promise<AxiosResponse<UnlockResponse>> =>
authClient.post<UnlockResponse>("/api/v1/auth/unlock", payload);
export default authClient; export default authClient;
+70
View File
@@ -0,0 +1,70 @@
import type { AxiosAdapter, AxiosError, InternalAxiosRequestConfig } from "axios";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const errorMessage = vi.fn();
vi.mock("element-plus", () => ({
ElMessage: {
error: errorMessage,
},
}));
vi.mock("../router", () => ({
default: {
push: vi.fn(),
},
}));
vi.mock("../utils/auth", () => ({
getToken: vi.fn(() => undefined),
}));
vi.mock("../store/study", () => ({
useStudyStore: vi.fn(() => ({
clearCurrentStudy: vi.fn(),
})),
}));
vi.mock("../session/sessionManager", () => ({
extendAccessToken: vi.fn(),
forceLogout: vi.fn(),
LOGOUT_REASON_AUTH_EXPIRED: "auth-expired",
}));
describe("api axios retry", () => {
beforeEach(() => {
vi.useFakeTimers();
errorMessage.mockClear();
});
afterEach(() => {
vi.useRealTimers();
});
it("retries network failures 10 times at 30 second intervals before showing one error", async () => {
const api = (await import("./axios")).default;
const adapter = vi.fn<AxiosAdapter>(async (config) => {
const error = new Error("Network Error") as AxiosError;
error.config = config as InternalAxiosRequestConfig;
error.isAxiosError = true;
return Promise.reject(error);
});
const request = api.get("/api/v1/projects", { adapter }).catch((error) => error);
await vi.dynamicImportSettled();
expect(adapter).toHaveBeenCalledTimes(1);
expect(errorMessage).not.toHaveBeenCalled();
for (let retry = 1; retry < 10; retry += 1) {
await vi.advanceTimersByTimeAsync(30000);
expect(adapter).toHaveBeenCalledTimes(retry + 1);
expect(errorMessage).not.toHaveBeenCalled();
}
await vi.advanceTimersByTimeAsync(30000);
expect(adapter).toHaveBeenCalledTimes(11);
await expect(request).resolves.toMatchObject({ message: "Network Error" });
expect(errorMessage).toHaveBeenCalledTimes(1);
});
});
+36 -42
View File
@@ -1,89 +1,83 @@
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from "axios"; import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from "axios";
import { ElMessage } from "element-plus"; import { ElMessage } from "element-plus";
import router from "../router";
import { getToken } from "../utils/auth"; import { getToken } from "../utils/auth";
import type { ApiError } from "../types/api"; import type { ApiError } from "../types/api";
import { useStudyStore } from "../store/study";
import { TEXT } from "../locales"; import { TEXT } from "../locales";
import { extendAccessToken, forceLogout, lockSession, markNetworkActive } from "../session/sessionManager";
import { useSessionStore } from "../store/session";
const instance: AxiosInstance = axios.create({ const instance: AxiosInstance = axios.create({
baseURL: "/", baseURL: "/",
timeout: 15000, timeout: 15000,
}); });
const NETWORK_RETRY_LIMIT = 10;
const NETWORK_RETRY_DELAY_MS = 30000;
export type ApiRequestConfig = AxiosRequestConfig & { export type ApiRequestConfig = AxiosRequestConfig & {
ignoreIdle?: boolean;
allowWhileLocked?: boolean;
suppressErrorMessage?: boolean; suppressErrorMessage?: boolean;
_retry?: boolean; _retry?: boolean;
_networkRetryCount?: number;
}; };
class LockedError extends Error { const wait = (ms: number) => new Promise((resolve) => window.setTimeout(resolve, ms));
constructor() {
super("LOCKED"); const clearInvalidStudyAndNavigate = async () => {
this.name = "LockedError"; try {
const [{ useStudyStore }, { default: router }] = await Promise.all([
import("../store/study"),
import("../router"),
]);
useStudyStore().clearCurrentStudy();
router.push("/admin/projects");
} catch {
/* ignore */
} }
} };
const forceAuthExpiredLogout = async () => {
const { forceLogout, LOGOUT_REASON_AUTH_EXPIRED } = await import("../session/sessionManager");
forceLogout(LOGOUT_REASON_AUTH_EXPIRED);
};
instance.interceptors.request.use((config: InternalAxiosRequestConfig & ApiRequestConfig) => { instance.interceptors.request.use((config: InternalAxiosRequestConfig & ApiRequestConfig) => {
const session = useSessionStore();
const reqUrl = config.url || "";
const allowWhileLocked =
config.allowWhileLocked ||
reqUrl.includes("/api/v1/auth/login") ||
reqUrl.includes("/api/v1/auth/register") ||
reqUrl.includes("/api/v1/auth/unlock");
if (session.locked && !allowWhileLocked) {
return Promise.reject(new LockedError());
}
const token = getToken(); const token = getToken();
if (token) { if (token) {
config.headers = config.headers || {}; config.headers = config.headers || {};
(config.headers as Record<string, string>).Authorization = `Bearer ${token}`; (config.headers as Record<string, string>).Authorization = `Bearer ${token}`;
} }
if (!config.ignoreIdle) {
markNetworkActive();
}
return config; return config;
}); });
instance.interceptors.response.use( instance.interceptors.response.use(
(response: AxiosResponse) => response, (response: AxiosResponse) => response,
async (error: AxiosError<ApiError>) => { async (error: AxiosError<ApiError>) => {
if (error instanceof LockedError) {
const lockedConfig = error.config as ApiRequestConfig | undefined;
if (!lockedConfig?.suppressErrorMessage) {
ElMessage.warning(TEXT.common.messages.sessionLocked || "请解锁后继续操作");
}
return Promise.reject(error);
}
const status = error.response?.status; const status = error.response?.status;
const data = error.response?.data; const data = error.response?.data;
const reqUrl = error.config?.url || ""; const reqUrl = error.config?.url || "";
const isAuthEndpoint = const isAuthEndpoint =
reqUrl.includes("/api/v1/auth/login") || reqUrl.includes("/api/v1/auth/login") ||
reqUrl.includes("/api/v1/auth/register") || reqUrl.includes("/api/v1/auth/register") ||
reqUrl.includes("/api/v1/auth/extend") || reqUrl.includes("/api/v1/auth/extend");
reqUrl.includes("/api/v1/auth/unlock");
if (isAuthEndpoint) { if (isAuthEndpoint) {
// 认证相关的错误由具体页面自行处理,避免重复提示 // 认证相关的错误由具体页面自行处理,避免重复提示
return Promise.reject(error); return Promise.reject(error);
} }
if (!status && error.config) {
const config = error.config as ApiRequestConfig;
const retryCount = config._networkRetryCount || 0;
if (retryCount < NETWORK_RETRY_LIMIT) {
config._networkRetryCount = retryCount + 1;
await wait(NETWORK_RETRY_DELAY_MS);
return instance(config);
}
}
// 如果当前项目不存在或已失效,清理项目并跳到项目管理页 // 如果当前项目不存在或已失效,清理项目并跳到项目管理页
if (status === 404 && typeof data?.message === "string" && data.message.toLowerCase().includes("study")) { if (status === 404 && typeof data?.message === "string" && data.message.toLowerCase().includes("study")) {
try { await clearInvalidStudyAndNavigate();
const studyStore = useStudyStore();
studyStore.clearCurrentStudy();
} catch {
/* ignore */
}
router.push("/admin/projects");
} }
if (status === 401) { if (status === 401) {
const config = error.config as ApiRequestConfig; const config = error.config as ApiRequestConfig;
if (config && !config._retry) { if (config && !config._retry) {
const { extendAccessToken } = await import("../session/sessionManager");
const result = await extendAccessToken("response-401"); const result = await extendAccessToken("response-401");
if (result.token) { if (result.token) {
config._retry = true; config._retry = true;
@@ -92,11 +86,11 @@ instance.interceptors.response.use(
return instance(config); return instance(config);
} }
if (result.authFailed) { if (result.authFailed) {
lockSession("token_invalid"); await forceAuthExpiredLogout();
} }
} }
} else if (status === 403 && (data as any)?.detail === "账号已停用") { } else if (status === 403 && (data as any)?.detail === "账号已停用") {
forceLogout(); await forceAuthExpiredLogout();
} else { } else {
const suppressErrorMessage = (error.config as ApiRequestConfig | undefined)?.suppressErrorMessage; const suppressErrorMessage = (error.config as ApiRequestConfig | undefined)?.suppressErrorMessage;
if (!suppressErrorMessage) { if (!suppressErrorMessage) {
-89
View File
@@ -1,89 +0,0 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const apiPost = vi.fn();
const apiPatch = vi.fn();
const apiDelete = vi.fn();
const apiGet = vi.fn();
vi.mock("./axios", () => ({
apiGet,
apiPost,
apiPatch,
apiDelete,
}));
describe("faqs category api", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("passes study_id as a query parameter for category write permission checks", async () => {
const { createFaqCategory, updateFaqCategory, deleteFaqCategory } = await import("./faqs");
const payload = { study_id: "study-1", name: "用药咨询" };
createFaqCategory(payload);
updateFaqCategory("category-1", payload);
deleteFaqCategory("category-1", "study-1");
expect(apiPost).toHaveBeenCalledWith("/api/v1/faqs/categories/", payload, { params: { study_id: "study-1" } });
expect(apiPatch).toHaveBeenCalledWith("/api/v1/faqs/categories/category-1", payload, {
params: { study_id: "study-1" },
});
expect(apiDelete).toHaveBeenCalledWith("/api/v1/faqs/categories/category-1", {
params: { study_id: "study-1" },
});
});
});
describe("faqs item api", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("passes study_id as a query parameter for item write permission checks", async () => {
const { createFaqItem, updateFaqItem, deleteFaqItem } = await import("./faqs");
const payload = { study_id: "study-1", category_id: "category-1", question: "是否需要空腹用药?" };
createFaqItem(payload);
updateFaqItem("item-1", payload);
deleteFaqItem("item-1", "study-1");
expect(apiPost).toHaveBeenCalledWith("/api/v1/faqs/items/", payload, { params: { study_id: "study-1" } });
expect(apiPatch).toHaveBeenCalledWith("/api/v1/faqs/items/item-1", payload, {
params: { study_id: "study-1" },
});
expect(apiDelete).toHaveBeenCalledWith("/api/v1/faqs/items/item-1", {
params: { study_id: "study-1" },
});
});
it("passes study_id as a query parameter for item detail permission checks", async () => {
const { fetchFaqItem, fetchFaqReplies, createFaqReply, deleteFaqReply, setFaqBestReply, setFaqStatus } = await import("./faqs");
fetchFaqItem("item-1", "study-1");
fetchFaqReplies("item-1", "study-1");
createFaqReply("item-1", "study-1", { content: "建议复查。" });
deleteFaqReply("item-1", "reply-1", "study-1");
setFaqBestReply("item-1", "study-1", { best_reply_id: "reply-1" });
setFaqStatus("item-1", "study-1", { status: "RESOLVED" });
expect(apiGet).toHaveBeenCalledWith("/api/v1/faqs/items/item-1", {
params: { study_id: "study-1" },
});
expect(apiGet).toHaveBeenCalledWith("/api/v1/faqs/items/item-1/replies", {
params: { study_id: "study-1" },
});
expect(apiPost).toHaveBeenCalledWith("/api/v1/faqs/items/item-1/replies", { content: "建议复查。" }, {
params: { study_id: "study-1" },
});
expect(apiDelete).toHaveBeenCalledWith("/api/v1/faqs/items/item-1/replies/reply-1", {
params: { study_id: "study-1" },
});
expect(apiPatch).toHaveBeenCalledWith("/api/v1/faqs/items/item-1/best-reply", { best_reply_id: "reply-1" }, {
params: { study_id: "study-1" },
});
expect(apiPatch).toHaveBeenCalledWith("/api/v1/faqs/items/item-1/status", { status: "RESOLVED" }, {
params: { study_id: "study-1" },
});
});
});
-23
View File
@@ -1,23 +0,0 @@
import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
const readSource = () => readFileSync(resolve(__dirname, "./feeContracts.ts"), "utf8");
describe("feeContracts API naming", () => {
it("uses study_id for contract fee project identity", () => {
const source = readSource();
expect(source).toContain("study_id: string");
expect(source).toContain("params: { study_id: string; center_id?: string; q?: string }");
expect(source).not.toContain("project_id: string");
expect(source).not.toContain("projectId");
expect(source).not.toContain("centerId");
});
it("does not expose currency in contract fee payloads because amounts are fixed to ten-thousand yuan", () => {
const source = readSource();
expect(source).not.toContain("currency?: string");
});
});
+2 -2
View File
@@ -34,7 +34,7 @@ export const updateApiEndpointPermissions = (
export const fetchApiOperations = () => export const fetchApiOperations = () =>
apiGet<{ operations: ApiOperation[] }>(`/api/v1/api-permissions/operations`); apiGet<{ operations: ApiOperation[] }>(`/api/v1/api-permissions/operations`);
// 权限系统监API // 系统监API
export const fetchPermissionMetrics = () => export const fetchPermissionMetrics = () =>
apiGet<PermissionMetricsResponse>(`/api/v1/permission-monitoring/metrics`); apiGet<PermissionMetricsResponse>(`/api/v1/permission-monitoring/metrics`);
@@ -55,7 +55,7 @@ export const resetPermissionMetrics = () =>
export const clearPermissionAlerts = () => export const clearPermissionAlerts = () =>
apiPost<void>(`/api/v1/permission-monitoring/clear-alerts`); apiPost<void>(`/api/v1/permission-monitoring/clear-alerts`);
// 权限监控 - 新增API // 系统监测 - 新增API
export const fetchAccessLogs = (params: { export const fetchAccessLogs = (params: {
study_id?: string; study_id?: string;
user_id?: string; user_id?: string;
File diff suppressed because one or more lines are too long
+455 -2
View File
@@ -16,10 +16,34 @@ describe("normalizeAuditEvent", () => {
{} {}
); );
expect(event.diffText).toEqual(["FAQ 分类已删除"]); expect(event.diffText).toEqual(["删除分类(历史日志未记录分类名称)"]);
expect(event.targetName).toBe(""); expect(event.targetName).toBe("");
}); });
it("renders legacy FAQ category actions as business descriptions", () => {
const event = normalizeAuditEvent(
{
action: "CREATE_FAQ_CATEGORY",
detail: JSON.stringify({
targetName: "入排标准",
description: "FAQ 分类 入排标准 已创建",
}),
operator_id: "operator-1",
operator_role: "ADMIN",
entity_type: "faq_category",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-06-04T11:16:04Z",
},
{}
);
expect(event.eventLabel).toBe("新增医学咨询分类");
expect(event.targetTypeLabel).toBe("医学咨询分类");
expect(event.targetName).toBe("入排标准");
expect(event.diffText).toEqual(["创建“入排标准”分类"]);
expect(event.actionText).toBe("创建“入排标准”分类");
});
it("keeps readable shipment identifiers but hides UUID-only shipment identifiers", () => { it("keeps readable shipment identifiers but hides UUID-only shipment identifiers", () => {
const event = normalizeAuditEvent( const event = normalizeAuditEvent(
{ {
@@ -34,7 +58,436 @@ describe("normalizeAuditEvent", () => {
{} {}
); );
expect(event.diffText).toEqual(["药品运输已创建"]); expect(event.diffText).toEqual(["创建药品流向(历史日志未记录对象信息)"]);
expect(event.targetName).toBe(""); expect(event.targetName).toBe("");
}); });
it("moves legacy subject numbers into the target column", () => {
const event = normalizeAuditEvent(
{
action: "CREATE_SUBJECT",
detail: "参与者 S001 已创建",
operator_id: "operator-1",
operator_role: "ADMIN",
entity_type: "subject",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-05-08T15:44:48Z",
},
{}
);
expect(event.eventLabel).toBe("新增参与者");
expect(event.targetTypeLabel).toBe("参与者");
expect(event.targetName).toBe("S001");
expect(event.diffText).toEqual(["创建参与者 S001"]);
expect(event.actionText).toBe("创建参与者 S001");
});
it("maps legacy project member target UUIDs to user display names", () => {
const userId = "169ede81-f2cb-4b44-84c5-c11111111111";
const event = normalizeAuditEvent(
{
action: "PROJECT_MEMBER_UPDATED",
detail: JSON.stringify({
targetName: userId,
before: { role_in_study: "CRA" },
after: { role_in_study: "PM" },
}),
operator_id: "operator-1",
operator_role: "ADMIN",
entity_type: "study_member",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-06-04T10:27:02Z",
},
{ [userId]: "张三" }
);
expect(event.targetName).toBe("张三");
expect(event.targetName).not.toContain(userId);
});
it("maps user UUIDs inside audit detail lines to display names", () => {
const userId = "94dccdc4-5592-4e59-b743-c33333333333";
const event = normalizeAuditEvent(
{
action: "SITE_UPDATED",
detail: JSON.stringify({
targetName: "重庆市璧山区人民医院",
before: { contact: userId },
after: { contact: "汤成泳" },
}),
operator_id: "operator-1",
operator_role: "ADMIN",
entity_type: "site",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-06-04T10:27:02Z",
},
{ [userId]: "李四" }
);
expect(event.diffText).toEqual(["负责人:李四 -> 汤成泳"]);
});
it("does not expose unmapped UUID-only target names", () => {
const event = normalizeAuditEvent(
{
action: "PROJECT_MEMBER_ADDED",
detail: JSON.stringify({
targetName: "307e9a37-530c-4894-8d19-f22222222222",
after: { role_in_study: "PM" },
}),
operator_id: "operator-1",
operator_role: "ADMIN",
entity_type: "study_member",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-06-04T10:27:02Z",
},
{}
);
expect(event.targetName).toBe("");
});
it("translates unknown CRUD action names into readable Chinese labels", () => {
const event = normalizeAuditEvent(
{
action: "CREATE_FAQ_ITEM",
detail: JSON.stringify({ targetName: "药物保存咨询" }),
operator_id: "operator-1",
operator_role: "ADMIN",
entity_type: "faq_item",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-06-04T10:27:02Z",
},
{}
);
expect(event.eventLabel).toBe("新增医学咨询问题");
expect(event.actionText).toBe("新增了医学咨询问题");
expect(event.eventLabel).not.toContain("CREATE_FAQ_ITEM");
expect(event.actionText).not.toContain("CREATE_FAQ_ITEM");
});
it("uses FAQ question titles in generic structured FAQ descriptions", () => {
const event = normalizeAuditEvent(
{
action: "CREATE_FAQ_ITEM",
detail: JSON.stringify({
targetName: "试验期间是否允许接种疫苗?",
description: "创建医学咨询问题",
}),
operator_id: "operator-1",
operator_role: "ADMIN",
entity_type: "faq_item",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-06-09T08:55:44Z",
},
{}
);
expect(event.targetName).toBe("试验期间是否允许接种疫苗?");
expect(event.diffText).toEqual(["创建医学咨询问题“试验期间是否允许接种疫苗?”"]);
expect(event.actionText).toBe("创建医学咨询问题“试验期间是否允许接种疫苗?”");
});
it("keeps action text focused on the business operation when target names exist", () => {
const event = normalizeAuditEvent(
{
action: "CREATE_FAQ_ITEM",
detail: JSON.stringify({ targetName: "药物保存咨询" }),
operator_id: "operator-1",
operator_role: "ADMIN",
entity_type: "faq_item",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-06-04T10:27:02Z",
},
{}
);
expect(event.eventLabel).toBe("新增医学咨询问题");
expect(event.actionText).toBe("新增了医学咨询问题");
expect(event.targetName).toBe("药物保存咨询");
});
it("uses changed fields as action text when the target name duplicates the operation", () => {
const event = normalizeAuditEvent(
{
action: "UPDATE_FAQ_CATEGORY",
detail: JSON.stringify({
targetName: "医学咨询分类",
before: { name: "旧分类" },
after: { name: "新分类" },
}),
operator_id: "operator-1",
operator_role: "ADMIN",
entity_type: "faq_category",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-06-04T10:27:02Z",
},
{}
);
expect(event.eventLabel).toBe("更新医学咨询分类");
expect(event.actionText).toBe("名称:旧分类 -> 新分类");
});
it("keeps setup module summary out of change detail lines", () => {
const event = normalizeAuditEvent(
{
action: "SAVE_STUDY_SETUP_CONFIG",
detail: JSON.stringify({
description:
"变更模块:项目里程碑; 变更明细:项目里程碑 / 参研中心调研+方案讨论会 / 耗时(天):30 -> 28;项目里程碑 / 参研中心调研+方案讨论会 / 计划日期:2026-06-01 -> 2026-06-03;项目里程碑 / 参研中心调研+方案讨论会 / 开始日期:2026-06-01 -> 2026-06-03",
}),
operator_id: "operator-1",
operator_role: "ADMIN",
entity_type: "study_setup_config",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-05-28T09:15:23Z",
},
{}
);
expect(event.actionText).toBe("变更模块:项目里程碑");
expect(event.diffText).toEqual([
"项目里程碑 / 参研中心调研+方案讨论会 / 耗时(天):30 -> 28",
"项目里程碑 / 参研中心调研+方案讨论会 / 计划日期:2026-06-01 -> 2026-06-03",
"项目里程碑 / 参研中心调研+方案讨论会 / 开始日期:2026-06-01 -> 2026-06-03",
]);
});
it("hides legacy setup technical ID lines and omission placeholders", () => {
const event = normalizeAuditEvent(
{
action: "UPDATE_SETUP_CONFIG",
detail:
"变更模块:项目里程碑; 变更明细:项目里程碑 / 参研中心调研+方案讨论会 / ID:未填写 -> 1778477970865_08roj;项目里程碑 / 参研中心调研+方案讨论会 / 名称:未填写 -> 参研中心调研+方案讨论会;其余 115 项变更已省略",
operator_id: "operator-1",
operator_role: "ADMIN",
entity_type: "study_setup_config",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-05-28T09:15:23Z",
},
{}
);
expect(event.diffText).toEqual([
"项目里程碑 / 参研中心调研+方案讨论会 / 名称:未填写 -> 参研中心调研+方案讨论会",
]);
expect((event.diffText || []).join(" ")).not.toContain("ID");
expect((event.diffText || []).join(" ")).not.toContain("已省略");
});
it("renders document version audit details in business language", () => {
const event = normalizeAuditEvent(
{
action: "VERSION_CREATED",
detail: JSON.stringify({
before: null,
after: {
id: "307e9a37-530c-4894-8d19-f22222222222",
document_id: "169ede81-f2cb-4b44-84c5-c11111111111",
version_no: "1.0",
status: "EFFECTIVE",
file_hash: "95ac0061c81793d533906155921871182076c2dd5bbdd1be0b0e09ebdff17894",
},
}),
operator_id: "operator-1",
operator_role: "PM",
entity_type: "DOCUMENT_VERSION",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-05-26T09:42:21Z",
},
{}
);
expect(event.eventLabel).toBe("新增文档版本");
expect(event.targetTypeLabel).toBe("文档版本");
expect(event.actionText).toBe("记录标识:未填写 -> 已生成");
expect(event.diffText).toEqual([
"记录标识:未填写 -> 已生成",
"所属文档:未填写 -> 已关联文档",
"版本号:未填写 -> V1.0",
"版本状态:未填写 -> 已生效",
"文件校验值:未填写 -> 已记录校验值",
]);
const detailText = (event.diffText || []).join(" ");
expect(detailText).not.toContain("字段(id");
expect(detailText).not.toContain("未识别对象");
expect(detailText).not.toContain("DocumentVersionStatus");
expect(detailText).not.toContain("95ac0061");
});
it("renders enum class-prefixed document version statuses in business language", () => {
const event = normalizeAuditEvent(
{
action: "VERSION_CREATED",
detail: JSON.stringify({
before: null,
after: {
status: "DocumentVersionStatus.EFFECTIVE",
},
}),
operator_id: "operator-1",
operator_role: "PM",
entity_type: "DOCUMENT_VERSION",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-05-26T09:42:21Z",
},
{}
);
expect(event.diffText).toEqual(["版本状态:未填写 -> 已生效"]);
expect((event.diffText || []).join(" ")).not.toContain("DocumentVersionStatus");
});
it("renders structured subject delete audits with subject business context", () => {
const event = normalizeAuditEvent(
{
action: "DELETE_SUBJECT",
detail: JSON.stringify({
targetName: "SUBJ-001",
description: "删除参与者 SUBJ-001",
before: {
subject_no: "SUBJ-001",
site_name: "北京协和医院",
status: "SCREENING",
consent_date: "2026-05-01",
baseline_date: null,
},
after: null,
}),
operator_id: "operator-1",
operator_role: "PM",
entity_type: "subject",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-05-26T09:42:21Z",
},
{}
);
expect(event.eventLabel).toBe("删除参与者");
expect(event.targetName).toBe("SUBJ-001");
expect(event.actionText).toBe("删除参与者 SUBJ-001");
expect(event.diffText).toContain("参与者编号:SUBJ-001 -> 未填写");
expect(event.diffText).toContain("中心名称:北京协和医院 -> 未填写");
expect(event.diffText).toContain("知情同意日期:2026-05-01 -> 未填写");
expect((event.diffText || []).join(" ")).not.toContain("字段(subject_no");
});
it("explains legacy subject delete logs that only stored UUIDs", () => {
const event = normalizeAuditEvent(
{
action: "DELETE_SUBJECT",
detail: "参与者 307e9a37-530c-4894-8d19-f22222222222 已删除",
operator_id: "operator-1",
operator_role: "PM",
entity_type: "subject",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-05-26T09:42:21Z",
},
{}
);
expect(event.diffText).toEqual(["删除参与者(历史日志未记录参与者编号)"]);
expect(event.actionText).toBe("删除参与者(历史日志未记录参与者编号)");
});
it("renders legacy drug and equipment audit details with business context", () => {
const shipmentEvent = normalizeAuditEvent(
{
action: "CREATE_DRUG_SHIPMENT",
detail: JSON.stringify({
targetName: "TRACK-001",
description: "药品运输 TRACK-001 已创建",
}),
operator_id: "operator-1",
operator_role: "PM",
entity_type: "drug_shipment",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-05-26T09:37:58Z",
},
{}
);
const equipmentEvent = normalizeAuditEvent(
{
action: "CREATE_MATERIAL_EQUIPMENT",
detail: "设备 307e9a37-530c-4894-8d19-f22222222222 已创建",
operator_id: "operator-1",
operator_role: "PM",
entity_type: "material_equipment",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-05-26T09:36:19Z",
},
{}
);
expect(shipmentEvent.targetName).toBe("TRACK-001");
expect(shipmentEvent.actionText).toBe("创建药品流向“TRACK-001”");
expect(equipmentEvent.actionText).toBe("创建设备(历史日志未记录设备名称)");
expect(equipmentEvent.targetName).toBe("");
});
it("marks generic legacy fee and startup audit details as missing object context", () => {
const feeEvent = normalizeAuditEvent(
{
action: "CREATE_CONTRACT_FEE",
detail: "合同费用已创建",
operator_id: "operator-1",
operator_role: "PM",
entity_type: "contract_fee",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-05-26T09:36:19Z",
},
{}
);
const startupEvent = normalizeAuditEvent(
{
action: "UPDATE_STARTUP_FEASIBILITY",
detail: "立项记录已更新",
operator_id: "operator-1",
operator_role: "PM",
entity_type: "startup_feasibility",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-05-26T09:33:41Z",
},
{}
);
expect(feeEvent.actionText).toBe("创建合同费用(历史日志未记录对象信息)");
expect(startupEvent.actionText).toBe("更新立项记录(历史日志未记录对象信息)");
});
it("renders high-value non-CRUD setup and lock actions in business language", () => {
const lockEvent = normalizeAuditEvent(
{
action: "LOCK_STUDY",
detail: "项目已锁定:真实世界研究 (RWS-001)",
operator_id: "operator-1",
operator_role: "ADMIN",
entity_type: "study",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-06-04T10:27:02Z",
},
{}
);
const rollbackEvent = normalizeAuditEvent(
{
action: "ROLLBACK_SETUP_CONFIG",
detail: "立项配置已回滚并替换当前发布为 v3,草稿分支已切换到对应分支基线",
operator_id: "operator-1",
operator_role: "PM",
entity_type: "study_setup_config",
entity_id: "307e9a37-530c-4894-8d19-f22222222222",
created_at: "2026-06-04T10:27:02Z",
},
{}
);
expect(lockEvent.eventLabel).toBe("锁定项目");
expect(lockEvent.actionText).toBe("锁定了项目");
expect(rollbackEvent.eventLabel).toBe("回滚立项配置");
expect(rollbackEvent.actionText).toBe("回滚了立项配置");
});
}); });
@@ -19,9 +19,11 @@ export const formatAuditRow = (event: AuditEvent): AuditExportRow => {
const descriptionParts: string[] = []; const descriptionParts: string[] = [];
descriptionParts.push(`${event.actorName} ${event.actionText}`); descriptionParts.push(`${event.actorName} ${event.actionText}`);
if (event.targetTypeLabel || event.targetName || event.targetId) { if (event.targetTypeLabel || event.targetName || event.targetId) {
const targetIdentifier = event.targetName || event.targetId;
descriptionParts.push( descriptionParts.push(
`${TEXT.audit.objectLabel}${event.targetTypeLabel || ""}${ `${TEXT.audit.objectLabel}${event.targetTypeLabel || ""}${
event.targetName ? `${event.targetName}` : `${event.targetId}`})` targetIdentifier ? `${targetIdentifier}` : ""
}`
); );
} }
if (event.diffText?.length) { if (event.diffText?.length) {
+407 -25
View File
@@ -1,4 +1,4 @@
import { auditDict } from "./auditDict"; import { auditDict as localizedAuditDict } from "./auditDict";
import { roleDict, getDictLabel, statusDict } from "../dictionaries"; import { roleDict, getDictLabel, statusDict } from "../dictionaries";
import { TEXT } from "../locales"; import { TEXT } from "../locales";
@@ -45,6 +45,94 @@ const entityTypeLabelMap: Record<string, string> = {
DOCUMENT_VERSION: "文档版本", DOCUMENT_VERSION: "文档版本",
DISTRIBUTION: "文档分发", DISTRIBUTION: "文档分发",
ACKNOWLEDGEMENT: "文档签收", ACKNOWLEDGEMENT: "文档签收",
ETMF_NODE: "eTMF 目录",
};
const auditActionVerbMap: Record<string, { labelPrefix: string; actionPrefix: string }> = {
CREATE: { labelPrefix: "新增", actionPrefix: "新增了" },
UPDATE: { labelPrefix: "更新", actionPrefix: "更新了" },
DELETE: { labelPrefix: "删除", actionPrefix: "删除了" },
};
const auditActionTargetMap: Record<string, string> = {
FAQ_CATEGORY: "医学咨询分类",
FAQ_ITEM: "医学咨询问题",
FAQ_REPLY: "医学咨询回复",
PRECAUTION: "注意事项",
STARTUP_FEASIBILITY: "立项记录",
STARTUP_ETHICS: "伦理记录",
KICKOFF_MEETING: "启动会",
TRAINING_AUTH: "培训授权",
SITE: "中心",
SUBJECT: "参与者",
VISIT: "访视",
AE: "不良事件",
SUBJECT_PD: "PD 记录",
SUBJECT_HISTORY: "病史记录",
DRUG_SHIPMENT: "药品流向",
MATERIAL_EQUIPMENT: "设备",
MONITORING_VISIT_ISSUE: "监查访视问题",
CONTRACT_FEE: "合同费用条目",
CONTRACT_FEE_PAYMENT: "合同费用回款",
ATTACHMENT: "附件",
SETUP_CONFIG_VERSION: "立项配置版本",
PROJECT_MILESTONE: "项目里程碑",
DOCUMENT: "文档",
DOCUMENT_VERSION: "文档版本",
DISTRIBUTION: "文档分发",
ACKNOWLEDGEMENT: "文档签收",
};
const explicitAuditActionMap: Record<string, { label: string; actionText: string; targetLabel: string }> = {
LOCK_STUDY: { label: "锁定项目", actionText: "锁定了项目", targetLabel: "项目" },
UNLOCK_STUDY: { label: "解锁项目", actionText: "解锁了项目", targetLabel: "项目" },
ROLLBACK_SETUP_CONFIG: { label: "回滚立项配置", actionText: "回滚了立项配置", targetLabel: "立项配置" },
CHECKOUT_SETUP_CONFIG_BRANCH_DRAFT: { label: "切换立项配置草稿分支", actionText: "切换了立项配置草稿分支", targetLabel: "立项配置" },
CLEAR_SETUP_CONFIG_DRAFT: { label: "清空立项配置草稿", actionText: "清空了立项配置草稿", targetLabel: "立项配置" },
REFILL_SETUP_CONFIG_DRAFT: { label: "回填立项配置草稿", actionText: "从当前发布版本回填了立项配置草稿", targetLabel: "立项配置" },
MERGE_SETUP_CONFIG_TO_MAIN: { label: "合并立项配置主版本", actionText: "合并了立项配置主版本", targetLabel: "立项配置" },
DELETE_SETUP_CONFIG_VERSION: { label: "删除立项配置版本", actionText: "删除了立项配置版本", targetLabel: "立项配置版本" },
CREATE_EARLY_TERMINATION: { label: "创建提前终止访视", actionText: "创建了提前终止访视", targetLabel: "访视" },
VISIT_STATUS_CHANGE: { label: "访视状态变更", actionText: "变更了访视状态", targetLabel: "访视" },
SUBJECT_STATUS_CHANGE: { label: "参与者状态变更", actionText: "变更了参与者状态", targetLabel: "参与者" },
AE_STATUS_CHANGE: { label: "AE 状态变更", actionText: "变更了 AE 状态", targetLabel: "不良事件" },
SITE_STATUS_CHANGED: { label: "中心状态变更", actionText: "变更了中心状态", targetLabel: "中心" },
UPLOAD_FILE: { label: "上传附件", actionText: "上传了附件", targetLabel: "附件" },
IMPORT_MONITORING_VISIT_ISSUE: { label: "导入监查访视问题", actionText: "导入了监查访视问题", targetLabel: "监查访视问题" },
DOCUMENT_CREATED: { label: "新增文档", actionText: "新增了文档", targetLabel: "文档" },
DOCUMENT_UPDATED: { label: "更新文档", actionText: "更新了文档", targetLabel: "文档" },
DOCUMENT_ARCHIVED: { label: "归档文档", actionText: "归档了文档", targetLabel: "文档" },
VERSION_CREATED: { label: "新增文档版本", actionText: "新增了文档版本", targetLabel: "文档版本" },
VERSION_DELETED: { label: "删除文档版本", actionText: "删除了文档版本", targetLabel: "文档版本" },
DISTRIBUTION_CREATED: { label: "发起文档分发", actionText: "发起了文档分发", targetLabel: "文档分发" },
ACK_CREATED: { label: "提交文档回执", actionText: "提交了文档回执", targetLabel: "文档签收" },
ETMF_NODE_CREATED: { label: "新增 eTMF 目录", actionText: "新增了 eTMF 目录", targetLabel: "eTMF 目录" },
ETMF_NODE_UPDATED: { label: "更新 eTMF 目录", actionText: "更新了 eTMF 目录", targetLabel: "eTMF 目录" },
};
export const auditDict: Record<string, { label: string; actionText: string; targetLabel: string }> = {
...localizedAuditDict,
...explicitAuditActionMap,
};
const buildReadableAuditDict = (action?: string, entityType?: string) => {
const actionKey = String(action || "").trim();
if (explicitAuditActionMap[actionKey]) return explicitAuditActionMap[actionKey];
const match = actionKey.match(/^(CREATE|UPDATE|DELETE)_(.+)$/);
if (match) {
const verb = auditActionVerbMap[match[1]];
const target = auditActionTargetMap[match[2]] || toReadableEntityType(entityType);
return {
label: `${verb.labelPrefix}${target}`,
actionText: `${verb.actionPrefix}${target}`,
targetLabel: target,
};
}
return {
label: actionKey ? TEXT.audit.eventFallback : TEXT.audit.eventFallback,
actionText: TEXT.audit.actionFallback,
targetLabel: toReadableEntityType(entityType),
};
}; };
const setupModuleLabelMap: Record<string, string> = { const setupModuleLabelMap: Record<string, string> = {
@@ -75,6 +163,86 @@ const normalizeSetupModuleText = (raw: string): string => {
const normalizeLegacyDetailLine = (line: string): string => { const normalizeLegacyDetailLine = (line: string): string => {
const text = String(line || "").trim(); const text = String(line || "").trim();
if (!text) return ""; if (!text) return "";
if (/^其余\s+\d+\s+项变更已省略$/.test(text)) return "";
if (/\/\s*ID[:]/.test(text) || /^ID[:]/.test(text)) return "";
const legacyFaqCategoryActionMatch = text.match(/^FAQ\s*分类\s+(.+?)\s+已(创建|更新|删除)$/i);
if (legacyFaqCategoryActionMatch) {
const categoryName = legacyFaqCategoryActionMatch[1].trim();
const verb = legacyFaqCategoryActionMatch[2];
const actionVerb = verb === "创建" ? "创建" : verb === "更新" ? "更新" : "删除";
return isUuidText(categoryName) ? `${actionVerb}分类(历史日志未记录分类名称)` : `${actionVerb}${categoryName}”分类`;
}
if (/^FAQ\s*已创建$/i.test(text)) return "创建医学咨询问题";
if (/^FAQ\s*updated$/i.test(text) || /^FAQ\s*已更新$/i.test(text)) return "更新医学咨询问题";
if (/^FAQ\s*已删除$/i.test(text)) return "删除医学咨询问题";
if (/^FAQ\s*已回复$/i.test(text)) return "回复医学咨询问题";
if (/^FAQ\s*回复已删除$/i.test(text)) return "删除医学咨询回复";
const legacyDrugShipmentMatch = text.match(/^药品运输\s+(.+?)\s+已(创建|更新|删除)$/);
if (legacyDrugShipmentMatch) {
const name = legacyDrugShipmentMatch[1].trim();
const verb = legacyDrugShipmentMatch[2];
return isUuidText(name) ? `${verb}药品流向(历史日志未记录对象信息)` : `${verb}药品流向“${name}`;
}
const legacyEquipmentMatch = text.match(/^设备\s+(.+?)\s+已(创建|更新|删除)$/);
if (legacyEquipmentMatch) {
const name = legacyEquipmentMatch[1].trim();
const verb = legacyEquipmentMatch[2];
return isUuidText(name) ? `${verb}设备(历史日志未记录设备名称)` : `${verb}设备“${name}`;
}
const legacyPrecautionMatch = text.match(/^注意事项\s+(.+?)\s+已(创建|更新|删除)$/);
if (legacyPrecautionMatch) {
const name = legacyPrecautionMatch[1].trim();
const verb = legacyPrecautionMatch[2];
return isUuidText(name) ? `${verb}注意事项(历史日志未记录标题)` : `${verb}注意事项“${name}`;
}
const legacyVisitMatch = text.match(/^访视\s+(.+?)\s+已(创建|更新|删除)$/);
if (legacyVisitMatch) {
const name = legacyVisitMatch[1].trim();
const verb = legacyVisitMatch[2];
return isUuidText(name) ? `${verb}访视(历史日志未记录访视号)` : `${verb}访视“${name}`;
}
const legacyAeMatch = text.match(/^AE\s+(.+?)\s+已(创建|删除)$/i);
if (legacyAeMatch) {
const name = legacyAeMatch[1].trim();
const verb = legacyAeMatch[2];
return isUuidText(name) ? `${verb}AE(历史日志未记录 AE 术语)` : `${verb}AE“${name}`;
}
const genericLegacyActionMap: Record<string, string> = {
: "创建药品流向(历史日志未记录对象信息)",
: "更新药品流向(历史日志未记录对象信息)",
: "删除药品流向(历史日志未记录对象信息)",
: "创建设备(历史日志未记录设备名称)",
: "更新设备(历史日志未记录设备名称)",
: "删除设备(历史日志未记录设备名称)",
: "创建合同费用(历史日志未记录对象信息)",
: "更新合同费用(历史日志未记录对象信息)",
: "删除合同费用(历史日志未记录对象信息)",
: "创建合同费用分期(历史日志未记录对象信息)",
: "更新合同费用分期(历史日志未记录对象信息)",
: "删除合同费用分期(历史日志未记录对象信息)",
: "创建立项记录(历史日志未记录对象信息)",
: "更新立项记录(历史日志未记录对象信息)",
: "删除立项记录(历史日志未记录对象信息)",
: "创建伦理记录(历史日志未记录对象信息)",
: "更新伦理记录(历史日志未记录对象信息)",
: "删除伦理记录(历史日志未记录对象信息)",
: "创建启动会记录(历史日志未记录对象信息)",
: "更新启动会记录(历史日志未记录对象信息)",
: "创建培训授权人员(历史日志未记录人员姓名)",
: "更新培训授权人员(历史日志未记录人员姓名)",
: "删除培训授权人员(历史日志未记录人员姓名)",
: "创建病史记录(历史日志未记录参与者)",
: "更新病史记录(历史日志未记录参与者)",
: "删除病史记录(历史日志未记录参与者)",
访: "更新访视(历史日志未记录访视号)",
};
if (genericLegacyActionMap[text]) return genericLegacyActionMap[text];
const legacySubjectActionMatch = text.match(/^参与者\s+(.+?)\s+已(创建|更新|删除)$/);
if (legacySubjectActionMatch) {
const subjectNo = legacySubjectActionMatch[1].trim();
const verb = legacySubjectActionMatch[2];
return isUuidText(subjectNo) ? `${verb}参与者(历史日志未记录参与者编号)` : `${verb}参与者 ${subjectNo}`;
}
const projectionStatusMatch = text.match(/^projection_status=(.+)$/i); const projectionStatusMatch = text.match(/^projection_status=(.+)$/i);
if (projectionStatusMatch) { if (projectionStatusMatch) {
const status = projectionStatusMatch[1].trim().toLowerCase(); const status = projectionStatusMatch[1].trim().toLowerCase();
@@ -107,7 +275,7 @@ const normalizeLegacyDetailLine = (line: string): string => {
const detailMatch = text.match(/^变更明细[:]\s*(.*)$/); const detailMatch = text.match(/^变更明细[:]\s*(.*)$/);
if (detailMatch) { if (detailMatch) {
const detailText = detailMatch[1].trim(); const detailText = detailMatch[1].trim();
return `变更明细:${detailText || "无"}`; return detailText;
} }
if (text === "立项配置已发布") return "立项配置已发布"; if (text === "立项配置已发布") return "立项配置已发布";
if (text.includes("siteEnrollmentPlans")) return text.replaceAll("siteEnrollmentPlans", "中心入组计划"); if (text.includes("siteEnrollmentPlans")) return text.replaceAll("siteEnrollmentPlans", "中心入组计划");
@@ -124,6 +292,89 @@ const isUuidText = (value: unknown): boolean => {
return new RegExp(`^${uuidTextPattern}$`).test(String(value || "").trim()); return new RegExp(`^${uuidTextPattern}$`).test(String(value || "").trim());
}; };
const replaceUuidReferences = (text: string, userMap: Record<string, string>): string => {
return String(text || "").replace(new RegExp(uuidTextPattern, "g"), (uuid) => userMap[uuid] || "未识别对象");
};
const extractLegacySubjectTargetName = (description: string): string => {
const match = String(description || "").trim().match(/^参与者\s+(.+?)\s+已(?:创建|更新|删除)$/);
if (!match) return "";
const subjectNo = match[1].trim();
return isUuidText(subjectNo) ? "" : subjectNo;
};
const extractLegacyFaqCategoryTargetName = (description: string): string => {
const match = String(description || "").trim().match(/^FAQ\s*分类\s+(.+?)\s+已(?:创建|更新|删除)$/i);
if (!match) return "";
const categoryName = match[1].trim();
return isUuidText(categoryName) ? "" : categoryName;
};
const enrichStructuredDescription = (action: string, description: string, targetName: string): string => {
const desc = String(description || "").trim();
const target = String(targetName || "").trim();
if (!desc || !target || desc.includes("“")) return desc;
if (action === "CREATE_FAQ_ITEM" && desc === "创建医学咨询问题") return `创建医学咨询问题“${target}`;
if (action === "UPDATE_FAQ_ITEM" && desc === "更新医学咨询问题") return `更新医学咨询问题“${target}`;
if (action === "DELETE_FAQ_ITEM" && desc === "删除医学咨询问题") return `删除医学咨询问题“${target}`;
if (action === "CREATE_FAQ_REPLY" && desc === "回复医学咨询问题") return `回复医学咨询问题“${target}`;
if (action === "DELETE_FAQ_REPLY" && desc === "删除医学咨询回复") return `删除医学咨询问题“${target}”的回复`;
const genericStructuredActionMap: Record<string, string> = {
CREATE_DRUG_SHIPMENT: "创建药品流向",
UPDATE_DRUG_SHIPMENT: "更新药品流向",
DELETE_DRUG_SHIPMENT: "删除药品流向",
CREATE_MATERIAL_EQUIPMENT: "创建设备",
UPDATE_MATERIAL_EQUIPMENT: "更新设备",
DELETE_MATERIAL_EQUIPMENT: "删除设备",
CREATE_PRECAUTION: "创建注意事项",
UPDATE_PRECAUTION: "更新注意事项",
DELETE_PRECAUTION: "删除注意事项",
CREATE_VISIT: "创建访视",
UPDATE_VISIT: "更新访视",
DELETE_VISIT: "删除访视",
CREATE_AE: "创建AE",
UPDATE_AE: "更新AE",
DELETE_AE: "删除AE",
CREATE_CONTRACT_FEE: "创建合同费用",
UPDATE_CONTRACT_FEE: "更新合同费用",
DELETE_CONTRACT_FEE: "删除合同费用",
CREATE_CONTRACT_FEE_PAYMENT: "创建合同费用分期",
UPDATE_CONTRACT_FEE_PAYMENT: "更新合同费用分期",
DELETE_CONTRACT_FEE_PAYMENT: "删除合同费用分期",
CREATE_STARTUP_FEASIBILITY: "创建立项记录",
UPDATE_STARTUP_FEASIBILITY: "更新立项记录",
DELETE_STARTUP_FEASIBILITY: "删除立项记录",
CREATE_STARTUP_ETHICS: "创建伦理记录",
UPDATE_STARTUP_ETHICS: "更新伦理记录",
DELETE_STARTUP_ETHICS: "删除伦理记录",
CREATE_KICKOFF_MEETING: "创建启动会记录",
UPDATE_KICKOFF_MEETING: "更新启动会记录",
CREATE_TRAINING_AUTH: "创建培训授权人员",
UPDATE_TRAINING_AUTH: "更新培训授权人员",
DELETE_TRAINING_AUTH: "删除培训授权人员",
CREATE_SUBJECT_HISTORY: "创建病史记录",
UPDATE_SUBJECT_HISTORY: "更新病史记录",
DELETE_SUBJECT_HISTORY: "删除病史记录",
};
const structuredPrefix = genericStructuredActionMap[action];
if (structuredPrefix && desc === structuredPrefix) return `${structuredPrefix}${target}`;
return desc;
};
const isSetupModuleSummaryLine = (line: string): boolean => /^变更模块[:]/.test(String(line || "").trim());
const buildActionText = (
dictActionText: string,
diffLines: string[],
summaryText = "",
preferDictAction = false
): string => {
if (summaryText) return summaryText;
if (preferDictAction) return dictActionText;
if (diffLines.length > 0) return diffLines[0];
return dictActionText;
};
const splitAuditDetailSegments = (line: string): string[] => { const splitAuditDetailSegments = (line: string): string[] => {
const text = String(line || "").trim(); const text = String(line || "").trim();
if (!text) return []; if (!text) return [];
@@ -134,6 +385,19 @@ const splitAuditDetailSegments = (line: string): string[] => {
}; };
const auditFieldLabelMap: Record<string, string> = { const auditFieldLabelMap: Record<string, string> = {
id: "记录标识",
trial_id: "所属项目",
study_id: "所属项目",
document_id: "所属文档",
version_id: "文档版本",
distribution_id: "分发记录",
target_id: "分发对象",
site_id: "中心",
etmf_node_id: "eTMF 目录",
current_effective_version_id: "当前生效版本",
parent_version_id: "上一版本",
derived_from_version_id: "来源版本",
owner_id: "负责人",
issue_no: "问题编号", issue_no: "问题编号",
source: "问题来源", source: "问题来源",
monitor_type: "监查类型", monitor_type: "监查类型",
@@ -142,6 +406,7 @@ const auditFieldLabelMap: Record<string, string> = {
recommendation: "建议措施", recommendation: "建议措施",
subject_name: "受试者", subject_name: "受试者",
subject_code: "受试者缩写号", subject_code: "受试者缩写号",
subject_no: "参与者编号",
description: "问题描述", description: "问题描述",
action_taken: "采取措施", action_taken: "采取措施",
follow_up_progress: "跟进计划及进展", follow_up_progress: "跟进计划及进展",
@@ -166,8 +431,75 @@ const auditFieldLabelMap: Record<string, string> = {
city: "城市", city: "城市",
contact: "负责人", contact: "负责人",
phone: "电话", phone: "电话",
doc_no: "文档编号",
doc_type: "文档类型",
title: "文档标题",
scope_type: "文档范围",
version_no: "版本号",
file_hash: "文件校验值",
file_uri: "文件位置",
file_size: "文件大小",
mime_type: "文件类型",
change_summary: "变更说明",
effective_at: "生效时间",
superseded_at: "替代时间",
submitted_at: "提交时间",
approved_at: "批准时间",
withdrawn_at: "撤回时间",
created_at: "创建时间",
created_by: "创建人",
target_type: "分发范围",
ack_type: "回执类型",
acked_at: "回执时间",
note: "备注",
plan_start_date: "计划开始日期", plan_start_date: "计划开始日期",
plan_end_date: "计划结束日期", plan_end_date: "计划结束日期",
screening_date: "筛选日期",
consent_date: "知情同意日期",
enrollment_date: "入组日期",
baseline_date: "基线日期",
completion_date: "完成日期",
actual_medication_count: "实际给药次数",
drop_reason: "脱落原因",
};
const documentVersionStatusLabelMap: Record<string, string> = {
DRAFT: "草稿",
SUBMITTED: "已提交",
APPROVED: "已批准",
EFFECTIVE: "已生效",
SUPERSEDED: "已被替代",
ARCHIVED: "已归档",
WITHDRAWN: "已撤回",
};
const documentStatusLabelMap: Record<string, string> = {
ACTIVE: "使用中",
ARCHIVED: "已归档",
};
const documentScopeTypeLabelMap: Record<string, string> = {
GLOBAL: "项目级文档",
SITE: "中心级文档",
DERIVED: "派生文档",
};
const distributionTargetTypeLabelMap: Record<string, string> = {
SITE: "中心",
ROLE: "角色",
USER: "指定人员",
};
const acknowledgementTypeLabelMap: Record<string, string> = {
RECEIVED: "已接收",
READ: "已阅读",
TRAINED: "已培训",
};
const normalizeEnumToken = (text: string): string => {
const trimmed = String(text || "").trim();
const parts = trimmed.split(".");
return parts[parts.length - 1] || trimmed;
}; };
const stableSerialize = (value: any): string => { const stableSerialize = (value: any): string => {
@@ -202,14 +534,44 @@ const compactObjectValue = (value: Record<string, any>): string => {
return entries.length > 3 ? `${rendered}${entries.length}` : rendered; return entries.length > 3 ? `${rendered}${entries.length}` : rendered;
}; };
const toReadableFieldLabel = (key: string): string => { const toReadableFieldLabel = (key: string, entityType = ""): string => {
const normalized = String(key || "").trim(); const normalized = String(key || "").trim();
if (!normalized) return "字段"; if (!normalized) return "字段";
if (normalized === "status" && entityType === "DOCUMENT_VERSION") return "版本状态";
if (normalized === "status" && entityType === "DOCUMENT") return "文档状态";
if (normalized === "due_at" && (entityType === "DISTRIBUTION" || entityType === "ACKNOWLEDGEMENT")) return "截止日期";
if (auditFieldLabelMap[normalized]) return auditFieldLabelMap[normalized]; if (auditFieldLabelMap[normalized]) return auditFieldLabelMap[normalized];
return `字段(${normalized}`; return `字段(${normalized}`;
}; };
const toReadableValue = (key: string, value: any): string => { const readableUuidPlaceholder = (key: string): string => {
const normalized = String(key || "").trim();
if (normalized === "id") return "已生成";
if (normalized === "trial_id" || normalized === "study_id") return "已关联项目";
if (normalized === "document_id") return "已关联文档";
if (normalized === "version_id" || normalized.endsWith("_version_id")) return "已关联版本";
if (normalized === "distribution_id") return "已关联分发记录";
if (normalized === "site_id") return "已关联中心";
if (normalized === "owner_id" || normalized === "created_by" || normalized.endsWith("_user_id")) return "已关联人员";
if (normalized.endsWith("_id")) return "已关联对象";
return "系统记录";
};
const toReadableEnumValue = (key: string, text: string): string => {
const normalizedText = normalizeEnumToken(text);
if (key === "status") {
return documentVersionStatusLabelMap[normalizedText] || documentStatusLabelMap[normalizedText] || getDictLabel(statusDict, normalizedText) || text;
}
if (key === "scope_type") return documentScopeTypeLabelMap[normalizedText] || text;
if (key === "target_type") return distributionTargetTypeLabelMap[normalizedText] || text;
if (key === "ack_type") return acknowledgementTypeLabelMap[normalizedText] || text;
if (key.toLowerCase().includes("status")) {
return documentVersionStatusLabelMap[normalizedText] || documentStatusLabelMap[normalizedText] || getDictLabel(statusDict, normalizedText) || text;
}
return text;
};
const toReadableValue = (key: string, value: any, userMap: Record<string, string>): string => {
if (value === null || value === undefined || value === "") return "未填写"; if (value === null || value === undefined || value === "") return "未填写";
if (typeof value === "boolean") { if (typeof value === "boolean") {
if (key === "is_active") return value ? "启用" : "停用"; if (key === "is_active") return value ? "启用" : "停用";
@@ -219,14 +581,14 @@ const toReadableValue = (key: string, value: any): string => {
if (typeof value === "string") { if (typeof value === "string") {
const text = value.trim(); const text = value.trim();
if (!text) return "未填写"; if (!text) return "未填写";
if (key.toLowerCase().includes("status")) { if (key === "file_hash") return "已记录校验值";
return getDictLabel(statusDict, text) || text; if (isUuidText(text)) return userMap[text] || readableUuidPlaceholder(key);
} if (key === "version_no") return text.startsWith("V") ? text : `V${text}`;
return text; return toReadableEnumValue(key, text);
} }
if (Array.isArray(value)) { if (Array.isArray(value)) {
if (value.length === 0) return "未填写"; if (value.length === 0) return "未填写";
const rendered = value.slice(0, 3).map((item) => toReadableValue(key, item)).join("、"); const rendered = value.slice(0, 3).map((item) => toReadableValue(key, item, userMap)).join("、");
return value.length > 3 ? `${rendered}${value.length}` : rendered; return value.length > 3 ? `${rendered}${value.length}` : rendered;
} }
if (typeof value === "object") { if (typeof value === "object") {
@@ -235,7 +597,12 @@ const toReadableValue = (key: string, value: any): string => {
return String(value); return String(value);
}; };
const buildDiffText = (before?: Record<string, any>, after?: Record<string, any>) => { const buildDiffText = (
before: Record<string, any> | undefined,
after: Record<string, any> | undefined,
userMap: Record<string, string>,
entityType = ""
) => {
if (!before && !after) return []; if (!before && !after) return [];
const lines: string[] = []; const lines: string[] = [];
const prevObj = before || {}; const prevObj = before || {};
@@ -244,20 +611,16 @@ const buildDiffText = (before?: Record<string, any>, after?: Record<string, any>
const prev = prevObj[key]; const prev = prevObj[key];
const next = nextObj[key]; const next = nextObj[key];
if (isSameValue(prev, next)) return; if (isSameValue(prev, next)) return;
const label = toReadableFieldLabel(key); const label = toReadableFieldLabel(key, entityType);
const prevText = toReadableValue(key, prev); const prevText = toReadableValue(key, prev, userMap);
const nextText = toReadableValue(key, next); const nextText = toReadableValue(key, next, userMap);
lines.push(`${label}${prevText} -> ${nextText}`); lines.push(`${label}${prevText} -> ${nextText}`);
}); });
return lines; return lines;
}; };
export const normalizeAuditEvent = (raw: any, userMap: Record<string, string>): AuditEvent => { export const normalizeAuditEvent = (raw: any, userMap: Record<string, string>): AuditEvent => {
const dict = auditDict[raw.action] || { const dict = auditDict[raw.action] || buildReadableAuditDict(raw.action, raw.entity_type);
label: raw.action ? `${TEXT.audit.eventFallback}${raw.action}` : TEXT.audit.eventFallback,
actionText: raw.action ? `执行了${raw.action}` : TEXT.audit.actionFallback,
targetLabel: toReadableEntityType(raw.entity_type),
};
let detailObj: any = {}; let detailObj: any = {};
if (raw.detail) { if (raw.detail) {
try { try {
@@ -268,18 +631,39 @@ export const normalizeAuditEvent = (raw: any, userMap: Record<string, string>):
} }
const before = detailObj.before; const before = detailObj.before;
const after = detailObj.after; const after = detailObj.after;
const fallbackDescription = typeof detailObj.description === "string" ? detailObj.description.trim() : ""; const detailTargetName = String(detailObj.targetName || "").trim();
let diffText = detailObj.diffText || buildDiffText(before, after); const rawFallbackDescription = typeof detailObj.description === "string" ? detailObj.description.trim() : "";
const fallbackDescription = enrichStructuredDescription(raw.action, rawFallbackDescription, isUuidText(detailTargetName) ? "" : detailTargetName);
let diffText = detailObj.diffText || buildDiffText(before, after, userMap, raw.entity_type || "");
if ((!Array.isArray(diffText) || diffText.length === 0) && fallbackDescription) { if ((!Array.isArray(diffText) || diffText.length === 0) && fallbackDescription) {
diffText = fallbackDescription diffText = fallbackDescription
.split(/[;\n]/) .split(/[;\n]/)
.map((item: string) => item.trim()) .map((item: string) => item.trim())
.filter(Boolean); .filter(Boolean);
} }
const readableDiffText = (Array.isArray(diffText) ? diffText : diffText ? [String(diffText)] : []) const normalizedDiffText = (Array.isArray(diffText) ? diffText : diffText ? [String(diffText)] : [])
.flatMap((line) => splitAuditDetailSegments(String(line))) .flatMap((line) => splitAuditDetailSegments(String(line)))
.map((line) => normalizeLegacyDetailLine(line)) .map((line) => normalizeLegacyDetailLine(line))
.map((line) => replaceUuidReferences(line, userMap))
.filter(Boolean); .filter(Boolean);
const setupModuleSummary = normalizedDiffText.find((line) => isSetupModuleSummaryLine(line)) || "";
const readableDiffText = normalizedDiffText.filter((line) => !isSetupModuleSummaryLine(line));
const legacySubjectTargetName = extractLegacySubjectTargetName(fallbackDescription);
const legacyFaqCategoryTargetName = extractLegacyFaqCategoryTargetName(fallbackDescription);
const targetName = isUuidText(detailTargetName)
? userMap[detailTargetName] || ""
: detailObj.targetName || legacySubjectTargetName || legacyFaqCategoryTargetName || (isUuidText(raw.entity_id) ? "" : raw.entity_id || "");
const preferDictAction = Boolean(explicitAuditActionMap[raw.action] && fallbackDescription);
const hasStructuredDetail = typeof raw.detail === "string" && raw.detail.trim().startsWith("{");
const actionText =
raw.action === "DELETE_SUBJECT" && hasStructuredDetail && fallbackDescription
? fallbackDescription
: buildActionText(
dict.actionText,
readableDiffText,
setupModuleSummary,
preferDictAction
);
return { return {
eventType: raw.action, eventType: raw.action,
eventLabel: dict.label, eventLabel: dict.label,
@@ -290,8 +674,8 @@ export const normalizeAuditEvent = (raw: any, userMap: Record<string, string>):
targetType: raw.entity_type || "", targetType: raw.entity_type || "",
targetTypeLabel: dict.targetLabel, targetTypeLabel: dict.targetLabel,
targetId: raw.entity_id || "", targetId: raw.entity_id || "",
targetName: detailObj.targetName || (isUuidText(raw.entity_id) ? "" : raw.entity_id || ""), targetName,
actionText: dict.actionText, actionText,
before, before,
after, after,
diffText: readableDiffText, diffText: readableDiffText,
@@ -302,8 +686,6 @@ export const normalizeAuditEvent = (raw: any, userMap: Record<string, string>):
}; };
}; };
export { auditDict };
// Deprecated: keep API-compatible no-op to avoid accidental reliance on browser local storage. // Deprecated: keep API-compatible no-op to avoid accidental reliance on browser local storage.
export const logAudit = async (eventType: string, payload: Record<string, any>) => { export const logAudit = async (eventType: string, payload: Record<string, any>) => {
void eventType; void eventType;
@@ -1,25 +0,0 @@
import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
const readSource = () => readFileSync(resolve(__dirname, "./FaqCategoryForm.vue"), "utf8");
describe("FaqCategoryForm drawer editor", () => {
it("uses the shared right-side drawer pattern instead of a dialog", () => {
const source = readSource();
expect(source).toContain("<el-drawer");
expect(source).toContain("faq-category-editor-drawer");
expect(source).toContain('direction="rtl"');
expect(source).toContain("editor-header");
expect(source).toContain("drawer-footer");
expect(source).not.toContain("<el-dialog");
});
it("labels the required name field as category name", () => {
const source = readSource();
expect(source).toContain("TEXT.modules.knowledgeMedicalConsult.categoryName");
expect(source).not.toContain(':label="TEXT.common.fields.name"');
});
});
+23 -3
View File
@@ -11,9 +11,9 @@
> >
<template #header> <template #header>
<div class="editor-header"> <div class="editor-header">
<div class="editor-title-row"> <div class="editor-title-row">
<div class="editor-title">分类管理</div> <div class="editor-title">分类管理</div>
<el-button size="small" class="header-add-btn" @click="addCategory"> <el-button v-if="canCreate" size="small" class="header-add-btn" @click="addCategory">
<el-icon><Plus /></el-icon> 新增 <el-icon><Plus /></el-icon> 新增
</el-button> </el-button>
</div> </div>
@@ -69,11 +69,13 @@
size="small" size="small"
placeholder="分类名称" placeholder="分类名称"
class="row-name-input" class="row-name-input"
:disabled="!cat._isNew && !canUpdate"
@input="cat._dirty = true" @input="cat._dirty = true"
/> />
</div> </div>
<button <button
v-if="cat._isNew ? canCreate : canDelete"
class="row-delete" class="row-delete"
type="button" type="button"
@click.stop="removeCategory(cat)" @click.stop="removeCategory(cat)"
@@ -120,6 +122,9 @@ const props = defineProps<{
modelValue: boolean; modelValue: boolean;
categories: FaqCategory[]; categories: FaqCategory[];
faqs?: any[]; faqs?: any[];
canCreate?: boolean;
canUpdate?: boolean;
canDelete?: boolean;
}>(); }>();
const emit = defineEmits<{ const emit = defineEmits<{
@@ -130,6 +135,9 @@ const emit = defineEmits<{
const study = useStudyStore(); const study = useStudyStore();
const saving = ref(false); const saving = ref(false);
let keyCounter = 0; let keyCounter = 0;
const canCreate = computed(() => props.canCreate !== false);
const canUpdate = computed(() => props.canUpdate !== false);
const canDelete = computed(() => props.canDelete !== false);
const editableCategories = ref<EditableCategory[]>([]); const editableCategories = ref<EditableCategory[]>([]);
@@ -174,6 +182,10 @@ const loadFromProps = () => {
}; };
const addCategory = () => { const addCategory = () => {
if (!canCreate.value) {
ElMessage.warning(TEXT.common.messages.noPermission);
return;
}
editableCategories.value.push({ editableCategories.value.push({
_key: `new-${++keyCounter}`, _key: `new-${++keyCounter}`,
_dirty: true, _dirty: true,
@@ -219,6 +231,11 @@ const moveDown = (idx: number) => {
}; };
const removeCategory = (cat: EditableCategory) => { const removeCategory = (cat: EditableCategory) => {
const allowed = cat._isNew ? canCreate.value : canDelete.value;
if (!allowed) {
ElMessage.warning(TEXT.common.messages.noPermission);
return;
}
if (!cat._isNew) { if (!cat._isNew) {
const count = (props.faqs || []).filter((f: any) => f.category_id === cat.id).length; const count = (props.faqs || []).filter((f: any) => f.category_id === cat.id).length;
if (count > 0) { if (count > 0) {
@@ -250,6 +267,7 @@ const saveAll = async () => {
const existingIds = all.filter((c) => !c._isNew && c.id).map((c) => c.id); const existingIds = all.filter((c) => !c._isNew && c.id).map((c) => c.id);
for (const orig of props.categories) { for (const orig of props.categories) {
if (!existingIds.includes(orig.id)) { if (!existingIds.includes(orig.id)) {
if (!canDelete.value) continue;
await deleteFaqCategory(orig.id, studyId); await deleteFaqCategory(orig.id, studyId);
} }
} }
@@ -270,9 +288,11 @@ const saveAll = async () => {
}; };
if (cat._isNew) { if (cat._isNew) {
if (!canCreate.value) continue;
if (!cat.name.trim()) continue; if (!cat.name.trim()) continue;
await createFaqCategory(payload); await createFaqCategory(payload);
} else if (cat._dirty) { } else if (cat._dirty) {
if (!canUpdate.value) continue;
await updateFaqCategory(cat.id, payload); await updateFaqCategory(cat.id, payload);
} }
} }
+13 -2
View File
@@ -2,7 +2,7 @@
<aside class="panel unified-shell faq-category-panel"> <aside class="panel unified-shell faq-category-panel">
<div class="header"> <div class="header">
<span>{{ TEXT.common.labels.category }}</span> <span>{{ TEXT.common.labels.category }}</span>
<div v-if="canCreateCategory"> <div v-if="canManageCategories">
<el-button type="primary" size="small" class="category-create-btn" @click="openManager()">编辑</el-button> <el-button type="primary" size="small" class="category-create-btn" @click="openManager()">编辑</el-button>
</div> </div>
</div> </div>
@@ -21,7 +21,15 @@
<span class="cat-count">{{ categoryCounts[c.id] || 0 }}</span> <span class="cat-count">{{ categoryCounts[c.id] || 0 }}</span>
</el-menu-item> </el-menu-item>
</el-menu> </el-menu>
<FaqCategoryManager v-model="showForm" :categories="props.categories" :faqs="props.faqs" @success="emit('refresh')" /> <FaqCategoryManager
v-model="showForm"
:categories="props.categories"
:faqs="props.faqs"
:can-create="canCreateCategory"
:can-update="canUpdateCategory"
:can-delete="canDeleteCategory"
@success="emit('refresh')"
/>
</aside> </aside>
</template> </template>
@@ -44,6 +52,9 @@ const showForm = ref(false);
const { can } = usePermission(); const { can } = usePermission();
const canCreateCategory = computed(() => can("faq.category.create")); const canCreateCategory = computed(() => can("faq.category.create"));
const canUpdateCategory = computed(() => can("faq.category.update"));
const canDeleteCategory = computed(() => can("faq.category.delete"));
const canManageCategories = computed(() => canCreateCategory.value || canUpdateCategory.value || canDeleteCategory.value);
const sortedCategories = computed(() => const sortedCategories = computed(() =>
[...props.categories].sort((a, b) => (a.sort_order ?? 0) - (b.sort_order ?? 0)) [...props.categories].sort((a, b) => (a.sort_order ?? 0) - (b.sort_order ?? 0))
); );
@@ -1,18 +0,0 @@
import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
const readSource = () => readFileSync(resolve(__dirname, "./FaqItemForm.vue"), "utf8");
describe("FaqItemForm drawer", () => {
it("uses the unified drawer pattern instead of a dialog", () => {
const source = readSource();
expect(source).toContain("<el-drawer");
expect(source).toContain('class="faq-item-editor-drawer"');
expect(source).toContain('class="editor-header"');
expect(source).toContain('class="drawer-footer"');
expect(source).not.toContain("<el-dialog");
expect(source).not.toContain('append-to=".layout-main .content-wrapper"');
});
});
File diff suppressed because it is too large Load Diff
@@ -1,44 +0,0 @@
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { describe, expect, it } from "vitest";
const readLayout = () => readFileSync(resolve(__dirname, "./Layout.vue"), "utf8");
describe("Layout breadcrumbs", () => {
it("shows the file version module before document detail titles", () => {
const source = readLayout();
expect(source).toContain('"/documents": { label: TEXT.menu.fileVersionManagement');
expect(source).toContain("study.viewContext?.pageTitle");
expect(source).toContain("items.push({ label: study.viewContext.pageTitle, path: route.path })");
});
it("uses context page titles instead of generic route detail titles", () => {
const source = readLayout();
expect(source).toContain("const hasContextPageTitle = Boolean(study.viewContext?.pageTitle)");
expect(source).toContain("if (!hasContextPageTitle && title");
});
it("keeps project management as the parent for admin project detail breadcrumbs", () => {
const source = readLayout();
expect(source).toContain('if (route.path.startsWith("/admin/projects/"))');
expect(source).toContain("label: TEXT.menu.projectManagement");
expect(source).toContain('path: "/admin/projects"');
});
it("uses stable breadcrumb keys when context titles change", () => {
const source = readLayout();
expect(source).toContain(':key="breadcrumbKey(item, index)"');
expect(source).not.toContain(':key="index"');
expect(source).toContain("const breadcrumbKey = (item: any, index: number)");
});
it("guards route component rendering while router resolves async views", () => {
const source = readLayout();
expect(source).toContain('<component v-if="Component" :is="Component" />');
});
});
-228
View File
@@ -1,228 +0,0 @@
<template>
<teleport to="body">
<div class="lock-screen" role="dialog" aria-modal="true">
<div class="lock-backdrop" />
<div class="lock-panel">
<h2 class="lock-title">已锁定</h2>
<p class="lock-desc">30 分钟无操作将锁定2 小时无操作将自动退出登录</p>
<p class="lock-countdown">剩余自动登出{{ autoLogoutCountdown }}</p>
<el-input
ref="passwordInput"
v-model="password"
type="password"
autocomplete="new-password"
name="lock-password"
placeholder="请输入登录密码"
show-password
@keyup.enter="onUnlock"
/>
<div class="lock-actions">
<el-button type="primary" :loading="loading" @click="onUnlock">解锁</el-button>
<el-button @click="onLogout">退出登录</el-button>
</div>
<p v-if="errorMessage" class="lock-error">{{ errorMessage }}</p>
</div>
</div>
</teleport>
</template>
<script setup lang="ts">
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from "vue";
import { ElMessage } from "element-plus";
import { useAuthStore } from "../store/auth";
import { useSessionStore } from "../store/session";
import {
AUTO_LOGOUT_TIMEOUT_HOURS,
UNLOCK_MAX_ATTEMPTS,
unlockWithPassword,
forceLogout,
} from "../session/sessionManager";
const auth = useAuthStore();
const session = useSessionStore();
const password = ref("");
const loading = ref(false);
const errorMessage = ref("");
const passwordInput = ref<{ focus?: () => void } | null>(null);
const nowTs = ref(Date.now());
let countdownTimer: number | null = null;
const pad2 = (value: number) => String(value).padStart(2, "0");
const autoLogoutCountdown = computed(() => {
const fallbackDeadlineAt =
Math.max(session.lastUserActiveAt, session.lastNetworkActiveAt) + AUTO_LOGOUT_TIMEOUT_HOURS * 60 * 60 * 1000;
const deadlineAt = session.lockAutoLogoutDeadlineAt || fallbackDeadlineAt;
const remainMs = Math.max(0, deadlineAt - nowTs.value);
const remainSec = Math.floor(remainMs / 1000);
const hours = Math.floor(remainSec / 3600);
const minutes = Math.floor((remainSec % 3600) / 60);
const seconds = remainSec % 60;
if (hours > 0) {
return `${pad2(hours)}:${pad2(minutes)}:${pad2(seconds)}`;
}
return `${pad2(minutes)}:${pad2(seconds)}`;
});
const startCountdown = () => {
if (countdownTimer) {
window.clearInterval(countdownTimer);
}
nowTs.value = Date.now();
countdownTimer = window.setInterval(() => {
nowTs.value = Date.now();
}, 1000);
};
const stopCountdown = () => {
if (!countdownTimer) return;
window.clearInterval(countdownTimer);
countdownTimer = null;
};
const focusPassword = () => {
nextTick(() => {
passwordInput.value?.focus?.();
});
};
const resolveUnlockEmail = async () => {
if (session.lockEmail) return session.lockEmail;
if (auth.user?.email) return auth.user.email;
const cachedEmail = localStorage.getItem("ctms_last_login_email");
if (cachedEmail) return cachedEmail;
try {
const me = await auth.fetchMe();
return me?.email || "";
} catch {
return "";
}
};
const onUnlock = async () => {
if (loading.value) return;
errorMessage.value = "";
const email = await resolveUnlockEmail();
if (!email) {
ElMessage.error("无法获取用户信息,请重新登录");
forceLogout();
return;
}
if (!password.value) {
errorMessage.value = "请输入密码";
focusPassword();
return;
}
loading.value = true;
try {
await unlockWithPassword(email, password.value);
password.value = "";
} catch (err: any) {
session.incrementUnlockAttempt();
const message = err?.response?.data?.detail || err?.response?.data?.message || err?.message || "密码错误";
errorMessage.value = message;
if (session.unlockAttempts >= UNLOCK_MAX_ATTEMPTS) {
ElMessage.error("错误次数过多,请重新登录");
forceLogout();
}
} finally {
loading.value = false;
focusPassword();
}
};
const onLogout = () => {
forceLogout();
};
onMounted(() => {
password.value = "";
errorMessage.value = "";
focusPassword();
if (session.locked) {
startCountdown();
}
});
watch(
() => session.locked,
(value) => {
if (value) {
password.value = "";
errorMessage.value = "";
focusPassword();
startCountdown();
return;
}
password.value = "";
errorMessage.value = "";
stopCountdown();
}
);
onUnmounted(() => {
stopCountdown();
});
</script>
<style scoped>
.lock-screen {
position: fixed;
inset: 0;
z-index: 2000;
display: grid;
place-items: center;
}
.lock-backdrop {
position: absolute;
inset: 0;
background: rgba(7, 10, 20, 0.35);
backdrop-filter: blur(10px);
}
.lock-panel {
position: relative;
z-index: 1;
width: min(420px, 92vw);
background: #ffffff;
border-radius: 16px;
padding: 28px;
box-shadow: 0 18px 48px rgba(15, 20, 35, 0.2);
display: flex;
flex-direction: column;
gap: 16px;
}
.lock-title {
margin: 0;
font-size: 22px;
font-weight: 600;
color: #0f172a;
}
.lock-desc {
margin: 0;
color: #64748b;
font-size: 14px;
}
.lock-countdown {
margin: -4px 0 0;
color: #1d4ed8;
font-size: 13px;
font-weight: 600;
}
.lock-actions {
display: flex;
gap: 12px;
justify-content: flex-end;
}
.lock-error {
margin: 0;
color: #d8342c;
font-size: 13px;
}
</style>
@@ -57,6 +57,16 @@ describe("PermissionAccessLogs", () => {
expect(source).not.toContain("筛选范围内行为总量"); expect(source).not.toContain("筛选范围内行为总量");
}); });
it("keeps audit grid widths consistent with SecurityCenter", () => {
const source = readSource();
expect(source).toContain("grid-template-columns: minmax(320px, 1fr) minmax(0, 1.35fr);");
expect(source).toContain("@media (max-width: 1100px)");
expect(source).toContain("grid-template-columns: repeat(2, minmax(0, 1fr));");
expect(source).toContain("@media (max-width: 720px)");
expect(source).toContain("grid-template-columns: 1fr;");
});
it("removes the duplicated audit hero copy", () => { it("removes the duplicated audit hero copy", () => {
const source = readSource(); const source = readSource();
@@ -65,6 +75,7 @@ describe("PermissionAccessLogs", () => {
expect(source).not.toContain("权限监控 / 访问日志"); expect(source).not.toContain("权限监控 / 访问日志");
expect(source).not.toContain("用户行为审计"); expect(source).not.toContain("用户行为审计");
expect(source).not.toContain("按用户行为聚合访问记录,接口访问细节保留在终端式流水中"); expect(source).not.toContain("按用户行为聚合访问记录,接口访问细节保留在终端式流水中");
expect(source).not.toContain("查看详细的接口访问与安全事件记录");
}); });
it("streams terminal logs from top to bottom and refreshes in realtime", () => { it("streams terminal logs from top to bottom and refreshes in realtime", () => {
@@ -85,14 +96,56 @@ describe("PermissionAccessLogs", () => {
expect(source).toContain("ip=${ip}"); expect(source).toContain("ip=${ip}");
expect(source).toContain("user.sample_ip_address || \"未知 IP\""); expect(source).toContain("user.sample_ip_address || \"未知 IP\"");
expect(source).toContain("ipRankingItems");
expect(source).toContain("IP访问排行"); expect(source).toContain("IP访问排行");
expect(source).toContain("账号"); expect(source).toContain("账号");
expect(source).toContain("rank-location"); expect(source).toContain("rank-location");
expect(source).not.toContain("TOP {{ userStats.length }}");
expect(source).not.toContain("rank-ip-line"); expect(source).not.toContain("rank-ip-line");
expect(source).not.toContain("来源IP"); expect(source).not.toContain("来源IP");
expect(source).not.toContain("去重IP"); expect(source).not.toContain("去重IP");
}); });
it("merges abnormal security IPs into the IP ranking and limits the list to top 10", () => {
const source = readSource();
expect(source).toContain("const IP_RANKING_LIMIT = 10;");
expect(source).toContain("const SECURITY_RANKING_PAGE_SIZE = 200;");
expect(source).toContain("const ipRankingItems = computed<IpRankingItem[]>(() =>");
expect(source).toContain("securityLogs.value.forEach((event) =>");
expect(source).toContain('const isAbnormal = event.category === "ABNORMAL_IP";');
expect(source).toContain('ABNORMAL_IP: "异常IP"');
expect(source).toContain(".slice(0, IP_RANKING_LIMIT)");
expect(source).toContain("TOP {{ ipRankingItems.length }}");
expect(source).toContain('v-for="(item, index) in ipRankingItems"');
expect(source).toContain("异常IP</el-tag>");
});
it("sorts IP ranking by overall access total before risk labels", () => {
const source = readSource();
expect(source).toContain("existing.total += user.total_count;");
expect(source).toContain("existing.riskCount += user.denied_count;");
expect(source).toContain("if (b.total !== a.total) return b.total - a.total;");
expect(source.indexOf("if (b.total !== a.total) return b.total - a.total;")).toBeLessThan(
source.indexOf("if (b.riskCount !== a.riskCount) return b.riskCount - a.riskCount;"),
);
expect(source.indexOf("if (b.total !== a.total) return b.total - a.total;")).toBeLessThan(
source.indexOf("if (b.isAbnormal !== a.isAbnormal) return Number(b.isAbnormal) - Number(a.isAbnormal);"),
);
});
it("loads all security pages for complete abnormal IP ranking data", () => {
const source = readSource();
expect(source).toContain("const first = await fetchSecurityAccessLogs({ status_min: 400, page: 1, page_size: SECURITY_RANKING_PAGE_SIZE });");
expect(source).toContain("const totalPages = Math.ceil(first.data.total / SECURITY_RANKING_PAGE_SIZE);");
expect(source).toContain("for (let nextPage = 2; nextPage <= totalPages; nextPage += 1)");
expect(source).toContain("allItems.push(...res.data.items);");
expect(source).toContain("securityLogs.value = allItems;");
expect(source).not.toContain("page_size: 80");
});
it("renders low-level security access logs for anonymous and invalid requests", () => { it("renders low-level security access logs for anonymous and invalid requests", () => {
const source = readSource(); const source = readSource();
@@ -111,6 +164,16 @@ describe("PermissionAccessLogs", () => {
expect(source).not.toContain("记录匿名、无效令牌、拒绝和异常状态请求,用于排查未知 IP 攻击"); expect(source).not.toContain("记录匿名、无效令牌、拒绝和异常状态请求,用于排查未知 IP 攻击");
}); });
it("shows interface audit log total instead of current page line count", () => {
const source = readSource();
expect(source).toContain("{{ formatNumber(total) }} 条");
expect(source).toContain("共 {{ formatNumber(total) }} 条");
expect(source).toContain(":total=\"total\"");
expect(source).not.toContain("{{ terminalLines.length }} 条");
expect(source).not.toContain("最近 {{ terminalLines.length }} 条");
});
it("opens both audit logs in realtime downloadable dialogs", () => { it("opens both audit logs in realtime downloadable dialogs", () => {
const source = readSource(); const source = readSource();
+139 -22
View File
@@ -44,21 +44,23 @@
<div class="section-head"> <div class="section-head">
<div class="section-title-group"> <div class="section-title-group">
<h4>IP访问排行</h4> <h4>IP访问排行</h4>
<p>按来源 IP 排序账号作为辅助定位</p>
</div> </div>
<el-tag effect="plain" size="small" type="info">TOP {{ userStats.length }}</el-tag> <el-tag effect="plain" size="small" type="info">TOP {{ ipRankingItems.length }}</el-tag>
</div> </div>
<div v-if="userStats.length" class="user-rank-list"> <div v-if="ipRankingItems.length" class="user-rank-list">
<div v-for="(user, index) in userStats" :key="`${user.sample_ip_address}-${user.user_id}-${user.role}`" class="user-rank-row"> <div v-for="(item, index) in ipRankingItems" :key="item.ip" class="user-rank-row" :class="{ 'abnormal-rank-row': item.isAbnormal }">
<span class="rank-no" :class="{ 'rank-top': index < 3 }">{{ String(index + 1).padStart(2, "0") }}</span> <span class="rank-no" :class="{ 'rank-top': index < 3 }">{{ String(index + 1).padStart(2, "0") }}</span>
<div class="rank-user"> <div class="rank-user">
<strong>{{ user.sample_ip_address || "未知 IP" }}</strong> <strong>
<small>{{ user.user_name }} / {{ roleLabel(user.role) }}</small> {{ item.ip }}
<span class="rank-location">{{ user.primary_location || "未知属地" }}</span> <el-tag v-if="item.isAbnormal" class="rank-risk-tag" effect="plain" size="small" type="danger">异常IP</el-tag>
</strong>
<small>{{ item.label }}</small>
<span class="rank-location">{{ item.location }}</span>
</div> </div>
<div class="rank-count"> <div class="rank-count">
<strong>{{ user.total_count }}</strong> <strong>{{ item.total }}</strong>
<small :class="{ 'denied-highlight': user.denied_count > 0 }">{{ user.denied_count }} 拒绝</small> <small :class="{ 'denied-highlight': item.riskCount > 0 }">{{ item.riskCount }} 异常/拒绝</small>
</div> </div>
</div> </div>
</div> </div>
@@ -69,7 +71,6 @@
<div class="section-head"> <div class="section-head">
<div class="section-title-group"> <div class="section-title-group">
<h4>日志审计</h4> <h4>日志审计</h4>
<p>查看详细的接口访问与安全事件记录</p>
</div> </div>
</div> </div>
<div class="log-actions"> <div class="log-actions">
@@ -81,7 +82,7 @@
<strong>接口访问审计日志</strong> <strong>接口访问审计日志</strong>
<span>记录所有 API 接口的访问行为</span> <span>记录所有 API 接口的访问行为</span>
</div> </div>
<el-tag effect="dark" size="small" round>{{ terminalLines.length }} </el-tag> <el-tag effect="dark" size="small" round>{{ formatNumber(total) }} </el-tag>
</div> </div>
<div v-if="showSecurityLog" class="log-action-item log-action-security" @click="openSecurityLogDialog"> <div v-if="showSecurityLog" class="log-action-item log-action-security" @click="openSecurityLogDialog">
<div class="log-action-icon security"> <div class="log-action-icon security">
@@ -103,7 +104,7 @@
<div class="dialog-head"> <div class="dialog-head">
<strong>接口访问审计日志</strong> <strong>接口访问审计日志</strong>
<div class="dialog-actions"> <div class="dialog-actions">
<el-tag effect="plain" type="info">最近 {{ terminalLines.length }} </el-tag> <el-tag effect="plain" type="info"> {{ formatNumber(total) }} </el-tag>
<el-button size="small" type="primary" @click="downloadInterfaceLog">下载日志</el-button> <el-button size="small" type="primary" @click="downloadInterfaceLog">下载日志</el-button>
</div> </div>
</div> </div>
@@ -180,6 +181,8 @@ const MetricIconSpeed = () => h("svg", { viewBox: "0 0 24 24", fill: "none", str
]); ]);
const REALTIME_POLL_INTERVAL_MS = 3000; const REALTIME_POLL_INTERVAL_MS = 3000;
const IP_RANKING_LIMIT = 10;
const SECURITY_RANKING_PAGE_SIZE = 200;
const emptySummary: AccessLogsSummary = { const emptySummary: AccessLogsSummary = {
total_count: 0, total_count: 0,
@@ -228,6 +231,26 @@ const SECURITY_AUTH_LABELS: Record<string, string> = {
AUTHENTICATED: "已认证", AUTHENTICATED: "已认证",
}; };
const SECURITY_CATEGORY_LABELS: Record<string, string> = {
ABNORMAL_IP: "异常IP",
PROBE: "敏感路径探测",
INVALID_TOKEN: "无效令牌",
SERVER_ERROR: "服务异常",
ANONYMOUS_API: "匿名 API",
NOT_FOUND_NOISE: "普通 404",
OTHER: "其他",
};
interface IpRankingItem {
ip: string;
location: string;
label: string;
total: number;
riskCount: number;
isAbnormal: boolean;
lastSeenAt: string | null;
}
const formatTerminalTime = (iso: string) => { const formatTerminalTime = (iso: string) => {
const date = new Date(iso); const date = new Date(iso);
const pad = (value: number) => String(value).padStart(2, "0"); const pad = (value: number) => String(value).padStart(2, "0");
@@ -239,6 +262,13 @@ const formatIpLocation = (row: AccessLogItem) => {
return parts.length ? parts.join(" / ") : row.ip_location; return parts.length ? parts.join(" / ") : row.ip_location;
}; };
const formatSecurityIpLocation = (row: SecurityAccessLogItem) => {
const parts = [row.ip_country && row.ip_country !== "中国" ? row.ip_country : "", row.ip_province, row.ip_city, row.ip_isp].filter(Boolean);
return parts.length ? parts.join(" / ") : row.ip_location;
};
const categoryLabel = (category: string) => SECURITY_CATEGORY_LABELS[category] || category || "其他";
const formatNumber = (value: number) => new Intl.NumberFormat("zh-CN").format(value || 0); const formatNumber = (value: number) => new Intl.NumberFormat("zh-CN").format(value || 0);
const metricCards = computed(() => [ const metricCards = computed(() => [
@@ -304,6 +334,77 @@ const securityTerminalLines = computed(() => {
return lines.filter((line) => line.toLowerCase().includes(token)); return lines.filter((line) => line.toLowerCase().includes(token));
}); });
const ipRankingItems = computed<IpRankingItem[]>(() => {
const rankingByIp = new Map<string, IpRankingItem>();
userStats.value.forEach((user) => {
const ip = user.sample_ip_address || "未知 IP";
const existing = rankingByIp.get(ip);
const lastSeenAt =
existing?.lastSeenAt && new Date(existing.lastSeenAt).getTime() > new Date(user.last_seen_at || 0).getTime()
? existing.lastSeenAt
: user.last_seen_at;
if (!existing) {
rankingByIp.set(ip, {
ip,
location: user.primary_location || "未知属地",
label: `${user.user_name || "未知用户"} / ${roleLabel(user.role)}`,
total: user.total_count,
riskCount: user.denied_count,
isAbnormal: false,
lastSeenAt,
});
return;
}
existing.total += user.total_count;
existing.riskCount += user.denied_count;
existing.lastSeenAt = lastSeenAt;
if (existing.location === "未知属地" && user.primary_location) existing.location = user.primary_location;
});
securityLogs.value.forEach((event) => {
const ip = event.client_ip || "未知 IP";
const existing = rankingByIp.get(ip);
const isAbnormal = event.category === "ABNORMAL_IP";
const location = formatSecurityIpLocation(event) || existing?.location || "未知属地";
const lastSeenAt =
existing?.lastSeenAt && new Date(existing.lastSeenAt).getTime() > new Date(event.created_at).getTime()
? existing.lastSeenAt
: event.created_at;
if (!existing) {
rankingByIp.set(ip, {
ip,
location,
label: isAbnormal ? categoryLabel(event.category) : `${categoryLabel(event.category)} / ${event.account_label || "未知账号"}`,
total: 1,
riskCount: isAbnormal || event.severity === "HIGH" || event.severity === "CRITICAL" ? 1 : 0,
isAbnormal,
lastSeenAt,
});
return;
}
existing.location = location;
existing.total += 1;
existing.riskCount += isAbnormal || event.severity === "HIGH" || event.severity === "CRITICAL" ? 1 : 0;
existing.isAbnormal = existing.isAbnormal || isAbnormal;
existing.lastSeenAt = lastSeenAt;
if (isAbnormal) existing.label = categoryLabel(event.category);
});
return [...rankingByIp.values()]
.sort((a, b) => {
if (b.total !== a.total) return b.total - a.total;
if (b.riskCount !== a.riskCount) return b.riskCount - a.riskCount;
if (b.isAbnormal !== a.isAbnormal) return Number(b.isAbnormal) - Number(a.isAbnormal);
return new Date(b.lastSeenAt || 0).getTime() - new Date(a.lastSeenAt || 0).getTime();
})
.slice(0, IP_RANKING_LIMIT);
});
const onFilterChange = () => { const onFilterChange = () => {
page.value = 1; page.value = 1;
loadData(); loadData();
@@ -363,9 +464,15 @@ const loadSecurityData = async (options: { silent?: boolean } = {}) => {
if (!showSecurityLog.value) return; if (!showSecurityLog.value) return;
if (!options.silent) securityLoading.value = true; if (!options.silent) securityLoading.value = true;
try { try {
const res = await fetchSecurityAccessLogs({ status_min: 400, page: 1, page_size: 80 }); const first = await fetchSecurityAccessLogs({ status_min: 400, page: 1, page_size: SECURITY_RANKING_PAGE_SIZE });
securityLogs.value = res.data.items; const allItems = [...first.data.items];
securitySummary.value = res.data.summary || emptySecuritySummary; const totalPages = Math.ceil(first.data.total / SECURITY_RANKING_PAGE_SIZE);
for (let nextPage = 2; nextPage <= totalPages; nextPage += 1) {
const res = await fetchSecurityAccessLogs({ status_min: 400, page: nextPage, page_size: SECURITY_RANKING_PAGE_SIZE });
allItems.push(...res.data.items);
}
securityLogs.value = allItems;
securitySummary.value = first.data.summary || emptySecuritySummary;
if (securityLogDialogVisible.value) scrollSecurityTerminalToBottom(); if (securityLogDialogVisible.value) scrollSecurityTerminalToBottom();
} catch { } catch {
if (options.silent) return; if (options.silent) return;
@@ -551,7 +658,7 @@ defineExpose({ refresh });
.audit-grid { .audit-grid {
display: grid; display: grid;
grid-template-columns: 380px minmax(0, 1fr); grid-template-columns: minmax(320px, 1fr) minmax(0, 1.35fr);
gap: 14px; gap: 14px;
} }
@@ -632,6 +739,10 @@ defineExpose({ refresh });
} }
.rank-user strong { .rank-user strong {
display: flex;
align-items: center;
gap: 6px;
min-width: 0;
color: var(--audit-ink); color: var(--audit-ink);
font-size: 14px; font-size: 14px;
font-weight: 600; font-weight: 600;
@@ -667,6 +778,14 @@ defineExpose({ refresh });
font-weight: 600; font-weight: 600;
} }
.abnormal-rank-row {
background: #fff7f7;
}
.rank-risk-tag {
flex-shrink: 0;
}
.log-actions { .log-actions {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@@ -798,17 +917,15 @@ defineExpose({ refresh });
} }
@media (max-width: 1100px) { @media (max-width: 1100px) {
.audit-metrics { .audit-metrics,
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.audit-grid { .audit-grid {
grid-template-columns: 1fr; grid-template-columns: repeat(2, minmax(0, 1fr));
} }
} }
@media (max-width: 720px) { @media (max-width: 720px) {
.audit-metrics { .audit-metrics,
.audit-grid {
grid-template-columns: 1fr; grid-template-columns: 1fr;
} }
} }
@@ -1,82 +0,0 @@
import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
const readSource = () => readFileSync(resolve(__dirname, "./PermissionIpLocations.vue"), "utf8");
const readMapSource = () => readFileSync(resolve(__dirname, "./chinaProvinceMap.ts"), "utf8");
describe("PermissionIpLocations", () => {
it("renders IP location visualization without duplicate region detail table", () => {
const source = readSource();
expect(source).toContain("fetchIpLocations");
expect(source).toContain("unique_ip_count");
expect(source).not.toContain("来源地域明细");
expect(source).not.toContain("table-card");
expect(source).not.toContain("<el-table :data=\"items\"");
});
it("renders an interactive China map and primary metric cards", () => {
const source = readSource();
expect(source).toContain("VChart");
expect(source).toContain("chinaMapOption");
expect(source).toContain("用户分布图");
expect(source).toContain("访问次数");
expect(source).toContain("访问用户数");
expect(source).toContain("来源 IP 数");
expect(source).toContain("summary");
});
it("uses the standard geojson.cn China map data instead of generated rectangles", () => {
const source = readSource();
const mapSource = readMapSource();
expect(source).toContain("../assets/china.json");
expect(mapSource).toContain("geojson.cn/api/china/1.6.3/china.json");
expect(mapSource).not.toContain("ProvinceBox");
expect(mapSource).not.toContain("provinceBoxes");
});
it("keeps the map legend and disables map zoom interactions", () => {
const source = readSource();
expect(source).toContain("visualMap:");
expect(source).toContain("VisualMapComponent");
expect(source).toContain("max: maxValue.value");
expect(source).toContain("roam: false");
expect(source).not.toContain("scaleLimit");
});
it("uses narrow metric cards without helper subtitles", () => {
const source = readSource();
expect(source).toContain("min-height: 64px");
expect(source).toContain("padding: 10px 16px");
expect(source).toContain("right: -46px");
expect(source).toContain("bottom: -52px");
expect(source).toContain("border-radius: 18px");
expect(source).not.toContain("<small>{{ card.hint }}</small>");
expect(source).not.toContain("hint:");
expect(source).not.toContain("权限检查总量");
});
it("removes the duplicated hero copy above the map", () => {
const source = readSource();
expect(source).toContain("ip-locations-toolbar");
expect(source).not.toContain("权限监控 / IP属地");
expect(source).not.toContain("按访问日志聚合省市、用户与来源 IP");
expect(source).not.toContain("悬停省份查看访问次数、访问用户数和来源 IP 数");
expect(source).not.toContain("hero-desc");
expect(source).not.toContain("eyebrow");
});
it("places the period filter toolbar on the left", () => {
const source = readSource();
expect(source).toContain(".ip-hero");
expect(source).toContain("justify-content: flex-start");
expect(source).not.toContain("justify-content: flex-end");
});
});
+534 -105
View File
@@ -3,8 +3,7 @@
<section class="ip-hero"> <section class="ip-hero">
<div class="ip-locations-toolbar"> <div class="ip-locations-toolbar">
<div class="toolbar-left"> <div class="toolbar-left">
<span class="toolbar-title">IP 属地分析</span> <span class="toolbar-title">访问来源分析</span>
<span class="toolbar-desc">查看访问来源的地理分布</span>
</div> </div>
<div class="toolbar-actions"> <div class="toolbar-actions">
<el-radio-group v-model="days" size="small" @change="loadData"> <el-radio-group v-model="days" size="small" @change="loadData">
@@ -13,7 +12,7 @@
<el-radio-button :value="30">30</el-radio-button> <el-radio-button :value="30">30</el-radio-button>
<el-radio-button :value="90">90</el-radio-button> <el-radio-button :value="90">90</el-radio-button>
</el-radio-group> </el-radio-group>
<el-button :loading="loading" size="small" @click="loadData"> <el-button :loading="loading" size="small" :disabled="loading" @click="loadData">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width: 14px; height: 14px; margin-right: 4px"><polyline points="23 4 23 10 17 10"/><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"/></svg> <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="width: 14px; height: 14px; margin-right: 4px"><polyline points="23 4 23 10 17 10"/><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"/></svg>
刷新 刷新
</el-button> </el-button>
@@ -33,23 +32,44 @@
</article> </article>
</section> </section>
<section v-loading="loading" class="distribution-card"> <section class="distribution-card">
<div class="map-panel"> <div class="map-panel">
<div class="section-head"> <div class="section-head">
<div class="section-title-group"> <div class="section-title-group">
<h4>中国地图</h4> <h4>来源分布</h4>
<p>访问来源地理分布热力图</p> </div>
<div class="map-head-actions">
<el-segmented v-model="mapView" :options="mapViewOptions" size="small" />
<el-tag type="info" effect="plain" round size="small">{{ currentPeriodLabel }}</el-tag>
<el-tooltip content="全屏预览" placement="top">
<el-button class="map-fullscreen-button" size="small" circle aria-label="全屏预览" @click="openFullscreenPreview">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M8 3H3v5"/><path d="M16 3h5v5"/><path d="M21 16v5h-5"/><path d="M3 16v5h5"/><path d="M3 3l6 6"/><path d="M21 3l-6 6"/><path d="M21 21l-6-6"/><path d="M3 21l6-6"/></svg>
</el-button>
</el-tooltip>
</div>
</div>
<div class="source-map-wrap">
<v-chart
ref="sourceChartRef"
:key="mapView"
:option="sourceMapOption"
:init-options="chartInitOptions"
:update-options="chartUpdateOptions"
autoresize
class="source-map"
/>
<div class="flow-legend map-legend" aria-label="访问链路图例">
<span class="legend-item"><i class="legend-dot server"></i>服务器</span>
<span class="legend-item"><i class="legend-dot normal"></i>正常访问点</span>
<span class="legend-item"><i class="legend-dot abnormal"></i>异常访问点</span>
</div> </div>
<el-tag type="info" effect="plain" round size="small">{{ currentPeriodLabel }}</el-tag>
</div> </div>
<v-chart :option="chinaMapOption" autoresize class="china-map" />
</div> </div>
<aside class="rank-panel"> <aside class="rank-panel">
<div class="section-head compact"> <div class="section-head compact">
<div class="section-title-group"> <div class="section-title-group">
<h4>热点</h4> <h4>热点来源</h4>
<p>按访问次数排序</p>
</div> </div>
</div> </div>
<div v-if="topRegions.length" class="region-list"> <div v-if="topRegions.length" class="region-list">
@@ -63,23 +83,65 @@
</aside> </aside>
</section> </section>
<el-dialog
v-model="fullscreenVisible"
class="source-map-dialog"
fullscreen
destroy-on-close
:show-close="false"
:close-on-click-modal="false"
append-to-body
@opened="resizeFullscreenChart"
>
<template #header>
<div class="fullscreen-map-header">
<div class="fullscreen-title-group">
<h3>来源分布全屏预览</h3>
<span>{{ currentPeriodLabel }}</span>
</div>
<div class="fullscreen-actions">
<el-segmented v-model="mapView" :options="mapViewOptions" size="small" />
<el-button size="small" @click="fullscreenVisible = false">退出全屏</el-button>
</div>
</div>
</template>
<div class="fullscreen-map-wrap">
<v-chart
ref="fullscreenChartRef"
:key="fullscreenMapKey"
:option="sourceMapOption"
:init-options="fullscreenChartInitOptions"
:update-options="chartUpdateOptions"
autoresize
class="fullscreen-source-map"
/>
<div class="flow-legend map-legend" aria-label="访问链路图例">
<span class="legend-item"><i class="legend-dot server"></i>服务器</span>
<span class="legend-item"><i class="legend-dot normal"></i>正常访问点</span>
<span class="legend-item"><i class="legend-dot abnormal"></i>异常访问点</span>
</div>
</div>
</el-dialog>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed, h, onMounted, ref } from "vue"; import { computed, h, nextTick, onMounted, ref } from "vue";
import VChart from "vue-echarts"; import VChart from "vue-echarts";
import { registerMap, use } from "echarts/core"; import { registerMap, use } from "echarts/core";
import { CanvasRenderer } from "echarts/renderers"; import { CanvasRenderer } from "echarts/renderers";
import { MapChart } from "echarts/charts"; import { EffectScatterChart, LinesChart } from "echarts/charts";
import { TooltipComponent, VisualMapComponent } from "echarts/components"; import { GeoComponent, TooltipComponent } from "echarts/components";
import chinaMapGeoJson from "../assets/china.json"; import chinaMapGeoJson from "../assets/china.json";
import worldMapGeoJson from "../assets/world.json";
import { fetchIpLocations } from "../api/projectPermissions"; import { fetchIpLocations } from "../api/projectPermissions";
import type { IpLocationsResponse, IpLocationStatItem } from "../types/api"; import type { IpLocationsResponse, IpLocationStatItem } from "../types/api";
import { normalizeProvinceName } from "./chinaProvinceMap"; import { resolveSourceCoordinate } from "./worldMapSource";
use([CanvasRenderer, MapChart, TooltipComponent, VisualMapComponent]); use([CanvasRenderer, LinesChart, EffectScatterChart, GeoComponent, TooltipComponent]);
registerMap("ctms-china", chinaMapGeoJson as any); registerMap("ctms-china", chinaMapGeoJson as any);
registerMap("ctms-world", worldMapGeoJson as any);
const IconGlobe = () => h("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", "stroke-width": "2" }, [ const IconGlobe = () => h("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", "stroke-width": "2" }, [
h("circle", { cx: "12", cy: "12", r: "10" }), h("circle", { cx: "12", cy: "12", r: "10" }),
@@ -103,9 +165,22 @@ const IconShield = () => h("svg", { viewBox: "0 0 24 24", fill: "none", stroke:
]); ]);
const loading = ref(false); const loading = ref(false);
const days = ref(7); const days = ref(90);
const mapView = ref<"china" | "flow">("china");
const items = ref<IpLocationStatItem[]>([]); const items = ref<IpLocationStatItem[]>([]);
const summary = ref<IpLocationsResponse["summary"] | null>(null); const summary = ref<IpLocationsResponse["summary"] | null>(null);
const getDevicePixelRatio = () => (typeof window === "undefined" ? 1 : window.devicePixelRatio || 1);
const chartInitOptions = { devicePixelRatio: Math.min(getDevicePixelRatio(), 2) };
const fullscreenChartInitOptions = { devicePixelRatio: Math.min(getDevicePixelRatio() * 1.5, 3) };
const chartUpdateOptions = { notMerge: true };
const fullscreenVisible = ref(false);
const sourceChartRef = ref<{ resize?: () => void } | null>(null);
const fullscreenChartRef = ref<{ resize?: () => void } | null>(null);
const serverLocation = {
name: "服务器所在地",
coord: [121.4737, 31.2304] as [number, number],
};
const periodLabels: Record<number, string> = { const periodLabels: Record<number, string> = {
1: "近 24 小时", 1: "近 24 小时",
@@ -115,11 +190,31 @@ const periodLabels: Record<number, string> = {
}; };
const currentPeriodLabel = computed(() => periodLabels[days.value] || `${days.value}`); const currentPeriodLabel = computed(() => periodLabels[days.value] || `${days.value}`);
const mapViewOptions = [
{ label: "中国", value: "china" },
{ label: "全球", value: "flow" },
];
const fullscreenMapKey = computed(() => `fullscreen-${mapView.value}-${fullscreenVisible.value ? "open" : "closed"}`);
const openFullscreenPreview = () => {
fullscreenVisible.value = true;
};
const resizeFullscreenChart = async () => {
await nextTick();
fullscreenChartRef.value?.resize?.();
};
const resizeSourceCharts = async () => {
await nextTick();
sourceChartRef.value?.resize?.();
fullscreenChartRef.value?.resize?.();
};
const formatNumber = (value: number) => new Intl.NumberFormat("zh-CN").format(value || 0); const formatNumber = (value: number) => new Intl.NumberFormat("zh-CN").format(value || 0);
const formatLocation = (row: IpLocationStatItem) => { const formatLocation = (row: IpLocationStatItem) => {
const parts = [row.province, row.city].filter(Boolean); const parts = [row.country && row.country !== "中国" ? row.country : "", row.province, row.city, row.isp].filter(Boolean);
return parts.length ? parts.join(" / ") : row.location; return parts.length ? parts.join(" / ") : row.location;
}; };
@@ -166,95 +261,203 @@ const metricCards = computed(() => [
}, },
]); ]);
const provinceData = computed(() => { const rankedSourceRows = computed(() => [...items.value].sort((a, b) => b.total_count - a.total_count).slice(0, 10));
const provinceMap = new Map<string, IpLocationStatItem & { name: string }>();
items.value.forEach((item) => {
const name = normalizeProvinceName(item.province || item.city);
if (!name || item.country !== "中国") return;
const current = provinceMap.get(name);
if (!current) {
provinceMap.set(name, { ...item, name });
return;
}
current.total_count += item.total_count;
current.allowed_count += item.allowed_count;
current.denied_count += item.denied_count;
current.unique_ip_count += item.unique_ip_count;
current.unique_user_count += item.unique_user_count || 0;
});
return Array.from(provinceMap.values());
});
const maxValue = computed(() => { const isChinaSource = (item: IpLocationStatItem) => item.location === "局域网" || item.country === "中国";
const values = provinceData.value.map((item) => item.total_count);
return values.length ? Math.max(...values) : 100;
});
const chinaMapOption = computed(() => ({ const chinaFlowSourceData = computed(() => rankedSourceRows.value.filter((item) => isChinaSource(item) && resolveSourceCoordinate(item)));
tooltip: {
trigger: "item", const flowSourceData = computed(() => rankedSourceRows.value.filter((item) => resolveSourceCoordinate(item)));
formatter: (params: any) => {
const data = params.data; const buildFlowMapOption = (data: IpLocationStatItem[], mapName = "ctms-world", zoom = 1.2, seriesName = "全球访问链路") => {
if (!data) return `${params.name}<br/>暂无访问数据`; const points = data
return [ .map((item) => ({
`${params.name}`, ...item,
`访问次数:${formatNumber(data.value)}`, name: formatLocation(item),
`访问用户数:${formatNumber(data.unique_user_count || 0)}`, coord: resolveSourceCoordinate(item),
`来源 IP 数:${formatNumber(data.unique_ip_count || 0)}`, abnormal: Boolean(item.country && item.country !== "中国"),
`拒绝次数:${formatNumber(data.denied_count || 0)}`, }))
].join("<br/>"); .filter((item): item is IpLocationStatItem & { name: string; coord: [number, number]; abnormal: boolean } => Boolean(item.coord));
const formatLineTooltip = (params: any) => {
const row = params.data;
if (!row) return "";
return [
`${row.fromName}${row.toName}`,
`访问次数:${formatNumber(row.value || 0)}`,
row.abnormal ? "事件类型:异常 IP" : "事件类型:正常访问",
].join("<br/>");
};
const formatPointTooltip = (params: any) => {
const row = params.data;
if (!row) return "";
if (row.isServer) return "中国 / 上海";
return [
`${row.name}`,
`访问次数:${formatNumber(row.total_count || 0)}`,
`访问用户数:${formatNumber(row.unique_user_count || 0)}`,
`来源 IP 数:${formatNumber(row.unique_ip_count || 0)}`,
`拒绝次数:${formatNumber(row.denied_count || 0)}`,
row.abnormal ? "事件类型:异常 IP" : "事件类型:正常访问",
].join("<br/>");
};
const formatGeoTooltip = (params: any) => {
const name = String(params?.name || "").trim();
return name || "暂无访问数据";
};
return {
tooltip: {
trigger: "item",
confine: true,
formatter: formatGeoTooltip,
}, },
}, geo: {
visualMap: { map: mapName,
min: 0,
max: maxValue.value,
left: "left",
bottom: "20",
text: ["高", "低"],
inRange: {
color: ["#edf3ff", "#a5c4fd", "#6399f7", "#3b82f6", "#1d4ed8"],
},
show: true,
calculable: false,
},
series: [
{
name: "用户分布图",
type: "map",
map: "ctms-china",
roam: false, roam: false,
zoom: 1.08, tooltip: {
show: true,
formatter: formatGeoTooltip,
},
zoom,
label: { show: false }, label: { show: false },
itemStyle: { itemStyle: {
areaColor: "#edf3ff", areaColor: "#e6edf8",
borderColor: "#ffffff", borderColor: "#aebed2",
borderWidth: 1, borderWidth: 1,
shadowColor: "rgba(37, 99, 235, 0.08)",
shadowBlur: 10,
shadowOffsetY: 2,
}, },
emphasis: { emphasis: {
label: { show: true, color: "#0f172a", fontSize: 12, fontWeight: 700 }, label: { show: false },
itemStyle: { areaColor: "#ffb86b", shadowBlur: 12, shadowColor: "rgba(245, 158, 11, 0.28)" }, itemStyle: { areaColor: "#dbeafe", borderColor: "#64748b", borderWidth: 1.3 },
}, },
data: provinceData.value.map((item) => ({
name: item.name,
value: item.total_count,
unique_user_count: item.unique_user_count,
unique_ip_count: item.unique_ip_count,
denied_count: item.denied_count,
})),
}, },
], series: [
})); {
name: seriesName,
type: "lines",
coordinateSystem: "geo",
zlevel: 2,
tooltip: {
show: true,
formatter: formatLineTooltip,
},
effect: {
show: true,
period: 4,
trailLength: 0.18,
symbol: "arrow",
symbolSize: 6,
},
lineStyle: {
width: 1,
opacity: 0.28,
curveness: 0.2,
},
data: points
.filter((point) => point.name !== serverLocation.name)
.map((point) => {
const lineWidth = point.abnormal ? 1.1 : 0.8;
return {
fromName: point.name,
toName: serverLocation.name,
value: point.total_count,
abnormal: point.abnormal,
coords: [point.coord, serverLocation.coord],
lineStyle: {
color: point.abnormal ? "#f97316" : "#2563eb",
width: lineWidth,
opacity: point.abnormal ? 0.58 : 0.38,
},
};
}),
},
{
name: "访问来源",
type: "effectScatter",
coordinateSystem: "geo",
zlevel: 3,
tooltip: {
show: true,
formatter: formatPointTooltip,
},
rippleEffect: {
brushType: "stroke",
scale: 1.7,
},
symbolSize: (value: number[], params: any) => {
const isDomestic = !params?.data?.abnormal;
const maxSize = isDomestic ? 12 : 16;
const minSize = isDomestic ? 5 : 7;
return Math.min(maxSize, Math.max(minSize, Math.sqrt(value[2] || 1) * 2));
},
itemStyle: {
color: (params: any) => (params.data.abnormal ? "#f97316" : "#2563eb"),
shadowBlur: 6,
shadowColor: "rgba(37, 99, 235, 0.2)",
},
data: points.map((point) => ({
name: formatLocation(point),
value: [...point.coord, point.total_count],
total_count: point.total_count,
unique_user_count: point.unique_user_count,
unique_ip_count: point.unique_ip_count,
denied_count: point.denied_count,
abnormal: point.abnormal,
})),
},
{
name: serverLocation.name,
type: "effectScatter",
coordinateSystem: "geo",
zlevel: 4,
tooltip: {
show: true,
formatter: formatPointTooltip,
},
rippleEffect: {
brushType: "fill",
scale: 1.5,
},
symbolSize: 9,
itemStyle: {
color: "#16a34a",
shadowBlur: 6,
shadowColor: "rgba(22, 163, 74, 0.24)",
},
data: [
{
name: serverLocation.name,
value: [...serverLocation.coord, 1],
isServer: true,
},
],
},
],
};
};
const sourceMapOption = computed(() =>
mapView.value === "flow"
? buildFlowMapOption(flowSourceData.value)
: buildFlowMapOption(chinaFlowSourceData.value, "ctms-china", 1.08, "中国访问链路"),
);
const visibleRankRows = computed(() =>
mapView.value === "china" ? chinaFlowSourceData.value : mapView.value === "flow" ? flowSourceData.value : rankedSourceRows.value,
);
const topRegions = computed(() => const topRegions = computed(() =>
[...items.value] visibleRankRows.value.map((item, index) => ({
.sort((a, b) => b.total_count - a.total_count) key: `${item.country}-${item.province}-${item.city}-${item.isp}`,
.slice(0, 6) rank: String(index + 1).padStart(2, "0"),
.map((item, index) => ({ name: formatLocation(item),
key: `${item.country}-${item.province}-${item.city}-${item.isp}`, total_count: formatNumber(item.total_count),
rank: String(index + 1).padStart(2, "0"), })),
name: formatLocation(item),
total_count: formatNumber(item.total_count),
})),
); );
const loadData = async () => { const loadData = async () => {
@@ -271,9 +474,12 @@ const loadData = async () => {
} }
}; };
onMounted(loadData); onMounted(async () => {
await loadData();
await resizeSourceCharts();
});
defineExpose({ refresh: loadData }); defineExpose({ refresh: loadData, resize: resizeSourceCharts });
</script> </script>
<style scoped> <style scoped>
@@ -291,6 +497,8 @@ defineExpose({ refresh: loadData });
--ip-shadow: 0 1px 3px rgba(0, 0, 0, 0.04), 0 4px 12px rgba(0, 0, 0, 0.03); --ip-shadow: 0 1px 3px rgba(0, 0, 0, 0.04), 0 4px 12px rgba(0, 0, 0, 0.03);
display: flex; display: flex;
flex-direction: column; flex-direction: column;
height: 100%;
min-height: 0;
gap: 14px; gap: 14px;
} }
@@ -305,7 +513,7 @@ defineExpose({ refresh: loadData });
.ip-locations-toolbar { .ip-locations-toolbar {
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: flex-start; justify-content: space-between;
gap: 12px; gap: 12px;
flex-wrap: wrap; flex-wrap: wrap;
} }
@@ -330,7 +538,9 @@ defineExpose({ refresh: loadData });
.toolbar-actions { .toolbar-actions {
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: flex-end;
gap: 10px; gap: 10px;
margin-left: auto;
} }
.metric-grid { .metric-grid {
@@ -357,8 +567,8 @@ defineExpose({ refresh: loadData });
.metric-card::after { .metric-card::after {
content: ""; content: "";
position: absolute; position: absolute;
right: -46px; right: -34px;
bottom: -52px; bottom: -42px;
width: 120px; width: 120px;
height: 120px; height: 120px;
border-radius: 18px; border-radius: 18px;
@@ -432,7 +642,10 @@ defineExpose({ refresh: loadData });
.distribution-card { .distribution-card {
display: grid; display: grid;
grid-template-columns: minmax(0, 1fr) 320px; grid-template-columns: minmax(0, 1fr) 320px;
grid-template-rows: minmax(0, 1fr);
gap: 14px; gap: 14px;
flex: 1 1 auto;
min-height: 0;
background: var(--ip-card); background: var(--ip-card);
border: 1px solid var(--ip-border); border: 1px solid var(--ip-border);
border-radius: var(--ip-radius); border-radius: var(--ip-radius);
@@ -443,12 +656,20 @@ defineExpose({ refresh: loadData });
.map-panel, .map-panel,
.rank-panel { .rank-panel {
min-width: 0; min-width: 0;
min-height: 0;
border: 1px solid #f1f5f9; border: 1px solid #f1f5f9;
border-radius: 14px; border-radius: 14px;
background: linear-gradient(180deg, #fafcff 0%, #ffffff 100%); background: linear-gradient(180deg, #fafcff 0%, #ffffff 100%);
padding: 16px; padding: 16px;
} }
.map-panel,
.rank-panel,
.source-map-wrap {
display: flex;
flex-direction: column;
}
.section-head { .section-head {
display: flex; display: flex;
align-items: flex-start; align-items: flex-start;
@@ -461,6 +682,72 @@ defineExpose({ refresh: loadData });
margin-bottom: 16px; margin-bottom: 16px;
} }
.map-head-actions {
display: flex;
align-items: center;
gap: 10px;
flex-wrap: wrap;
justify-content: flex-end;
max-width: 72%;
}
.map-fullscreen-button svg {
width: 14px;
height: 14px;
}
.flow-legend {
display: flex;
align-items: center;
justify-content: flex-start;
gap: 12px;
color: var(--ip-muted);
font-size: 12px;
line-height: 1;
}
.map-legend {
position: absolute;
left: 14px;
bottom: 14px;
z-index: 5;
padding: 9px 12px;
border: 1px solid rgba(226, 232, 240, 0.92);
border-radius: 999px;
background: rgba(255, 255, 255, 0.92);
box-shadow: 0 8px 22px rgba(15, 23, 42, 0.08);
backdrop-filter: blur(8px);
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 6px;
white-space: nowrap;
}
.legend-dot {
display: inline-block;
width: 8px;
height: 8px;
border-radius: 999px;
}
.legend-dot.server {
background: #16a34a;
box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.12);
}
.legend-dot.normal {
background: #2563eb;
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}
.legend-dot.abnormal {
background: #f97316;
box-shadow: 0 0 0 3px rgba(249, 115, 22, 0.14);
}
.section-title-group h4 { .section-title-group h4 {
margin: 0; margin: 0;
color: var(--ip-ink); color: var(--ip-ink);
@@ -474,9 +761,89 @@ defineExpose({ refresh: loadData });
font-size: 12px; font-size: 12px;
} }
.china-map { .source-map-wrap,
.fullscreen-map-wrap {
position: relative;
min-width: 0;
min-height: 0;
flex: 1;
}
.source-map {
width: 100%; width: 100%;
height: 430px; height: 100%;
min-height: 0;
}
.rank-panel {
height: 100%;
overflow: hidden;
}
.region-list {
flex: 1;
min-width: 0;
overflow-x: hidden;
overflow-y: auto;
}
:global(.source-map-dialog) {
--el-dialog-padding-primary: 0;
}
:global(.source-map-dialog .el-dialog__header) {
padding: 0;
margin: 0;
}
:global(.source-map-dialog .el-dialog__body) {
height: calc(100vh - 74px);
padding: 0 22px 22px;
background: #f8fafc;
}
.fullscreen-map-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
min-height: 74px;
padding: 16px 22px;
border-bottom: 1px solid var(--ip-border);
background: #ffffff;
}
.fullscreen-title-group {
display: flex;
flex-direction: column;
gap: 4px;
min-width: 0;
}
.fullscreen-title-group h3 {
margin: 0;
color: var(--ip-ink);
font-size: 16px;
font-weight: 700;
}
.fullscreen-title-group span {
color: var(--ip-muted);
font-size: 12px;
}
.fullscreen-actions {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 12px;
flex-wrap: wrap;
}
.fullscreen-source-map {
width: 100%;
height: calc(100vh - 96px);
min-height: 520px;
} }
.region-list { .region-list {
@@ -486,12 +853,15 @@ defineExpose({ refresh: loadData });
} }
.region-row { .region-row {
box-sizing: border-box;
display: grid; display: grid;
grid-template-columns: 36px minmax(0, 1fr) auto; grid-template-columns: 36px minmax(0, 1fr) minmax(72px, max-content);
gap: 10px; gap: 10px;
align-items: center; align-items: center;
min-width: 0;
width: 100%; width: 100%;
padding: 12px; max-width: 100%;
padding: 12px 10px;
border-radius: 10px; border-radius: 10px;
background: #fff; background: #fff;
border: 1px solid #f1f5f9; border: 1px solid #f1f5f9;
@@ -515,6 +885,7 @@ defineExpose({ refresh: loadData });
color: #94a3b8; color: #94a3b8;
font-size: 12px; font-size: 12px;
font-weight: 700; font-weight: 700;
min-width: 0;
} }
.rank.top { .rank.top {
@@ -523,6 +894,7 @@ defineExpose({ refresh: loadData });
} }
.region-name { .region-name {
min-width: 0;
overflow: hidden; overflow: hidden;
font-size: 13px; font-size: 13px;
font-weight: 600; font-weight: 600;
@@ -531,9 +903,14 @@ defineExpose({ refresh: loadData });
} }
.region-value { .region-value {
min-width: 0;
overflow: hidden;
color: var(--ip-blue); color: var(--ip-blue);
font-size: 15px; font-size: 15px;
font-weight: 700; font-weight: 700;
text-align: right;
text-overflow: ellipsis;
white-space: nowrap;
} }
@media (max-width: 1100px) { @media (max-width: 1100px) {
@@ -544,20 +921,72 @@ defineExpose({ refresh: loadData });
.distribution-card { .distribution-card {
grid-template-columns: 1fr; grid-template-columns: 1fr;
} }
.map-panel,
.rank-panel {
min-height: 0;
}
.rank-panel {
max-height: none;
}
} }
@media (max-width: 720px) { @media (max-width: 720px) {
.ip-locations-toolbar { .ip-locations-toolbar {
flex-direction: column; flex-direction: column;
align-items: flex-start; align-items: stretch;
}
.toolbar-actions {
justify-content: flex-end;
width: 100%;
margin-left: 0;
flex-wrap: wrap;
} }
.metric-grid { .metric-grid {
grid-template-columns: 1fr; grid-template-columns: 1fr;
} }
.china-map { .source-map {
height: 320px; height: 100%;
min-height: 0;
}
.map-head-actions {
justify-content: flex-start;
max-width: none;
}
:global(.source-map-dialog .el-dialog__body) {
height: calc(100vh - 126px);
padding: 0 12px 12px;
}
.fullscreen-map-header {
align-items: flex-start;
flex-direction: column;
gap: 10px;
min-height: 126px;
padding: 12px;
}
.fullscreen-actions {
justify-content: flex-start;
}
.fullscreen-source-map {
height: calc(100vh - 144px);
min-height: 360px;
}
.map-legend {
left: 10px;
right: 10px;
bottom: 10px;
justify-content: center;
border-radius: 12px;
} }
} }
</style> </style>
@@ -48,6 +48,7 @@ describe("PermissionMonitoring.vue", () => {
PermissionTrendCharts: true, PermissionTrendCharts: true,
PermissionAccessLogs: true, PermissionAccessLogs: true,
PermissionIpLocations: true, PermissionIpLocations: true,
SecurityCenter: true,
}, },
}, },
}); });
@@ -64,6 +65,7 @@ describe("PermissionMonitoring.vue", () => {
PermissionTrendCharts: true, PermissionTrendCharts: true,
PermissionAccessLogs: true, PermissionAccessLogs: true,
PermissionIpLocations: true, PermissionIpLocations: true,
SecurityCenter: true,
}, },
}, },
}); });
@@ -77,4 +79,53 @@ describe("PermissionMonitoring.vue", () => {
expect(source).toContain("isAdmin?: boolean"); expect(source).toContain("isAdmin?: boolean");
expect(source).toContain(':show-security-log="props.isAdmin"'); expect(source).toContain(':show-security-log="props.isAdmin"');
}); });
it("uses a full-height source-analysis tab without forcing scroll on other tabs", () => {
const source = readFileSync(resolve(__dirname, "./PermissionMonitoring.vue"), "utf8");
expect(source).toContain(":class=\"{ 'is-source-tab': activeTab === 'ip-locations' }\"");
expect(source).toContain('class="ip-locations-pane"');
expect(source).toContain(".permission-monitoring.is-source-tab .soybean-monitoring-tabs :deep(.el-tabs__content)");
expect(source).toContain("overflow: hidden");
expect(source).toContain(".soybean-monitoring-tabs :deep(.el-tabs__content)");
expect(source).toContain("overflow: auto");
});
it("resizes the source-analysis chart when its tab becomes active", () => {
const source = readFileSync(resolve(__dirname, "./PermissionMonitoring.vue"), "utf8");
expect(source).toContain('import { ref, computed, h, nextTick, onMounted } from "vue"');
expect(source).toContain("type IpLocationsExpose");
expect(source).toContain("resize: () => void | Promise<void>");
expect(source).toContain('if (tab === "ip-locations")');
expect(source).toContain("await nextTick()");
expect(source).toContain("await ipLocationsRef.value?.resize()");
});
it("uses system monitoring tab names and operation-oriented overview copy", () => {
const source = readFileSync(resolve(__dirname, "./PermissionMonitoring.vue"), "utf8");
expect(source).toContain('label="运行概览"');
expect(source).toContain('label="安全中心"');
expect(source).toContain('label="性能趋势"');
expect(source).toContain('label="访问审计"');
expect(source).toContain('label="来源分析"');
expect(source).toContain('<SecurityCenter v-if="props.isAdmin"');
expect(source).toContain('name="security"');
expect(source).toContain('label: "今日监测事件"');
expect(source).toContain('label: "每分钟事件"');
expect(source).toContain('label: "今日通过率"');
expect(source).toContain('label: "历史事件总数"');
expect(source).toContain("异常拒绝排行");
expect(source).not.toContain('label="实时概览"');
expect(source).not.toContain('label="趋势分析"');
expect(source).not.toContain('label="访问日志"');
expect(source).not.toContain('label="IP属地"');
expect(source).not.toContain('label: "今日总检查"');
expect(source).not.toContain('label: "每分钟请求"');
expect(source).not.toContain('label: "今日允许率"');
expect(source).not.toContain('label: "历史总记录"');
expect(source).not.toContain("被拒绝最多的权限");
});
}); });
+199 -15
View File
@@ -1,7 +1,13 @@
<template> <template>
<div class="permission-monitoring"> <div class="permission-monitoring" :class="{ 'is-source-tab': activeTab === 'ip-locations' }">
<el-tabs v-model="activeTab" @tab-change="onTabChange"> <el-tabs v-model="activeTab" class="soybean-monitoring-tabs" @tab-change="onTabChange">
<el-tab-pane label="实时概览" name="overview"> <el-tab-pane label="运行概览" name="overview">
<template #label>
<span class="soybean-tab-label">
<component :is="IconCheck" />
<span>运行概览</span>
</span>
</template>
<div v-if="statsSummary" class="stats-summary"> <div v-if="statsSummary" class="stats-summary">
<div v-for="card in summaryCards" :key="card.label" class="summary-card" :class="card.tone"> <div v-for="card in summaryCards" :key="card.label" class="summary-card" :class="card.tone">
<div class="summary-icon"> <div class="summary-icon">
@@ -83,7 +89,7 @@
<div v-if="topDenied && topDenied.length > 0" class="denied-card"> <div v-if="topDenied && topDenied.length > 0" class="denied-card">
<div class="card-header"> <div class="card-header">
<h3>被拒绝最多的权限</h3> <h3>异常拒绝排行</h3>
<el-tag effect="plain" size="small" type="info"> 7 </el-tag> <el-tag effect="plain" size="small" type="info"> 7 </el-tag>
</div> </div>
<div class="denied-list"> <div class="denied-list">
@@ -121,23 +127,53 @@
</div> </div>
</el-tab-pane> </el-tab-pane>
<el-tab-pane label="趋势分析" name="trends"> <el-tab-pane label="性能趋势" name="trends">
<template #label>
<span class="soybean-tab-label">
<component :is="IconActivity" />
<span>性能趋势</span>
</span>
</template>
<PermissionTrendCharts ref="trendChartsRef" /> <PermissionTrendCharts ref="trendChartsRef" />
</el-tab-pane> </el-tab-pane>
<el-tab-pane label="访问日志" name="logs"> <el-tab-pane v-if="props.isAdmin" label="安全中心" name="security">
<template #label>
<span class="soybean-tab-label">
<component :is="IconShield" />
<span>安全中心</span>
</span>
</template>
<SecurityCenter v-if="props.isAdmin" ref="securityCenterRef" />
</el-tab-pane>
<el-tab-pane label="访问审计" name="logs">
<template #label>
<span class="soybean-tab-label">
<component :is="IconDatabase" />
<span>访问审计</span>
</span>
</template>
<PermissionAccessLogs ref="accessLogsRef" :show-security-log="props.isAdmin" /> <PermissionAccessLogs ref="accessLogsRef" :show-security-log="props.isAdmin" />
</el-tab-pane> </el-tab-pane>
<el-tab-pane label="IP属地" name="ip-locations"> <el-tab-pane label="来源分析" name="ip-locations">
<PermissionIpLocations ref="ipLocationsRef" /> <template #label>
<span class="soybean-tab-label">
<component :is="IconPercent" />
<span>来源分析</span>
</span>
</template>
<div class="ip-locations-pane">
<PermissionIpLocations ref="ipLocationsRef" />
</div>
</el-tab-pane> </el-tab-pane>
</el-tabs> </el-tabs>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, h, onMounted } from "vue"; import { ref, computed, h, nextTick, onMounted } from "vue";
import type { import type {
PermissionMetricsResponse, PermissionMetricsResponse,
AlertsResponse, AlertsResponse,
@@ -155,6 +191,7 @@ import {
import PermissionTrendCharts from "./PermissionTrendCharts.vue"; import PermissionTrendCharts from "./PermissionTrendCharts.vue";
import PermissionAccessLogs from "./PermissionAccessLogs.vue"; import PermissionAccessLogs from "./PermissionAccessLogs.vue";
import PermissionIpLocations from "./PermissionIpLocations.vue"; import PermissionIpLocations from "./PermissionIpLocations.vue";
import SecurityCenter from "./SecurityCenter.vue";
const IconCheck = () => h("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", "stroke-width": "2" }, [ const IconCheck = () => h("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", "stroke-width": "2" }, [
h("polyline", { points: "20 6 9 17 4 12" }), h("polyline", { points: "20 6 9 17 4 12" }),
@@ -176,6 +213,9 @@ const IconDatabase = () => h("svg", { viewBox: "0 0 24 24", fill: "none", stroke
h("path", { d: "M21 12c0 1.66-4 3-9 3s-9-1.34-9-3" }), h("path", { d: "M21 12c0 1.66-4 3-9 3s-9-1.34-9-3" }),
h("path", { d: "M3 5v14c0 1.66 4 3 9 3s9-1.34 9-3V5" }), h("path", { d: "M3 5v14c0 1.66 4 3 9 3s9-1.34 9-3V5" }),
]); ]);
const IconShield = () => h("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", "stroke-width": "2" }, [
h("path", { d: "M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z" }),
]);
const activeTab = ref("overview"); const activeTab = ref("overview");
const props = withDefaults(defineProps<{ isAdmin?: boolean }>(), { const props = withDefaults(defineProps<{ isAdmin?: boolean }>(), {
@@ -189,7 +229,13 @@ const statsSummary = ref<StatsSummaryResponse | null>(null);
const trendChartsRef = ref<InstanceType<typeof PermissionTrendCharts>>(); const trendChartsRef = ref<InstanceType<typeof PermissionTrendCharts>>();
const accessLogsRef = ref<InstanceType<typeof PermissionAccessLogs>>(); const accessLogsRef = ref<InstanceType<typeof PermissionAccessLogs>>();
const ipLocationsRef = ref<InstanceType<typeof PermissionIpLocations>>(); type IpLocationsExpose = {
refresh: () => void | Promise<void>;
resize: () => void | Promise<void>;
};
const ipLocationsRef = ref<IpLocationsExpose>();
const securityCenterRef = ref<InstanceType<typeof SecurityCenter>>();
const formatTime = (timestamp: number): string => { const formatTime = (timestamp: number): string => {
return new Date(timestamp * 1000).toLocaleString("zh-CN"); return new Date(timestamp * 1000).toLocaleString("zh-CN");
@@ -226,19 +272,19 @@ const summaryCards = computed(() => {
if (!statsSummary.value) return []; if (!statsSummary.value) return [];
return [ return [
{ {
label: "今日总检查", label: "今日监测事件",
value: statsSummary.value.today.total_checks.toLocaleString(), value: statsSummary.value.today.total_checks.toLocaleString(),
tone: "blue", tone: "blue",
icon: IconCheck, icon: IconCheck,
}, },
{ {
label: "每分钟请求", label: "每分钟事件",
value: statsSummary.value.last_hour.requests_per_minute, value: statsSummary.value.last_hour.requests_per_minute,
tone: "cyan", tone: "cyan",
icon: IconActivity, icon: IconActivity,
}, },
{ {
label: "今日允许率", label: "今日通过率",
value: `${statsSummary.value.today.allow_rate}%`, value: `${statsSummary.value.today.allow_rate}%`,
tone: statsSummary.value.today.allow_rate >= 80 ? "green" : "warning", tone: statsSummary.value.today.allow_rate >= 80 ? "green" : "warning",
icon: IconPercent, icon: IconPercent,
@@ -250,7 +296,7 @@ const summaryCards = computed(() => {
icon: IconClock, icon: IconClock,
}, },
{ {
label: "历史总记录", label: "历史事件总数",
value: statsSummary.value.total_logs.toLocaleString(), value: statsSummary.value.total_logs.toLocaleString(),
tone: "violet", tone: "violet",
icon: IconDatabase, icon: IconDatabase,
@@ -273,13 +319,18 @@ const loadOverviewData = async () => {
if (summaryRes.status === "fulfilled") statsSummary.value = summaryRes.value.data; if (summaryRes.status === "fulfilled") statsSummary.value = summaryRes.value.data;
}; };
const onTabChange = (tab: string) => { const onTabChange = async (tab: string) => {
if (tab === "overview") loadOverviewData(); if (tab === "overview") loadOverviewData();
if (tab === "ip-locations") {
await nextTick();
await ipLocationsRef.value?.resize();
}
}; };
const refresh = () => { const refresh = () => {
if (activeTab.value === "overview") loadOverviewData(); if (activeTab.value === "overview") loadOverviewData();
else if (activeTab.value === "trends") trendChartsRef.value?.refresh(); else if (activeTab.value === "trends") trendChartsRef.value?.refresh();
else if (activeTab.value === "security") securityCenterRef.value?.refresh();
else if (activeTab.value === "logs") accessLogsRef.value?.refresh(); else if (activeTab.value === "logs") accessLogsRef.value?.refresh();
else if (activeTab.value === "ip-locations") ipLocationsRef.value?.refresh(); else if (activeTab.value === "ip-locations") ipLocationsRef.value?.refresh();
}; };
@@ -294,11 +345,144 @@ defineExpose({ refresh });
--mon-ink: #1a2332; --mon-ink: #1a2332;
--mon-muted: #64748b; --mon-muted: #64748b;
--mon-border: #e2e8f0; --mon-border: #e2e8f0;
--mon-tab-active: #6c63ff;
--mon-tab-active-bg: #f0edff;
--mon-radius: 16px; --mon-radius: 16px;
--mon-shadow: 0 1px 3px rgba(0, 0, 0, 0.04), 0 4px 12px rgba(0, 0, 0, 0.03); --mon-shadow: 0 1px 3px rgba(0, 0, 0, 0.04), 0 4px 12px rgba(0, 0, 0, 0.03);
display: flex;
flex-direction: column;
height: 100%;
min-height: 0;
padding: 0; padding: 0;
} }
.soybean-monitoring-tabs {
display: flex;
flex: 1 1 auto;
flex-direction: column;
min-height: 0;
}
.soybean-monitoring-tabs :deep(.el-tabs__header) {
height: 56px;
margin: 0 0 16px;
padding: 0 24px;
background: #ffffff;
border-top: 1px solid var(--mon-border);
border-bottom: 1px solid var(--mon-border);
box-shadow: 0 1px 2px rgba(15, 23, 42, 0.05);
}
.soybean-monitoring-tabs :deep(.el-tabs__nav-wrap) {
height: 56px;
}
.soybean-monitoring-tabs :deep(.el-tabs__nav-wrap::after),
.soybean-monitoring-tabs :deep(.el-tabs__active-bar) {
display: none;
}
.soybean-monitoring-tabs :deep(.el-tabs__nav-scroll),
.soybean-monitoring-tabs :deep(.el-tabs__nav) {
height: 100%;
}
.soybean-monitoring-tabs :deep(.el-tabs__nav) {
display: flex;
align-items: center;
}
.soybean-monitoring-tabs :deep(.el-tabs__item) {
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
height: 36px;
margin: 0 4px;
padding: 0 18px;
border-radius: 10px;
color: #303133;
font-size: 15px;
font-weight: 600;
line-height: 1;
transition: color 0.2s ease, background 0.2s ease;
}
:deep(.soybean-monitoring-tabs.el-tabs--top > .el-tabs__header .el-tabs__item:nth-child(2)) {
padding-left: 18px;
}
:deep(.soybean-monitoring-tabs.el-tabs--top > .el-tabs__header .el-tabs__item:last-child) {
padding-right: 18px;
}
.soybean-monitoring-tabs :deep(.el-tabs__item:not(.is-active)::after) {
content: "";
position: absolute;
top: 10px;
right: -5px;
width: 1px;
height: 16px;
background: #dcdfe6;
}
.soybean-monitoring-tabs :deep(.el-tabs__item:last-child::after),
.soybean-monitoring-tabs :deep(.el-tabs__item.is-active + .el-tabs__item::after) {
display: none;
}
.soybean-monitoring-tabs :deep(.el-tabs__item:hover) {
color: var(--mon-tab-active);
}
.soybean-monitoring-tabs :deep(.el-tabs__item.is-active) {
background: var(--mon-tab-active-bg);
color: var(--mon-tab-active);
}
.soybean-monitoring-tabs :deep(.el-tabs__content) {
flex: 1 1 auto;
min-height: 0;
padding: 0;
overflow: auto;
}
.permission-monitoring.is-source-tab .soybean-monitoring-tabs :deep(.el-tabs__content) {
overflow: hidden;
}
.soybean-monitoring-tabs :deep(.el-tab-pane) {
min-height: 100%;
}
.soybean-monitoring-tabs :deep(.el-tab-pane[aria-hidden="false"]) {
height: 100%;
min-height: 0;
}
.ip-locations-pane {
height: 100%;
min-height: 0;
overflow: hidden;
}
.soybean-tab-label {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
width: 100%;
min-width: 0;
line-height: 1;
}
.soybean-tab-label svg {
width: 16px;
height: 16px;
flex: 0 0 auto;
}
.stats-summary { .stats-summary {
display: grid; display: grid;
grid-template-columns: repeat(5, minmax(0, 1fr)); grid-template-columns: repeat(5, minmax(0, 1fr));
@@ -1,52 +0,0 @@
import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
const readSource = () => readFileSync(resolve(__dirname, "./PermissionTemplateSelector.vue"), "utf8");
describe("PermissionTemplateSelector", () => {
it("renders compact role permission cards without footer placeholder space", () => {
const source = readSource();
expect(source).toContain("角色权限概览");
expect(source).toContain("align-items: start");
expect(source).toContain("padding: 12px 14px");
expect(source).not.toContain("card-footer");
expect(source).not.toContain("点击编辑权限");
expect(source).not.toContain("inactive-label");
expect(source).not.toContain("edit-hint");
});
it("reads current project permissions by role key, not by role label", () => {
const source = readSource();
expect(source).toContain('v-for="template in sortedTemplates"');
expect(source).toContain('<span class="card-name">{{ template.name }}</span>');
expect(source).toContain("refreshKey?: number;");
expect(source).toContain("watch(() => props.refreshKey, loadTemplates);");
expect(source).not.toContain("ROLE_LABELS");
expect(source).toContain("template.category && props.currentPermissions[template.category]");
expect(source).toContain("enabledPercent(template.category)");
expect(source).toContain("countCurrentEnabled(template.category)");
expect(source).toContain("countCurrentDisabled(template.category)");
expect(source).toContain("const perms = props.currentPermissions?.[role];");
expect(source).toContain("emit('edit-role', template.category!)");
});
it("sorts role cards with the shared role template order", () => {
const source = readSource();
expect(source).toContain("useRoleTemplateMeta");
expect(source).toContain("compareRolesByTemplateOrder");
expect(source).toContain("const sortedTemplates = computed");
expect(source).toContain("compareRolesByTemplateOrder(a.category, b.category)");
});
it("does not keep hard-coded preset role labels or icons", () => {
const source = readSource();
expect(source).not.toContain('QA: "QA"');
expect(source).not.toContain('CTA: "CTA"');
expect(source).not.toContain('QA: "✅"');
});
});
@@ -2,8 +2,7 @@
<div class="trend-charts"> <div class="trend-charts">
<div class="trend-toolbar"> <div class="trend-toolbar">
<div class="toolbar-left"> <div class="toolbar-left">
<span class="toolbar-title">数据趋势</span> <span class="toolbar-title">系统性能趋势</span>
<span class="toolbar-desc">查看权限系统各项指标的变化趋势</span>
</div> </div>
<el-radio-group v-model="period" size="small" @change="loadData"> <el-radio-group v-model="period" size="small" @change="loadData">
<el-radio-button value="24h">24小时</el-radio-button> <el-radio-button value="24h">24小时</el-radio-button>
@@ -18,7 +17,7 @@
<div class="chart-icon blue"> <div class="chart-icon blue">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="22 12 18 12 15 21 9 3 6 12 2 12"/></svg> <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="22 12 18 12 15 21 9 3 6 12 2 12"/></svg>
</div> </div>
<h4>检查量趋势</h4> <h4>监测事件趋势</h4>
</div> </div>
<v-chart :option="checksChartOption" autoresize class="chart" /> <v-chart :option="checksChartOption" autoresize class="chart" />
</div> </div>
@@ -45,7 +44,7 @@
<div class="chart-icon danger"> <div class="chart-icon danger">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="4.93" y1="4.93" x2="19.07" y2="19.07"/></svg> <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="4.93" y1="4.93" x2="19.07" y2="19.07"/></svg>
</div> </div>
<h4>拒绝率趋势</h4> <h4>异常拒绝率趋势</h4>
</div> </div>
<v-chart :option="denyRateOption" autoresize class="chart" /> <v-chart :option="denyRateOption" autoresize class="chart" />
</div> </div>
@@ -181,7 +180,7 @@ const denyRateOption = computed(() => ({
yAxis: { type: "value", max: 100, name: "%", splitLine: { lineStyle: { color: "#f1f5f9" } }, axisLabel: { color: "#64748b" } }, yAxis: { type: "value", max: 100, name: "%", splitLine: { lineStyle: { color: "#f1f5f9" } }, axisLabel: { color: "#64748b" } },
series: [ series: [
{ {
name: "拒绝率", name: "异常拒绝率",
type: "line", type: "line",
smooth: true, smooth: true,
symbol: "circle", symbol: "circle",
@@ -1,20 +0,0 @@
import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
const readQuickActionsSource = () => readFileSync(resolve(__dirname, "./QuickActions.vue"), "utf8");
describe("QuickActions project permissions", () => {
it("filters quick action entries with the same route permission contract as sidebar and router", () => {
const source = readQuickActionsSource();
expect(source).toContain("useAuthStore");
expect(source).toContain("useStudyStore");
expect(source).toContain("getProjectRoutePermission");
expect(source).toContain("hasProjectPermission");
expect(source).toContain("isSystemAdmin");
expect(source).toContain("visibleActions");
expect(source).toContain("v-for=\"item in visibleActions\"");
expect(source).not.toContain("v-for=\"item in actions\"");
});
});
@@ -0,0 +1,98 @@
import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
const readSource = () => readFileSync(resolve(__dirname, "./SecurityCenter.vue"), "utf8");
describe("SecurityCenter", () => {
it("renders the security dashboard in the same audit-card style as access logs", () => {
const source = readSource();
expect(source).toContain("audit-filters");
expect(source).toContain("audit-metrics");
expect(source).toContain("audit-grid");
expect(source).toContain("metric-card");
expect(source).toContain("ranking-card");
expect(source).not.toContain("security-summary");
expect(source).not.toContain("security-card");
});
it("places filters inside the event details section instead of the page header", () => {
const source = readSource();
const tableIndex = source.indexOf('<section class="security-table-wrap">');
const filtersIndex = source.indexOf('<section class="audit-filters detail-filters">');
expect(tableIndex).toBeGreaterThan(-1);
expect(filtersIndex).toBeGreaterThan(tableIndex);
expect(source).not.toContain('<section class="audit-filters">\n <el-select');
});
it("surfaces abnormal IP location ranking, endpoint type distribution and risk level distribution", () => {
const source = readSource();
expect(source).toContain("异常排行");
expect(source).toContain("事件分布");
expect(source).toContain("访问的接口类型");
expect(source).toContain("风险等级");
expect(source).toContain("event-distribution-card");
expect(source).toContain("distribution-section");
expect(source).toContain(".event-distribution-card > .section-head");
expect(source).toContain("padding-top: 24px");
expect(source).toContain("ipRiskStats");
expect(source).toContain("endpointTypeStats");
expect(source).toContain("riskLevelStats");
expect(source).toContain("resolveEndpointType");
expect(source).toContain("formatIpLocation");
expect(source).toContain("fallbackIpLocation");
expect(source).toContain("row.ip_province");
expect(source).toContain("row.ip_city");
expect(source).toContain("row.ip_isp");
expect(source).toContain("item.location");
expect(source).toContain("categoryText");
expect(source).toContain("rank-main-line");
expect(source).toContain("rank-meta-line");
expect(source).toContain("lastSeenAt");
expect(source).toContain("最后访问");
expect(source).toContain("slice(0, 10)");
expect(source).not.toContain("公网位置待解析");
expect(source).not.toContain("异常IP(位置)排行");
expect(source).not.toContain("risk-level-card");
});
it("keeps detailed security events searchable and inspectable", () => {
const source = readSource();
expect(source).toContain("detailFilteredEvents");
expect(source).toContain("paginatedEvents");
expect(source).toContain("openDetail");
expect(source).toContain("安全事件明细");
expect(source).toContain('label="IP"');
expect(source).toContain('label="位置"');
expect(source).not.toContain('label="来源 IP"');
expect(source).not.toContain('label="IP来源"');
expect(source).toContain("搜索 IP、位置、账号、路径或 UA");
expect(source).toContain("loadSecurityEvents");
});
it("labels abnormal IP events and paginates details like account management", () => {
const source = readSource();
expect(source).toContain('{ label: "异常IP", value: "ABNORMAL_IP" }');
expect(source).toContain('v-model:current-page="page"');
expect(source).toContain('v-model:page-size="pageSize"');
expect(source).toContain(':page-sizes="[10, 20, 50]"');
expect(source).toContain('layout="prev, pager, next, sizes, total"');
});
it("calculates dashboard statistics from all events instead of detail filters or current page", () => {
const source = readSource();
expect(source).toContain("statsEvents");
expect(source).toContain("detailFilteredEvents");
expect(source).toContain("loadSecurityStats");
expect(source).toContain("SECURITY_STATS_PAGE_SIZE");
expect(source).toContain("statsEvents.value");
expect(source).not.toContain("const uniqueIpCount = new Set(events.value");
expect(source).not.toContain("filteredStatsEvents");
});
});
+971
View File
@@ -0,0 +1,971 @@
<template>
<div class="security-center">
<section class="audit-metrics">
<article v-for="card in metricCards" :key="card.label" class="metric-card" :class="card.tone">
<div class="metric-icon">
<component :is="card.icon" />
</div>
<div class="metric-body">
<span class="metric-label">{{ card.label }}</span>
<strong class="metric-value">{{ card.value }}</strong>
</div>
</article>
</section>
<section class="audit-grid">
<article class="ranking-card">
<div class="section-head">
<div class="section-title-group">
<h4>异常排行</h4>
</div>
<el-tag effect="plain" size="small" type="info">TOP {{ ipRiskStats.length }}</el-tag>
</div>
<div v-if="ipRiskStats.length" class="risk-rank-list">
<div v-for="(item, index) in ipRiskStats" :key="item.ip" class="risk-rank-row">
<span class="rank-no" :class="{ 'rank-top': index < 3 }">{{ String(index + 1).padStart(2, "0") }}</span>
<div class="rank-user">
<div class="rank-main-line">
<strong>{{ item.ip }}</strong>
<small>{{ item.location }}</small>
</div>
<div class="rank-meta-line">
<span>{{ item.categoryText }}</span>
<span>最后访问 {{ formatTime(item.lastSeenAt) }}</span>
</div>
</div>
<div class="rank-count">
<strong>{{ item.total }}</strong>
<small :class="{ 'denied-highlight': item.highRiskCount > 0 }">{{ item.highRiskCount }} 高危</small>
</div>
</div>
</div>
<el-empty v-else description="暂无异常数据" :image-size="72" />
</article>
<article class="ranking-card event-distribution-card">
<div class="section-head">
<div class="section-title-group">
<h4>事件分布</h4>
</div>
</div>
<div class="distribution-section">
<div class="distribution-section-head">
<strong>访问的接口类型</strong>
</div>
<div v-if="endpointTypeStats.length" class="distribution-list">
<div v-for="item in endpointTypeStats" :key="item.type" class="distribution-row">
<div class="distribution-info">
<strong>{{ item.type }}</strong>
<small>{{ item.samplePath }}</small>
</div>
<div class="distribution-meter">
<span :style="{ width: `${item.percent}%` }" />
</div>
<strong class="distribution-count">{{ item.total }}</strong>
</div>
</div>
<el-empty v-else description="暂无接口类型数据" :image-size="72" />
</div>
<div class="distribution-section compact-risk-section">
<div class="distribution-section-head">
<strong>风险等级</strong>
</div>
<div class="risk-level-list">
<div v-for="item in riskLevelStats" :key="item.value" class="risk-level-row">
<span class="risk-dot" :class="item.tone" />
<span>{{ item.label }}</span>
<div class="risk-level-meter">
<span :class="item.tone" :style="{ width: `${item.percent}%` }" />
</div>
<strong>{{ item.total }}</strong>
</div>
</div>
</div>
</article>
</section>
<section class="security-table-wrap">
<div class="section-head table-head">
<div class="section-title-group">
<h4>安全事件明细</h4>
</div>
<section class="audit-filters detail-filters">
<el-select v-model="severityFilter" placeholder="风险等级" clearable style="width: 130px" @change="onFilterChange">
<el-option v-for="item in severityOptions" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
<el-select v-model="categoryFilter" placeholder="事件分类" clearable style="width: 150px" @change="onFilterChange">
<el-option v-for="item in categoryOptions" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
<el-select v-model="authFilter" placeholder="认证状态" clearable style="width: 140px" @change="onFilterChange">
<el-option label="匿名" value="ANONYMOUS" />
<el-option label="无效令牌" value="INVALID_TOKEN" />
<el-option label="已认证" value="AUTHENTICATED" />
</el-select>
<el-input
v-model="keyword"
placeholder="搜索 IP、位置、账号、路径或 UA"
clearable
class="security-search"
:prefix-icon="SearchIcon"
/>
<el-button type="primary" :loading="loading" @click="loadSecurityEvents">刷新</el-button>
</section>
<el-tag effect="plain" size="small" type="info">{{ securityTotal }} </el-tag>
</div>
<el-table :data="paginatedEvents" v-loading="loading" class="security-table" table-layout="fixed">
<el-table-column label="级别" width="96">
<template #default="{ row }">
<el-tag :type="severityTagType(row.severity)" effect="dark" size="small">{{ severityLabel(row.severity) }}</el-tag>
</template>
</el-table-column>
<el-table-column label="分类" width="130">
<template #default="{ row }">
<span class="category-pill">{{ categoryLabel(row.category) }}</span>
</template>
</el-table-column>
<el-table-column label="时间" width="160">
<template #default="{ row }">{{ formatTime(row.created_at) }}</template>
</el-table-column>
<el-table-column label="IP" width="132">
<template #default="{ row }">{{ row.client_ip || "未知 IP" }}</template>
</el-table-column>
<el-table-column label="位置" width="180">
<template #default="{ row }">{{ formatIpLocation(row) }}</template>
</el-table-column>
<el-table-column label="账号" width="140" prop="account_label" />
<el-table-column label="请求" min-width="320">
<template #default="{ row }">
<div class="request-cell">
<strong>{{ row.method }} {{ row.status_code }} / {{ resolveEndpointType(row.path) }}</strong>
<span>{{ row.path }}</span>
</div>
</template>
</el-table-column>
<el-table-column label="耗时" width="86">
<template #default="{ row }">{{ row.elapsed_ms.toFixed(1) }}ms</template>
</el-table-column>
<el-table-column label="操作" width="90" align="center">
<template #default="{ row }">
<el-button link type="primary" @click="openDetail(row)">详情</el-button>
</template>
</el-table-column>
</el-table>
<div v-if="securityTotal > 0" class="pagination-wrap">
<el-pagination
v-model:current-page="page"
v-model:page-size="pageSize"
:page-sizes="[10, 20, 50]"
:total="securityTotal"
layout="prev, pager, next, sizes, total"
small
@size-change="onPageSizeChange"
/>
</div>
</section>
<el-drawer v-model="detailVisible" size="520px" direction="rtl" :show-close="false">
<template #header>
<div class="security-detail-head">
<strong>安全事件详情</strong>
<el-button size="small" @click="detailVisible = false">关闭</el-button>
</div>
</template>
<div v-if="selectedEvent" class="security-detail">
<div class="detail-grid">
<div><span>级别</span><strong>{{ severityLabel(selectedEvent.severity) }}</strong></div>
<div><span>分类</span><strong>{{ categoryLabel(selectedEvent.category) }}</strong></div>
<div><span>状态码</span><strong>{{ selectedEvent.status_code }}</strong></div>
<div><span>认证状态</span><strong>{{ authLabel(selectedEvent.auth_status) }}</strong></div>
<div><span>IP</span><strong>{{ selectedEvent.client_ip || "未知" }}</strong></div>
<div><span>位置</span><strong>{{ formatIpLocation(selectedEvent) }}</strong></div>
<div><span>接口类型</span><strong>{{ resolveEndpointType(selectedEvent.path) }}</strong></div>
<div><span>账号</span><strong>{{ selectedEvent.account_label }}</strong></div>
</div>
<div class="detail-section">
<span>请求路径</span>
<code>{{ selectedEvent.method }} {{ selectedEvent.path }}</code>
</div>
<div class="detail-section">
<span>User Agent</span>
<code>{{ selectedEvent.user_agent || "-" }}</code>
</div>
</div>
</el-drawer>
</div>
</template>
<script setup lang="ts">
import { computed, h, onMounted, ref, watch } from "vue";
import { Search as SearchIcon } from "@element-plus/icons-vue";
import { fetchSecurityAccessLogs } from "@/api/projectPermissions";
import type { SecurityAccessLogItem } from "@/types/api";
type MetricTone = "blue" | "cyan" | "danger" | "indigo";
const MetricIconShield = () => h("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", "stroke-width": "2" }, [
h("path", { d: "M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z" }),
]);
const MetricIconIp = () => h("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", "stroke-width": "2" }, [
h("circle", { cx: "12", cy: "10", r: "3" }),
h("path", { d: "M12 21s7-5.2 7-11a7 7 0 1 0-14 0c0 5.8 7 11 7 11z" }),
]);
const MetricIconAlert = () => h("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", "stroke-width": "2" }, [
h("path", { d: "M10.3 3.9 1.8 18a2 2 0 0 0 1.7 3h17a2 2 0 0 0 1.7-3L13.7 3.9a2 2 0 0 0-3.4 0z" }),
h("line", { x1: "12", y1: "9", x2: "12", y2: "13" }),
h("line", { x1: "12", y1: "17", x2: "12.01", y2: "17" }),
]);
const MetricIconServer = () => h("svg", { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", "stroke-width": "2" }, [
h("rect", { x: "3", y: "4", width: "18", height: "8", rx: "2" }),
h("rect", { x: "3", y: "12", width: "18", height: "8", rx: "2" }),
h("line", { x1: "7", y1: "8", x2: "7.01", y2: "8" }),
h("line", { x1: "7", y1: "16", x2: "7.01", y2: "16" }),
]);
const loading = ref(false);
const statsEvents = ref<SecurityAccessLogItem[]>([]);
const page = ref(1);
const pageSize = ref(10);
const keyword = ref("");
const severityFilter = ref("");
const categoryFilter = ref("");
const authFilter = ref("");
const detailVisible = ref(false);
const selectedEvent = ref<SecurityAccessLogItem | null>(null);
const severityOptions = [
{ label: "严重", value: "CRITICAL", tone: "critical" },
{ label: "高", value: "HIGH", tone: "high" },
{ label: "中", value: "MEDIUM", tone: "medium" },
{ label: "低", value: "LOW", tone: "low" },
];
const categoryOptions = [
{ label: "敏感路径探测", value: "PROBE" },
{ label: "异常IP", value: "ABNORMAL_IP" },
{ label: "无效令牌", value: "INVALID_TOKEN" },
{ label: "服务异常", value: "SERVER_ERROR" },
{ label: "匿名 API", value: "ANONYMOUS_API" },
{ label: "普通 404", value: "NOT_FOUND_NOISE" },
{ label: "其他", value: "OTHER" },
];
const severityLabel = (value: string) => severityOptions.find((item) => item.value === value)?.label || value;
const categoryLabel = (value: string) => categoryOptions.find((item) => item.value === value)?.label || value;
const authLabel = (value: string) => {
const labels: Record<string, string> = {
ANONYMOUS: "匿名",
INVALID_TOKEN: "无效令牌",
AUTHENTICATED: "已认证",
};
return labels[value] || value;
};
const severityTagType = (value: string) => {
if (value === "CRITICAL" || value === "HIGH") return "danger";
if (value === "MEDIUM") return "warning";
return "info";
};
const formatNumber = (value: number) => new Intl.NumberFormat("zh-CN").format(value || 0);
const formatTime = (value: string) => new Date(value).toLocaleString("zh-CN", { hour12: false });
const isHighRisk = (event: SecurityAccessLogItem) => event.severity === "CRITICAL" || event.severity === "HIGH";
const fallbackIpLocation = (ip: string | null) => {
if (!ip) return "未知位置";
if (ip === "127.0.0.1" || ip === "::1") return "本机访问";
if (/^(10\.|192\.168\.|172\.(1[6-9]|2\d|3[0-1])\.)/.test(ip)) return "内网地址";
return "未知位置";
};
const formatIpLocation = (row: SecurityAccessLogItem) => {
const parts = [
row.ip_country && row.ip_country !== "中国" ? row.ip_country : "",
row.ip_province,
row.ip_city,
row.ip_isp,
].filter(Boolean);
if (parts.length) return parts.join(" / ");
return row.ip_location || fallbackIpLocation(row.client_ip);
};
const resolveEndpointType = (path: string) => {
const normalizedPath = path.toLowerCase();
if (normalizedPath.includes("/auth") || normalizedPath.includes("/login")) return "认证接口";
if (normalizedPath.startsWith("/api/")) return "业务 API";
if (normalizedPath.match(/\.(js|css|png|jpg|jpeg|svg|ico|map)$/)) return "静态资源";
if ([".env", "wp-", "php", "admin", "shell", "config"].some((marker) => normalizedPath.includes(marker))) return "探测路径";
return "其他请求";
};
const detailFilteredEvents = computed(() => {
const token = keyword.value.trim().toLowerCase();
return statsEvents.value.filter((event) => {
if (severityFilter.value && event.severity !== severityFilter.value) return false;
if (categoryFilter.value && event.category !== categoryFilter.value) return false;
if (authFilter.value && event.auth_status !== authFilter.value) return false;
if (!token) return true;
return [
event.client_ip,
formatIpLocation(event),
event.account_label,
event.path,
resolveEndpointType(event.path),
event.user_agent,
event.auth_status,
event.status_code,
].some((value) => String(value || "").toLowerCase().includes(token));
});
});
const securityTotal = computed(() => detailFilteredEvents.value.length);
const paginatedEvents = computed(() => {
const start = (page.value - 1) * pageSize.value;
return detailFilteredEvents.value.slice(start, start + pageSize.value);
});
const countByCategory = (category: string) => statsEvents.value.filter((event) => event.category === category).length;
const metricCards = computed<Array<{ label: string; value: string; tone: MetricTone; icon: any }>>(() => {
const uniqueIpCount = new Set(statsEvents.value.map((event) => event.client_ip || "未知 IP")).size;
const highRiskCount = statsEvents.value.filter(isHighRisk).length;
const serverErrorCount = countByCategory("SERVER_ERROR");
return [
{ label: "安全事件", value: formatNumber(statsEvents.value.length), tone: "blue", icon: MetricIconShield },
{ label: "异常 IP", value: formatNumber(uniqueIpCount), tone: "cyan", icon: MetricIconIp },
{ label: "高危风险", value: formatNumber(highRiskCount), tone: highRiskCount > 0 ? "danger" : "indigo", icon: MetricIconAlert },
{ label: "服务异常", value: formatNumber(serverErrorCount), tone: serverErrorCount > 0 ? "danger" : "indigo", icon: MetricIconServer },
];
});
const ipRiskStats = computed(() => {
const stats = new Map<string, { ip: string; location: string; categoryText: string; total: number; highRiskCount: number; lastSeenAt: string }>();
statsEvents.value.forEach((event) => {
const ip = event.client_ip || "未知 IP";
const current = stats.get(ip) || {
ip,
location: formatIpLocation(event),
categoryText: categoryLabel(event.category),
total: 0,
highRiskCount: 0,
lastSeenAt: event.created_at,
};
current.total += 1;
if (isHighRisk(event)) current.highRiskCount += 1;
if (new Date(event.created_at).getTime() > new Date(current.lastSeenAt).getTime()) {
current.lastSeenAt = event.created_at;
current.categoryText = categoryLabel(event.category);
}
stats.set(ip, current);
});
return [...stats.values()].sort((a, b) => b.highRiskCount - a.highRiskCount || b.total - a.total).slice(0, 10);
});
const endpointTypeStats = computed(() => {
const stats = new Map<string, { type: string; total: number; samplePath: string }>();
statsEvents.value.forEach((event) => {
const type = resolveEndpointType(event.path);
const current = stats.get(type) || { type, total: 0, samplePath: event.path };
current.total += 1;
stats.set(type, current);
});
const maxTotal = Math.max(1, ...[...stats.values()].map((item) => item.total));
return [...stats.values()]
.sort((a, b) => b.total - a.total)
.map((item) => ({ ...item, percent: Math.max(8, Math.round((item.total / maxTotal) * 100)) }));
});
const riskLevelStats = computed(() => {
const filteredCounts = severityOptions.map((item) => ({
...item,
total: statsEvents.value.filter((event) => event.severity === item.value).length,
}));
const maxTotal = Math.max(1, ...filteredCounts.map((item) => item.total));
return filteredCounts.map((item) => {
return {
...item,
percent: item.total > 0 ? Math.max(8, Math.round((item.total / maxTotal) * 100)) : 0,
};
});
});
const SECURITY_STATS_PAGE_SIZE = 200;
const loadSecurityEvents = async () => {
loading.value = true;
try {
await loadSecurityStats();
} finally {
loading.value = false;
}
};
const loadSecurityStats = async () => {
const first = await fetchSecurityAccessLogs({
status_min: 400,
auth_status: authFilter.value || undefined,
page: 1,
page_size: SECURITY_STATS_PAGE_SIZE,
});
const allItems = [...first.data.items];
const total = first.data.total;
const totalPages = Math.ceil(total / SECURITY_STATS_PAGE_SIZE);
for (let nextPage = 2; nextPage <= totalPages; nextPage += 1) {
const res = await fetchSecurityAccessLogs({
status_min: 400,
auth_status: authFilter.value || undefined,
page: nextPage,
page_size: SECURITY_STATS_PAGE_SIZE,
});
allItems.push(...res.data.items);
}
statsEvents.value = allItems;
};
const onFilterChange = () => {
page.value = 1;
loadSecurityEvents();
};
const onPageSizeChange = (size: number) => {
pageSize.value = size;
page.value = 1;
};
watch(keyword, () => {
page.value = 1;
});
const openDetail = (event: SecurityAccessLogItem) => {
selectedEvent.value = event;
detailVisible.value = true;
};
onMounted(loadSecurityEvents);
defineExpose({ refresh: loadSecurityEvents });
</script>
<style scoped>
.security-center {
--audit-ink: #1a2332;
--audit-muted: #64748b;
--audit-border: #e2e8f0;
--audit-blue: #3b82f6;
--audit-cyan: #06b6d4;
--audit-danger: #ef4444;
--audit-indigo: #6366f1;
--card-radius: 16px;
--card-shadow: 0 1px 3px rgba(0, 0, 0, 0.04), 0 4px 12px rgba(0, 0, 0, 0.03);
display: flex;
flex-direction: column;
gap: 16px;
}
.audit-filters {
display: flex;
gap: 10px;
flex-wrap: wrap;
align-items: center;
padding: 14px 18px;
background: #fff;
border-radius: var(--card-radius);
border: 1px solid var(--audit-border);
box-shadow: var(--card-shadow);
}
.security-search {
width: 280px;
}
.audit-metrics {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 14px;
}
.metric-card {
position: relative;
overflow: hidden;
display: flex;
align-items: center;
gap: 14px;
min-height: 64px;
padding: 10px 16px;
border-radius: var(--card-radius);
background: #fff;
border: 1px solid var(--audit-border);
box-shadow: var(--card-shadow);
transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.metric-card:hover {
transform: translateY(-2px);
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}
.metric-card::before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
height: 3px;
border-radius: var(--card-radius) var(--card-radius) 0 0;
}
.metric-card.blue::before { background: linear-gradient(90deg, #3b82f6, #60a5fa); }
.metric-card.cyan::before { background: linear-gradient(90deg, #06b6d4, #22d3ee); }
.metric-card.danger::before { background: linear-gradient(90deg, #ef4444, #f87171); }
.metric-card.indigo::before { background: linear-gradient(90deg, #6366f1, #818cf8); }
.metric-icon {
display: flex;
align-items: center;
justify-content: center;
width: 44px;
height: 44px;
border-radius: 12px;
flex-shrink: 0;
}
.metric-icon svg {
width: 22px;
height: 22px;
}
.metric-card.blue .metric-icon { background: #eff6ff; color: #3b82f6; }
.metric-card.cyan .metric-icon { background: #ecfeff; color: #06b6d4; }
.metric-card.danger .metric-icon { background: #fef2f2; color: #ef4444; }
.metric-card.indigo .metric-icon { background: #eef2ff; color: #6366f1; }
.metric-body {
display: flex;
flex-direction: column;
gap: 4px;
min-width: 0;
}
.metric-label {
color: var(--audit-muted);
font-size: 13px;
font-weight: 500;
}
.metric-value {
color: var(--audit-ink);
font-size: 24px;
font-weight: 700;
line-height: 1.1;
}
.audit-grid {
display: grid;
grid-template-columns: minmax(320px, 1fr) minmax(0, 1.35fr);
gap: 14px;
}
.ranking-card,
.security-table-wrap {
min-width: 0;
padding: 20px;
border: 1px solid var(--audit-border);
border-radius: var(--card-radius);
background: #fff;
box-shadow: var(--card-shadow);
}
.section-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 16px;
}
.table-head {
align-items: center;
flex-wrap: wrap;
margin-bottom: 10px;
}
.table-head .section-title-group {
flex-shrink: 0;
}
.table-head .el-tag {
margin-left: auto;
flex-shrink: 0;
}
.detail-filters {
flex: 1;
min-width: 0;
padding: 0;
border: 0;
box-shadow: none;
}
.section-title-group h4 {
margin: 0;
color: var(--audit-ink);
font-size: 16px;
font-weight: 600;
}
.section-title-group p {
margin: 4px 0 0;
color: var(--audit-muted);
font-size: 12px;
}
.risk-rank-list {
display: flex;
flex-direction: column;
gap: 2px;
}
.risk-rank-row {
display: grid;
grid-template-columns: 36px minmax(0, 1fr) auto;
gap: 10px;
align-items: center;
padding: 12px 10px;
border-radius: 10px;
transition: background 0.15s ease;
}
.risk-rank-row:hover {
background: #f8fafc;
}
.rank-no {
display: flex;
align-items: center;
justify-content: center;
width: 28px;
height: 28px;
border-radius: 8px;
background: #f1f5f9;
color: #94a3b8;
font-size: 12px;
font-weight: 700;
}
.rank-no.rank-top {
background: linear-gradient(135deg, #ef4444, #6366f1);
color: #fff;
}
.rank-user,
.rank-count {
display: flex;
flex-direction: column;
gap: 6px;
min-width: 0;
}
.rank-main-line,
.rank-meta-line {
display: flex;
align-items: baseline;
gap: 8px;
min-width: 0;
}
.rank-main-line strong {
flex-shrink: 0;
overflow: hidden;
color: var(--audit-ink);
font-size: 14px;
font-weight: 600;
text-overflow: ellipsis;
white-space: nowrap;
}
.rank-main-line small {
overflow: hidden;
color: var(--audit-muted);
font-size: 12px;
text-overflow: ellipsis;
white-space: nowrap;
}
.rank-meta-line span {
color: var(--audit-muted);
font-size: 12px;
}
.rank-count {
align-items: flex-end;
}
.rank-count strong {
color: var(--audit-ink);
font-size: 18px;
font-weight: 700;
}
.rank-count small {
color: var(--audit-muted);
font-size: 11px;
}
.rank-count .denied-highlight {
color: var(--audit-danger);
font-weight: 600;
}
.distribution-list,
.risk-level-list {
display: flex;
flex-direction: column;
gap: 12px;
}
.event-distribution-card {
display: flex;
flex-direction: column;
gap: 22px;
}
.distribution-section {
display: flex;
flex-direction: column;
gap: 14px;
}
.distribution-section + .distribution-section {
padding-top: 24px;
border-top: 1px solid var(--audit-border);
}
.event-distribution-card > .section-head {
margin-bottom: 0;
}
.event-distribution-card .section-title-group h4 {
font-size: 16px;
}
.distribution-section-head {
display: flex;
align-items: center;
justify-content: space-between;
}
.distribution-section-head strong {
color: var(--audit-ink);
font-size: 15px;
font-weight: 700;
}
.distribution-row {
display: grid;
grid-template-columns: minmax(160px, 0.9fr) minmax(120px, 1fr) 44px;
gap: 12px;
align-items: center;
}
.distribution-info {
min-width: 0;
}
.distribution-info strong {
display: block;
color: var(--audit-ink);
font-size: 14px;
font-weight: 600;
}
.distribution-info small {
display: block;
overflow: hidden;
margin-top: 3px;
color: var(--audit-muted);
font-size: 12px;
text-overflow: ellipsis;
white-space: nowrap;
}
.distribution-meter,
.risk-level-meter {
overflow: hidden;
height: 8px;
border-radius: 999px;
background: #f1f5f9;
}
.distribution-meter span {
display: block;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #06b6d4, #3b82f6);
}
.distribution-count {
color: var(--audit-ink);
font-size: 16px;
text-align: right;
}
.risk-level-row {
display: grid;
grid-template-columns: 10px 52px minmax(0, 1fr) 44px;
gap: 10px;
align-items: center;
}
.risk-dot {
width: 8px;
height: 8px;
border-radius: 999px;
}
.risk-dot.critical,
.risk-level-meter span.critical { background: #991b1b; }
.risk-dot.high,
.risk-level-meter span.high { background: #ef4444; }
.risk-dot.medium,
.risk-level-meter span.medium { background: #f59e0b; }
.risk-dot.low,
.risk-level-meter span.low { background: #64748b; }
.risk-level-row > span:nth-child(2) {
color: var(--audit-ink);
font-size: 13px;
font-weight: 600;
}
.risk-level-meter span {
display: block;
height: 100%;
border-radius: inherit;
}
.risk-level-row strong {
color: var(--audit-ink);
font-size: 14px;
text-align: right;
}
.security-table-wrap {
overflow: hidden;
}
.pagination-wrap {
display: flex;
justify-content: flex-end;
margin-top: 10px;
padding: 8px 16px 0;
}
.category-pill {
display: inline-flex;
align-items: center;
max-width: 112px;
padding: 3px 8px;
border-radius: 999px;
background: #eef2f7;
color: #334155;
font-size: 12px;
font-weight: 600;
}
.request-cell {
display: flex;
flex-direction: column;
gap: 3px;
min-width: 0;
}
.request-cell strong {
font-size: 12px;
color: #475569;
}
.request-cell span {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
color: var(--audit-ink);
}
.security-detail-head {
display: flex;
width: 100%;
align-items: center;
justify-content: space-between;
}
.security-detail {
display: flex;
flex-direction: column;
gap: 14px;
}
.detail-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 10px;
}
.detail-grid div,
.detail-section {
display: flex;
flex-direction: column;
gap: 6px;
padding: 12px;
border: 1px solid var(--audit-border);
border-radius: 8px;
background: #f8fafc;
}
.detail-grid span,
.detail-section span {
color: var(--audit-muted);
font-size: 12px;
}
.detail-grid strong {
color: var(--audit-ink);
font-size: 14px;
}
.detail-section code {
white-space: pre-wrap;
word-break: break-all;
color: var(--audit-ink);
}
@media (max-width: 1100px) {
.audit-metrics,
.audit-grid {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
@media (max-width: 720px) {
.audit-metrics,
.audit-grid {
grid-template-columns: 1fr;
}
.security-search {
width: 100%;
}
.distribution-row {
grid-template-columns: 1fr 44px;
}
.distribution-meter {
grid-column: 1 / -1;
grid-row: 2;
}
}
</style>
@@ -0,0 +1,98 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { mount } from "@vue/test-utils";
import { createPinia, setActivePinia } from "pinia";
import { useAuthStore } from "../store/auth";
import { useSessionStore } from "../store/session";
import SessionTimeoutPrompt from "./SessionTimeoutPrompt.vue";
const sessionManagerMock = vi.hoisted(() => ({
forceLogout: vi.fn(),
markUserActive: vi.fn(),
}));
vi.mock("../session/sessionManager", () => ({
forceLogout: sessionManagerMock.forceLogout,
markUserActive: sessionManagerMock.markUserActive,
LOGOUT_REASON_MANUAL: "manual",
TIMEOUT_WARNING_SECONDS: 60,
}));
const mountPrompt = () =>
mount(SessionTimeoutPrompt, {
global: {
stubs: {
transition: false,
"el-button": {
inheritAttrs: false,
template: '<button type="button" @click="$emit(\'click\')"><slot /></button>',
},
},
},
});
const createStorage = () => {
const data = new Map<string, string>();
return {
getItem: (key: string) => data.get(key) ?? null,
setItem: (key: string, value: string) => {
data.set(key, String(value));
},
removeItem: (key: string) => {
data.delete(key);
},
clear: () => {
data.clear();
},
};
};
describe("SessionTimeoutPrompt", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-03-31T12:00:00.000Z"));
setActivePinia(createPinia());
sessionManagerMock.forceLogout.mockClear();
sessionManagerMock.markUserActive.mockClear();
Object.defineProperty(window, "localStorage", {
value: createStorage(),
configurable: true,
});
});
it("shows the remaining time before automatic logout", () => {
const auth = useAuthStore();
const session = useSessionStore();
auth.setToken("token");
session.showTimeoutWarning(Date.now() + 60_000);
const wrapper = mountPrompt();
expect(wrapper.text()).toContain("即将自动退出");
expect(wrapper.text()).toContain("60 秒");
expect(wrapper.find(".timeout-progress span").attributes("style")).toContain("width: 100%");
});
it("continues the session from the warning action", async () => {
const auth = useAuthStore();
const session = useSessionStore();
auth.setToken("token");
session.showTimeoutWarning(Date.now() + 60_000);
const wrapper = mountPrompt();
await wrapper.findAll("button")[1].trigger("click");
expect(sessionManagerMock.markUserActive).toHaveBeenCalledOnce();
});
it("logs out immediately from the warning action", async () => {
const auth = useAuthStore();
const session = useSessionStore();
auth.setToken("token");
session.showTimeoutWarning(Date.now() + 60_000);
const wrapper = mountPrompt();
await wrapper.findAll("button")[0].trigger("click");
expect(sessionManagerMock.forceLogout).toHaveBeenCalledWith("manual");
});
});
@@ -0,0 +1,199 @@
<template>
<transition name="session-timeout-fade">
<div v-if="visible" class="session-timeout-prompt" role="status" aria-live="polite">
<div class="timeout-icon" aria-hidden="true">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="9"></circle>
<path d="M12 7v5l3 2"></path>
</svg>
</div>
<div class="timeout-copy">
<strong>即将自动退出</strong>
<span>{{ countdownText }} 后将退出登录以保护项目数据</span>
</div>
<div class="timeout-actions">
<el-button size="small" @click="logoutNow">立即退出</el-button>
<el-button size="small" type="primary" @click="continueSession">继续使用</el-button>
</div>
<div class="timeout-progress" aria-hidden="true">
<span :style="{ width: progressWidth }"></span>
</div>
</div>
</transition>
</template>
<script setup lang="ts">
import { computed, onBeforeUnmount, ref, watch } from "vue";
import { useAuthStore } from "../store/auth";
import { useSessionStore } from "../store/session";
import {
forceLogout,
LOGOUT_REASON_MANUAL,
markUserActive,
TIMEOUT_WARNING_SECONDS,
} from "../session/sessionManager";
const auth = useAuthStore();
const session = useSessionStore();
const nowTs = ref(Date.now());
let timer: number | null = null;
const visible = computed(() => Boolean(auth.token && session.timeoutWarningVisible));
const countdownText = computed(() => {
const remainSeconds = Math.max(0, Math.ceil((session.timeoutAt - nowTs.value) / 1000));
return `${remainSeconds}`;
});
const progressWidth = computed(() => {
const remainSeconds = Math.max(0, Math.ceil((session.timeoutAt - nowTs.value) / 1000));
const ratio = Math.min(1, remainSeconds / TIMEOUT_WARNING_SECONDS);
return `${Math.round(ratio * 100)}%`;
});
const stopTimer = () => {
if (!timer) return;
window.clearInterval(timer);
timer = null;
};
const startTimer = () => {
stopTimer();
nowTs.value = Date.now();
timer = window.setInterval(() => {
nowTs.value = Date.now();
}, 1000);
};
const continueSession = () => {
markUserActive();
};
const logoutNow = () => {
forceLogout(LOGOUT_REASON_MANUAL);
};
watch(
visible,
(value) => {
if (value) {
startTimer();
return;
}
stopTimer();
},
{ immediate: true },
);
onBeforeUnmount(() => {
stopTimer();
});
</script>
<style scoped>
.session-timeout-prompt {
position: fixed;
right: 24px;
bottom: 24px;
z-index: 2200;
width: min(420px, calc(100vw - 32px));
display: grid;
grid-template-columns: 38px minmax(0, 1fr) auto;
align-items: center;
gap: 12px;
padding: 14px 16px;
border-radius: 14px;
border: 1px solid rgba(197, 139, 42, 0.24);
background: rgba(255, 252, 246, 0.96);
box-shadow: 0 18px 40px rgba(15, 23, 42, 0.14), inset 0 1px 0 rgba(255, 255, 255, 0.72);
backdrop-filter: blur(16px);
color: #3b2b12;
}
.timeout-progress {
grid-column: 1 / -1;
height: 3px;
overflow: hidden;
border-radius: 999px;
background: rgba(197, 139, 42, 0.14);
}
.timeout-progress span {
display: block;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #d59a35, #f2b84f);
box-shadow: 0 0 12px rgba(242, 184, 79, 0.36);
transition: width 220ms ease;
}
.timeout-icon {
width: 38px;
height: 38px;
display: inline-flex;
align-items: center;
justify-content: center;
border-radius: 11px;
color: var(--ctms-warning);
background: #fff3dc;
box-shadow: inset 0 0 0 1px rgba(197, 139, 42, 0.16);
}
.timeout-icon svg {
width: 20px;
height: 20px;
}
.timeout-copy {
min-width: 0;
display: flex;
flex-direction: column;
gap: 2px;
}
.timeout-copy strong {
font-size: 13px;
line-height: 18px;
font-weight: 800;
color: #182235;
}
.timeout-copy span {
font-size: 12px;
line-height: 18px;
font-weight: 600;
color: #7a5a23;
}
.timeout-actions {
display: inline-flex;
gap: 8px;
align-items: center;
}
.session-timeout-fade-enter-active,
.session-timeout-fade-leave-active {
transition: opacity 160ms ease, transform 160ms ease;
}
.session-timeout-fade-enter-from,
.session-timeout-fade-leave-to {
opacity: 0;
transform: translateY(8px);
}
@media (max-width: 640px) {
.session-timeout-prompt {
left: 16px;
right: 16px;
bottom: 16px;
width: auto;
grid-template-columns: 34px minmax(0, 1fr);
}
.timeout-actions {
grid-column: 1 / -1;
justify-content: flex-end;
}
}
</style>
@@ -1,117 +0,0 @@
import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
const readAttachmentList = () => readFileSync(resolve(__dirname, "./AttachmentList.vue"), "utf8");
describe("AttachmentList permissions", () => {
it("does not require project member list permission to render uploaders", () => {
const source = readAttachmentList();
expect(source).not.toContain("listMembers");
});
it("matches the contract-fee attachment table layout", () => {
const source = readAttachmentList();
expect(source).toContain('prop="filename" :label="TEXT.common.labels.filename" min-width="360" show-overflow-tooltip');
expect(source).toContain(':label="TEXT.common.labels.size" min-width="180"');
expect(source).toContain(':label="TEXT.common.labels.uploader" min-width="180"');
expect(source).toContain('prop="uploaded_at" :label="TEXT.common.labels.uploadedAt" min-width="180" show-overflow-tooltip');
expect(source).toContain(':label="TEXT.common.labels.actions" min-width="180"');
expect(source).not.toContain('width="160"');
expect(source).toContain('class="attachment-actions"');
expect(source).toContain("flex-wrap: nowrap;");
expect(source).toContain("gap: 4px;");
});
it("can aggregate multiple attachment entity types into one table with an attachment type column", () => {
const source = readAttachmentList();
expect(source).toContain("entityGroups?: AttachmentEntityGroup[]");
expect(source).toContain('v-if="hasEntityGroups"');
expect(source).toContain('label="附件类型"');
expect(source).toContain("scope.row.attachment_type_label");
expect(source).toContain("fetchAttachments(props.studyId, group.entityType, props.entityId)");
expect(source).toContain("attachment_type_label: group.label");
expect(source).toContain("attachment_entity_type: group.entityType");
});
it("can reload attachments when the parent refresh key changes", () => {
const source = readAttachmentList();
expect(source).toContain("refreshKey?: number");
expect(source).toContain("() => props.refreshKey");
expect(source).toContain("load();");
});
it("can hide the uploader while keeping the attachment table visible", () => {
const source = readAttachmentList();
expect(source).toContain("hideUploader?: boolean");
expect(source).toContain("canShowUploader");
expect(source).toContain("!props.hideUploader && canCreate.value");
});
it("hides the default attachment title row when uploader is hidden and no explicit title is provided", () => {
const source = readAttachmentList();
expect(source).toContain("const showHeader = computed(() => !!props.title || canShowUploader.value)");
expect(source).toContain('<div v-if="showHeader" class="header">');
});
it("uses the shared upload implementation for table-mode immediate uploads", () => {
const source = readAttachmentList();
expect(source).not.toContain("AttachmentUploader");
expect(source).toContain(':http-request="uploadImmediate"');
expect(source).toContain("const uploadImmediate = async");
expect(source).toContain("await uploadFile(group, file, props.entityId)");
expect(source).toContain("ElMessage.success(TEXT.common.messages.uploadSuccess)");
expect(source).toContain("load();");
});
it("supports upload-card mode without rendering the table header", () => {
const source = readAttachmentList();
expect(source).toContain('mode?: "table" | "upload"');
expect(source).toContain("displayMode");
expect(source).toContain('v-if="displayMode === \'table\'"');
expect(source).toContain("v-else");
expect(source).toContain('class="attachment-upload-card"');
expect(source).toContain(':auto-upload="false"');
expect(source).toContain("queuePendingUpload(group.key, file)");
expect(source).toContain("点击上传文件");
});
it("allows create forms to choose attachments before the entity id exists", () => {
const source = readAttachmentList();
expect(source).toContain("const pendingSnapshot = () =>");
expect(source).toContain("const uploadPending = async");
expect(source).toContain("const targetEntityId = entityId || props.entityId");
expect(source).toContain("defineExpose({");
expect(source).toContain("pendingSnapshot");
expect(source).toContain("uploadPending");
expect(source).toContain("pendingMap[group.key] = remainItems");
expect(source).toContain("throw new Error(TEXT.common.messages.uploadFailed)");
});
it("fails pending upload when selected files have no saved entity id", () => {
const source = readAttachmentList();
expect(source).toContain("const hasPendingUploads = computed");
expect(source).toContain("if (!targetEntityId && hasPendingUploads.value)");
expect(source).toContain("throw new Error(TEXT.common.messages.uploadFailed)");
});
it("uses entity groups as upload groups when an editor has multiple attachment types", () => {
const source = readAttachmentList();
expect(source).toContain("const uploadGroups = computed");
expect(source).toContain("props.entityGroups.map((group)");
expect(source).toContain("key: group.entityType");
expect(source).toContain("entityType: group.entityType");
expect(source).toContain("uploadAttachment(props.studyId, group.entityType, targetEntityId, file");
});
});
+110
View File
@@ -0,0 +1,110 @@
// 地图数据来源:https://unpkg.com/echarts@4.9.0/map/json/world.json
// 组件直接导入本地缓存的 ECharts 世界地图 GeoJSON,避免运行时依赖外部网络。
import type { IpLocationStatItem } from "../types/api";
export const normalizeWorldCountryName = (value?: string | null) => {
const country = String(value || "").trim();
const aliases: Record<string, string> = {
: "China",
China: "China",
"Mainland China": "China",
: "China",
: "China",
: "China",
: "United States",
"United States": "United States",
"United States of America": "United States",
USA: "United States",
Australia: "Australia",
: "Australia",
Japan: "Japan",
: "Japan",
Singapore: "Singapore",
: "Singapore",
Germany: "Germany",
: "Germany",
France: "France",
: "France",
"United Kingdom": "United Kingdom",
: "United Kingdom",
Canada: "Canada",
: "Canada",
India: "India",
: "India",
Russia: "Russia",
: "Russia",
};
return aliases[country] || country;
};
export const countryCoordinates: Record<string, [number, number]> = {
China: [104.1954, 35.8617],
"United States": [-95.7129, 37.0902],
Netherlands: [5.2913, 52.1326],
Türkiye: [35.2433, 38.9637],
Turkey: [35.2433, 38.9637],
Australia: [133.7751, -25.2744],
Japan: [138.2529, 36.2048],
Singapore: [103.8198, 1.3521],
Germany: [10.4515, 51.1657],
France: [2.2137, 46.2276],
"United Kingdom": [-3.436, 55.3781],
Canada: [-106.3468, 56.1304],
India: [78.9629, 20.5937],
Russia: [105.3188, 61.524],
};
export const cityCoordinates: Record<string, [number, number]> = {
"局域网": [121.86, 31.45],
"北京市": [116.4074, 39.9042],
"天津市": [117.2008, 39.0842],
"河北省": [114.5025, 38.0455],
"山西省": [112.5492, 37.857],
"内蒙古自治区": [111.6708, 40.8183],
"辽宁省": [123.4315, 41.8057],
"吉林省": [125.3245, 43.8868],
"黑龙江省": [126.6424, 45.7567],
"上海市": [121.4737, 31.2304],
"江苏省": [118.7633, 32.0617],
"浙江省": [120.1551, 30.2741],
"安徽省": [117.2272, 31.8206],
"福建省": [119.2965, 26.0745],
"江西省": [115.8582, 28.682],
"山东省": [117.1201, 36.6512],
"河南省": [113.6254, 34.7466],
"湖北省": [114.3055, 30.5928],
"湖南省": [112.9388, 28.2282],
"广东省": [113.2644, 23.1291],
"广西壮族自治区": [108.3669, 22.817],
"海南省": [110.3312, 20.0311],
"重庆": [106.5516, 29.563],
"重庆市": [106.5516, 29.563],
"四川省": [104.0665, 30.5723],
"贵州省": [106.6302, 26.647],
"云南省": [102.8329, 24.8801],
"西藏自治区": [91.1322, 29.6604],
"陕西省": [108.9398, 34.3416],
"甘肃省": [103.8343, 36.0611],
"青海省": [101.7782, 36.6171],
"宁夏回族自治区": [106.2309, 38.4872],
"新疆维吾尔自治区": [87.6168, 43.8256],
"台湾省": [121.5654, 25.033],
"香港特别行政区": [114.1694, 22.3193],
"澳门特别行政区": [113.5439, 22.1987],
"南京": [118.7969, 32.0603],
"南京市": [118.7969, 32.0603],
"San Jose": [-121.8863, 37.3382],
"South Holland": [4.493, 52.0208],
"Istanbul": [28.9784, 41.0082],
};
export const resolveSourceCoordinate = (item: IpLocationStatItem): [number, number] | null => {
if (item.location === "局域网") return cityCoordinates["局域网"];
const city = String(item.city || "").trim();
if (city && cityCoordinates[city]) return cityCoordinates[city];
const province = String(item.province || "").trim();
if (province && cityCoordinates[province]) return cityCoordinates[province];
const country = normalizeWorldCountryName(item.country);
return country ? countryCoordinates[country] || null : null;
};
@@ -1,46 +0,0 @@
import { describe, expect, it, vi } from "vitest";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { useRoleTemplateMeta } from "./useRoleTemplateMeta";
vi.mock("@/api/projectPermissions", () => ({
fetchPermissionTemplates: vi.fn().mockResolvedValue({
data: [
{ category: "PV", name: "PV" },
{ category: "QA", name: "QA" },
{ category: "CTA", name: "CTA" },
{ category: "CRA", name: "CRA" },
{ category: "PM", name: "PM" },
],
}),
}));
const readSource = () => readFileSync(resolve(__dirname, "./useRoleTemplateMeta.ts"), "utf8");
describe("useRoleTemplateMeta fallback copy", () => {
it("uses QA and CTA as first-class preset role keys", () => {
const source = readSource();
expect(source).toContain('PM: { label: "PM"');
expect(source).toContain("项目负责人,统筹项目全局,协调进度、资源与关键决策。");
expect(source).toContain("负责各中心临床监查执行,跟进现场质量、数据和问题闭环。");
expect(source).toContain("负责合同、药品及相关项目事务管理,保障执行支持与物资协同。");
expect(source).toContain('QA: { label: "QA"');
expect(source).toContain("负责医学审核与稽查,关注质量风险、合规性和医学一致性。");
expect(source).toContain('CTA: { label: "CTA"');
expect(source).toContain("负责药物警戒相关工作,跟踪安全性事件并支持风险评估。");
});
it("pins PM first before applying the role template list order", async () => {
const { compareRolesByTemplateOrder, loadRoleTemplates } = useRoleTemplateMeta();
await loadRoleTemplates();
expect(["CRA", "PV", "QA", "PM", "CTA"].sort(compareRolesByTemplateOrder)).toEqual([
"PM",
"PV",
"QA",
"CTA",
"CRA",
]);
});
});
+23 -4
View File
@@ -2,6 +2,9 @@ export const TEXT = {
common: { common: {
appName: "CTMS", appName: "CTMS",
fallback: "—", fallback: "—",
separators: {
dot: "·",
},
loading: "加载中", loading: "加载中",
actions: { actions: {
add: "新增", add: "新增",
@@ -51,7 +54,6 @@ export const TEXT = {
noPermission: "无权限执行该操作", noPermission: "无权限执行该操作",
networkError: "网络错误,请稍后重试", networkError: "网络错误,请稍后重试",
fileTooLarge: "文件大小不能超过", fileTooLarge: "文件大小不能超过",
sessionLocked: "请解锁后继续操作",
sessionExpired: "登录已过期,请重新登录", sessionExpired: "登录已过期,请重新登录",
requestFailed: "请求失败,请稍后重试", requestFailed: "请求失败,请稍后重试",
invalidStateAction: "当前状态不允许该操作", invalidStateAction: "当前状态不允许该操作",
@@ -89,6 +91,18 @@ export const TEXT = {
userFallback: "用户", userFallback: "用户",
basicInfo: "基本信息", basicInfo: "基本信息",
allSites: "所有中心", allSites: "所有中心",
locked: "已锁定",
phase: "分期",
plannedSites: "中心",
plannedEnrollment: "入组",
dataUpdatedAt: "数据",
projectReminders: "提醒",
projectRemindersSubtitle: "逾期与风险提示",
projectRemindersEmpty: "暂无逾期风险",
overdueAes: "逾期 AE",
overdueAesDesc: "需关注安全性事件处理时效",
overdueMonitoringIssues: "逾期监查问题",
overdueMonitoringIssuesDesc: "需跟进问题关闭与整改",
yes: "是", yes: "是",
no: "否", no: "否",
}, },
@@ -238,8 +252,12 @@ export const TEXT = {
ISSUE_STATUS_CHANGED: { label: "风险/问题状态变更", actionText: "更新了风险/问题状态", targetLabel: "风险/问题" }, ISSUE_STATUS_CHANGED: { label: "风险/问题状态变更", actionText: "更新了风险/问题状态", targetLabel: "风险/问题" },
UNAUTHORIZED_ACTION_ATTEMPT: { label: "越权操作尝试", actionText: "尝试执行无权限操作", targetLabel: "系统资源" }, UNAUTHORIZED_ACTION_ATTEMPT: { label: "越权操作尝试", actionText: "尝试执行无权限操作", targetLabel: "系统资源" },
INVALID_STATE_TRANSITION_ATTEMPT: { label: "非法状态流转尝试", actionText: "尝试执行非法状态流转", targetLabel: "业务对象" }, INVALID_STATE_TRANSITION_ATTEMPT: { label: "非法状态流转尝试", actionText: "尝试执行非法状态流转", targetLabel: "业务对象" },
PROJECT_MEMBER_ADDED: { label: "项目成员新增", actionText: "添加了项目成员", targetLabel: "项目成员" },
PROJECT_MEMBER_UPDATED: { label: "项目成员变更", actionText: "调整了项目成员", targetLabel: "项目成员" }, PROJECT_MEMBER_UPDATED: { label: "项目成员变更", actionText: "调整了项目成员", targetLabel: "项目成员" },
PROJECT_MEMBER_REMOVED: { label: "项目成员移除", actionText: "移除了项目成员", targetLabel: "项目成员" },
SITE_CRA_BOUND: { label: "中心 CRA 绑定", actionText: "调整了中心与 CRA 的绑定", targetLabel: "中心" }, SITE_CRA_BOUND: { label: "中心 CRA 绑定", actionText: "调整了中心与 CRA 的绑定", targetLabel: "中心" },
SITE_CREATED: { label: "新增中心", actionText: "新增了中心", targetLabel: "中心" },
SITE_UPDATED: { label: "更新中心", actionText: "更新了中心", targetLabel: "中心" },
SITE_STATUS_CHANGED: { label: "中心状态变更", actionText: "更新了中心信息", targetLabel: "中心" }, SITE_STATUS_CHANGED: { label: "中心状态变更", actionText: "更新了中心信息", targetLabel: "中心" },
AUDIT_EXPORT_SYSTEM: { label: "导出系统审计", actionText: "导出了系统审计日志", targetLabel: "审计日志" }, AUDIT_EXPORT_SYSTEM: { label: "导出系统审计", actionText: "导出了系统审计日志", targetLabel: "审计日志" },
AUDIT_EXPORT_PROJECT: { label: "导出项目审计", actionText: "导出了项目审计日志", targetLabel: "审计日志" }, AUDIT_EXPORT_PROJECT: { label: "导出项目审计", actionText: "导出了项目审计日志", targetLabel: "审计日志" },
@@ -259,6 +277,7 @@ export const TEXT = {
projectManagement: "项目管理", projectManagement: "项目管理",
auditLogs: "审计日志", auditLogs: "审计日志",
permissionManagement: "权限管理", permissionManagement: "权限管理",
systemMonitoring: "系统监测",
currentProject: "当前项目", currentProject: "当前项目",
projectOverview: "项目总览", projectOverview: "项目总览",
projectMilestones: "项目里程碑", projectMilestones: "项目里程碑",
@@ -947,8 +966,7 @@ export const TEXT = {
default: "当前角色无权执行该操作", default: "当前角色无权执行该操作",
}, },
profile: { profile: {
title: "个人设置", title: "个人中心",
subtitle: "更新个人信息或修改登录密码",
uploadAvatar: "上传头像", uploadAvatar: "上传头像",
currentPassword: "当前密码", currentPassword: "当前密码",
currentPasswordHint: "修改密码时必填", currentPasswordHint: "修改密码时必填",
@@ -962,6 +980,7 @@ export const TEXT = {
updateSuccess: "已更新", updateSuccess: "已更新",
updateFailed: "更新失败", updateFailed: "更新失败",
avatarUpdated: "头像已更新", avatarUpdated: "头像已更新",
avatarTypeInvalid: "头像仅支持图片格式",
avatarUploadFailed: "头像上传失败", avatarUploadFailed: "头像上传失败",
}, },
}, },
@@ -1132,7 +1151,7 @@ export const TEXT = {
title: "权限管理", title: "权限管理",
moduleLevel: "模块级权限", moduleLevel: "模块级权限",
apiLevel: "角色权限", apiLevel: "角色权限",
monitoring: "权限监控", monitoring: "系统监测",
refreshMetrics: "刷新指标", refreshMetrics: "刷新指标",
save: "保存", save: "保存",
search: "搜索端点", search: "搜索端点",
+31 -1
View File
@@ -29,10 +29,23 @@ describe("admin project route permissions", () => {
expect(source).toContain("studyStore.currentPermissions?.[role]?.[operationKey]"); expect(source).toContain("studyStore.currentPermissions?.[role]?.[operationKey]");
}); });
it("checks admin project permissions against query project id when present", () => {
const source = readRouter();
const accessStart = source.indexOf("const ensureAdminProjectAccess");
const accessEnd = source.indexOf("const ensureRouteProjectMember", accessStart);
const accessBlock = source.slice(accessStart, accessEnd);
expect(accessBlock).toContain("const targetProjectId = getTargetProjectId(to);");
expect(accessBlock).toContain("studyStore.currentStudy?.id !== targetProjectId");
expect(accessBlock).toContain("fetchStudyDetail(targetProjectId)");
expect(accessBlock).toContain("fetchApiEndpointPermissions(targetProjectId)");
expect(accessBlock).not.toContain("typeof to.params.projectId");
});
it("guards project permission configuration with the system-level project config permission", () => { it("guards project permission configuration with the system-level project config permission", () => {
const source = readRouter(); const source = readRouter();
const projectPermissionRouteStart = source.indexOf('name: "AdminPermissionsProject"'); const projectPermissionRouteStart = source.indexOf('name: "AdminPermissionsProject"');
const projectPermissionRouteEnd = source.indexOf('name: "AdminPermissionsMonitoring"', projectPermissionRouteStart); const projectPermissionRouteEnd = source.indexOf('path: "system-monitoring"', projectPermissionRouteStart);
const projectPermissionRoute = source.slice(projectPermissionRouteStart, projectPermissionRouteEnd); const projectPermissionRoute = source.slice(projectPermissionRouteStart, projectPermissionRouteEnd);
expect(source).toContain('const SYSTEM_PERMISSION_PROJECT_CONFIG = "system:permissions:project_config";'); expect(source).toContain('const SYSTEM_PERMISSION_PROJECT_CONFIG = "system:permissions:project_config";');
@@ -48,6 +61,23 @@ describe("admin project route permissions", () => {
expect(targetProjectBranch).not.toContain("ensureDefaultPmStudy"); expect(targetProjectBranch).not.toContain("ensureDefaultPmStudy");
}); });
it("registers system monitoring as an admin-only peer admin module with legacy redirects", () => {
const source = readRouter();
const monitoringRouteStart = source.indexOf('name: "AdminSystemMonitoring"');
const monitoringRouteEnd = source.indexOf("],", monitoringRouteStart);
const monitoringRoute = source.slice(monitoringRouteStart, monitoringRouteEnd);
expect(source).toContain('import SystemMonitoringPage from "../views/admin/SystemMonitoringPage.vue";');
expect(source).toContain('path: "system-monitoring"');
expect(monitoringRoute).toContain("component: SystemMonitoringPage");
expect(monitoringRoute).toContain("requiresAdmin: true");
expect(source).toContain('path: "permission-monitoring"');
expect(source).toContain('path: "permissions/monitoring"');
expect(source).toContain('redirect: "/admin/system-monitoring"');
expect(source).not.toContain('const SYSTEM_PERMISSION_MONITORING_METRICS = "system:monitoring:metrics";');
expect(source).not.toContain("[SYSTEM_PERMISSION_MONITORING_METRICS]");
});
it("keeps login landing independent from PM management backend access", () => { it("keeps login landing independent from PM management backend access", () => {
const source = readRouter(); const source = readRouter();
+19 -12
View File
@@ -18,6 +18,7 @@ import AdminUserApproval from "../views/admin/AdminUserApproval.vue";
import AdminProjects from "../views/admin/Projects.vue"; import AdminProjects from "../views/admin/Projects.vue";
import AdminSites from "../views/admin/Sites.vue"; import AdminSites from "../views/admin/Sites.vue";
import PermissionManagement from "../views/admin/PermissionManagement.vue"; import PermissionManagement from "../views/admin/PermissionManagement.vue";
import SystemMonitoringPage from "../views/admin/SystemMonitoringPage.vue";
import ProjectDetail from "../views/admin/ProjectDetail.vue"; import ProjectDetail from "../views/admin/ProjectDetail.vue";
import ProfileSettings from "../views/ProfileSettings.vue"; import ProfileSettings from "../views/ProfileSettings.vue";
import ProjectOverview from "../views/ia/ProjectOverview.vue"; import ProjectOverview from "../views/ia/ProjectOverview.vue";
@@ -57,7 +58,6 @@ import { TEXT } from "../locales";
const SYSTEM_PERMISSION_READ = "system:permissions:read"; const SYSTEM_PERMISSION_READ = "system:permissions:read";
const SYSTEM_PERMISSION_PROJECT_CONFIG = "system:permissions:project_config"; const SYSTEM_PERMISSION_PROJECT_CONFIG = "system:permissions:project_config";
const SYSTEM_PERMISSION_MONITORING_METRICS = "system:monitoring:metrics";
const routes: RouteRecordRaw[] = [ const routes: RouteRecordRaw[] = [
{ {
@@ -372,6 +372,20 @@ const routes: RouteRecordRaw[] = [
component: AuditLogs, component: AuditLogs,
meta: { title: TEXT.menu.auditLogs, adminProjectPermission: { module: "audit_export", action: "read" } }, meta: { title: TEXT.menu.auditLogs, adminProjectPermission: { module: "audit_export", action: "read" } },
}, },
{
path: "system-monitoring",
name: "AdminSystemMonitoring",
component: SystemMonitoringPage,
meta: { title: TEXT.menu.systemMonitoring, requiresAdmin: true },
},
{
path: "permission-monitoring",
redirect: "/admin/system-monitoring",
},
{
path: "permissions/monitoring",
redirect: "/admin/system-monitoring",
},
{ {
path: "permissions", path: "permissions",
redirect: "/admin/permissions/system", redirect: "/admin/permissions/system",
@@ -388,12 +402,6 @@ const routes: RouteRecordRaw[] = [
component: PermissionManagement, component: PermissionManagement,
meta: { title: "项目权限配置", systemPermission: SYSTEM_PERMISSION_PROJECT_CONFIG }, meta: { title: "项目权限配置", systemPermission: SYSTEM_PERMISSION_PROJECT_CONFIG },
}, },
{
path: "permissions/monitoring",
name: "AdminPermissionsMonitoring",
component: PermissionManagement,
meta: { title: "权限监控", systemPermission: SYSTEM_PERMISSION_MONITORING_METRICS },
},
], ],
}, },
{ {
@@ -416,7 +424,6 @@ const ADMIN_PROJECT_OPERATION_KEYS: Record<string, Record<"read" | "write", stri
const SYSTEM_PERMISSION_ROLES: Record<string, string[]> = { const SYSTEM_PERMISSION_ROLES: Record<string, string[]> = {
[SYSTEM_PERMISSION_READ]: ["ADMIN", "PM"], [SYSTEM_PERMISSION_READ]: ["ADMIN", "PM"],
[SYSTEM_PERMISSION_PROJECT_CONFIG]: ["ADMIN", "PM"], [SYSTEM_PERMISSION_PROJECT_CONFIG]: ["ADMIN", "PM"],
[SYSTEM_PERMISSION_MONITORING_METRICS]: ["ADMIN", "PM"],
}; };
const getTargetProjectId = (to: any) => { const getTargetProjectId = (to: any) => {
@@ -454,11 +461,11 @@ const ensureAdminProjectAccess = async (to: any, isAdmin: boolean) => {
const permission = to.meta.adminProjectPermission as { module: string; action: "read" | "write" } | undefined; const permission = to.meta.adminProjectPermission as { module: string; action: "read" | "write" } | undefined;
if (!permission) return true; if (!permission) return true;
const routeProjectId = typeof to.params.projectId === "string" ? to.params.projectId : ""; const targetProjectId = getTargetProjectId(to);
if (routeProjectId && studyStore.currentStudy?.id !== routeProjectId) { if (targetProjectId && studyStore.currentStudy?.id !== targetProjectId) {
const [{ data: project }, { data: permissions }] = await Promise.all([ const [{ data: project }, { data: permissions }] = await Promise.all([
fetchStudyDetail(routeProjectId), fetchStudyDetail(targetProjectId),
fetchApiEndpointPermissions(routeProjectId), fetchApiEndpointPermissions(targetProjectId),
]); ]);
studyStore.setCurrentStudy(project); studyStore.setCurrentStudy(project);
studyStore.currentPermissions = permissions; studyStore.currentPermissions = permissions;
+26 -17
View File
@@ -9,10 +9,9 @@ vi.mock("../router", () => ({
vi.mock("../api/authClient", () => ({ vi.mock("../api/authClient", () => ({
extendToken: vi.fn(), extendToken: vi.fn(),
unlockSession: vi.fn(),
})); }));
describe("session manager idle recovery", () => { describe("session manager idle logout", () => {
beforeEach(() => { beforeEach(() => {
vi.resetModules(); vi.resetModules();
vi.useFakeTimers(); vi.useFakeTimers();
@@ -50,37 +49,47 @@ describe("session manager idle recovery", () => {
}); });
}); });
it("locks instead of refreshing activity after long inactivity when user activity resumes", async () => { it("shows a timeout warning one minute before automatic logout", async () => {
vi.setSystemTime(new Date("2026-03-10T01:00:00.000Z")); vi.setSystemTime(new Date("2026-03-10T01:00:00.000Z"));
const { useSessionStore } = await import("../store/session"); const { useSessionStore } = await import("../store/session");
const { IDLE_TIMEOUT_MINUTES, AUTO_LOGOUT_TIMEOUT_HOURS, markUserActive } = await import("./sessionManager"); const { IDLE_TIMEOUT_MINUTES, TIMEOUT_WARNING_SECONDS, initSessionManager } = await import("./sessionManager");
const session = useSessionStore();
const oldTs = Date.now() - ((IDLE_TIMEOUT_MINUTES * 60 - TIMEOUT_WARNING_SECONDS) * 1000);
session.recordUserActivity(oldTs);
initSessionManager();
expect(session.timeoutWarningVisible).toBe(true);
expect(session.timeoutAt).toBe(oldTs + IDLE_TIMEOUT_MINUTES * 60 * 1000);
});
it("logs out after long inactivity when user activity resumes", async () => {
vi.setSystemTime(new Date("2026-03-10T01:00:00.000Z"));
const { useSessionStore } = await import("../store/session");
const router = (await import("../router")).default;
const { IDLE_TIMEOUT_MINUTES, LOGOUT_REASON_TIMEOUT, markUserActive } = await import("./sessionManager");
const session = useSessionStore(); const session = useSessionStore();
const oldTs = Date.now() - (IDLE_TIMEOUT_MINUTES * 60 * 1000 + 1_000); const oldTs = Date.now() - (IDLE_TIMEOUT_MINUTES * 60 * 1000 + 1_000);
session.recordUserActivity(oldTs); session.recordUserActivity(oldTs);
session.recordNetworkActivity(oldTs);
markUserActive(Date.now()); markUserActive(Date.now());
expect(session.locked).toBe(true); expect(window.sessionStorage.getItem("ctms_logout_reason")).toBe(LOGOUT_REASON_TIMEOUT);
expect(session.lockReason).toBe("idle"); expect(router.replace).toHaveBeenCalledWith("/login");
expect(session.lockAutoLogoutDeadlineAt).toBe(oldTs + AUTO_LOGOUT_TIMEOUT_HOURS * 60 * 60 * 1000);
}); });
it("locks instead of refreshing activity after long inactivity when network activity resumes", async () => { it("clears the timeout warning when user activity resumes", async () => {
vi.setSystemTime(new Date("2026-03-10T01:00:00.000Z")); vi.setSystemTime(new Date("2026-03-10T01:00:00.000Z"));
const { useSessionStore } = await import("../store/session"); const { useSessionStore } = await import("../store/session");
const { IDLE_TIMEOUT_MINUTES, AUTO_LOGOUT_TIMEOUT_HOURS, markNetworkActive } = await import("./sessionManager"); const { markUserActive } = await import("./sessionManager");
const session = useSessionStore(); const session = useSessionStore();
const oldTs = Date.now() - (IDLE_TIMEOUT_MINUTES * 60 * 1000 + 1_000);
session.recordUserActivity(oldTs); session.showTimeoutWarning(Date.now() + 60_000);
session.recordNetworkActivity(oldTs);
markNetworkActive(Date.now()); markUserActive(Date.now());
expect(session.locked).toBe(true); expect(session.timeoutWarningVisible).toBe(false);
expect(session.lockReason).toBe("idle"); expect(session.timeoutAt).toBe(0);
expect(session.lockAutoLogoutDeadlineAt).toBe(oldTs + AUTO_LOGOUT_TIMEOUT_HOURS * 60 * 60 * 1000);
}); });
}); });
+22 -109
View File
@@ -2,22 +2,20 @@ import router from "../router";
import { useAuthStore } from "../store/auth"; import { useAuthStore } from "../store/auth";
import { useSessionStore } from "../store/session"; import { useSessionStore } from "../store/session";
import { getToken, setToken, clearToken } from "../utils/auth"; import { getToken, setToken, clearToken } from "../utils/auth";
import { extendToken, getLoginKey, unlockSession } from "../api/authClient"; import { extendToken } from "../api/authClient";
import { encryptLoginPayload } from "../utils/loginCrypto";
import { parseJwtExp } from "./jwt"; import { parseJwtExp } from "./jwt";
export const IDLE_TIMEOUT_MINUTES = 30; export const IDLE_TIMEOUT_MINUTES = 30;
export const AUTO_LOGOUT_TIMEOUT_HOURS = 2; export const TIMEOUT_WARNING_SECONDS = 60;
export const EXTEND_EARLY_SECONDS = 120; export const EXTEND_EARLY_SECONDS = 120;
export const EXTEND_MIN_INTERVAL_SECONDS = 60; export const EXTEND_MIN_INTERVAL_SECONDS = 60;
export const UNLOCK_MAX_ATTEMPTS = 5;
export const LOGOUT_REASON_TIMEOUT = "timeout"; export const LOGOUT_REASON_TIMEOUT = "timeout";
export const LOGOUT_REASON_MANUAL = "manual";
export const LOGOUT_REASON_AUTH_EXPIRED = "auth_expired";
const LOGOUT_REASON_STORAGE_KEY = "ctms_logout_reason"; const LOGOUT_REASON_STORAGE_KEY = "ctms_logout_reason";
type BroadcastMessage = type BroadcastMessage =
| { type: "ACTIVE"; at: number } | { type: "ACTIVE"; at: number }
| { type: "NETWORK"; at: number }
| { type: "LOCK"; reason: string; email?: string; deadlineAt?: number }
| { type: "TOKEN_UPDATED"; token: string } | { type: "TOKEN_UPDATED"; token: string }
| { type: "LOGOUT"; reason?: string }; | { type: "LOGOUT"; reason?: string };
@@ -26,23 +24,15 @@ let extendPromise: Promise<{ token: string | null; authFailed: boolean }> | null
let initialized = false; let initialized = false;
const channel = typeof BroadcastChannel !== "undefined" ? new BroadcastChannel("ctms-auth") : null; const channel = typeof BroadcastChannel !== "undefined" ? new BroadcastChannel("ctms-auth") : null;
const getLastActiveAt = (session: ReturnType<typeof useSessionStore>) => const getTimeoutAt = (session: ReturnType<typeof useSessionStore>) =>
Math.max(session.lastUserActiveAt, session.lastNetworkActiveAt); session.lastUserActiveAt + IDLE_TIMEOUT_MINUTES * 60 * 1000;
const reconcileSessionState = (now: number = Date.now()) => { const reconcileSessionState = (now: number = Date.now()) => {
const session = useSessionStore(); const session = useSessionStore();
if (session.locked) return false; if (now >= getTimeoutAt(session)) {
const idleTimeoutMs = IDLE_TIMEOUT_MINUTES * 60 * 1000;
const autoLogoutMs = AUTO_LOGOUT_TIMEOUT_HOURS * 60 * 60 * 1000;
const lastActiveAt = getLastActiveAt(session);
if (now - lastActiveAt > autoLogoutMs) {
forceLogout(LOGOUT_REASON_TIMEOUT); forceLogout(LOGOUT_REASON_TIMEOUT);
return false; return false;
} }
if (now - lastActiveAt > idleTimeoutMs) {
lockSession("idle");
return false;
}
return true; return true;
}; };
@@ -61,24 +51,13 @@ const handleBroadcast = (message: BroadcastMessage) => {
const session = useSessionStore(); const session = useSessionStore();
const auth = useAuthStore(); const auth = useAuthStore();
if (message.type === "ACTIVE") { if (message.type === "ACTIVE") {
if (session.locked) return;
session.recordUserActivity(message.at); session.recordUserActivity(message.at);
scheduleIdleCheck(); scheduleIdleCheck();
} }
if (message.type === "NETWORK") {
if (session.locked) return;
session.recordNetworkActivity(message.at);
scheduleIdleCheck();
}
if (message.type === "LOCK") {
const reason = message.reason as "idle" | "extend_failed" | "token_invalid";
session.lock(reason || "idle", message.email, message.deadlineAt);
scheduleIdleCheck();
}
if (message.type === "TOKEN_UPDATED") { if (message.type === "TOKEN_UPDATED") {
auth.setToken(message.token); auth.setToken(message.token);
setToken(message.token); setToken(message.token);
session.unlock(); session.resetActivity();
} }
if (message.type === "LOGOUT") { if (message.type === "LOGOUT") {
performLogout(message.reason); performLogout(message.reason);
@@ -91,39 +70,22 @@ const scheduleIdleCheck = () => {
} }
const session = useSessionStore(); const session = useSessionStore();
const now = Date.now(); const now = Date.now();
if (session.locked) { const timeoutAt = getTimeoutAt(session);
const remain = (session.lockAutoLogoutDeadlineAt || 0) - now; const warningAt = timeoutAt - TIMEOUT_WARNING_SECONDS * 1000;
if (remain <= 0) { if (timeoutAt <= now) {
checkIdle();
return;
}
idleTimer = window.setTimeout(checkIdle, remain);
return;
}
const idleTimeoutMs = IDLE_TIMEOUT_MINUTES * 60 * 1000;
const autoLogoutMs = AUTO_LOGOUT_TIMEOUT_HOURS * 60 * 60 * 1000;
const lastActiveAt = getLastActiveAt(session);
const idleRemain = lastActiveAt + idleTimeoutMs - now;
const logoutRemain = lastActiveAt + autoLogoutMs - now;
const nextCheck = Math.min(idleRemain, logoutRemain);
if (nextCheck <= 0) {
checkIdle(); checkIdle();
return; return;
} }
idleTimer = window.setTimeout(checkIdle, nextCheck); if (warningAt <= now) {
session.showTimeoutWarning(timeoutAt);
idleTimer = window.setTimeout(checkIdle, timeoutAt - now);
return;
}
session.clearTimeoutWarning();
idleTimer = window.setTimeout(checkIdle, warningAt - now);
}; };
const checkIdle = () => { const checkIdle = () => {
const session = useSessionStore();
if (session.locked) {
const deadlineAt = session.lockAutoLogoutDeadlineAt || 0;
if (deadlineAt > 0 && Date.now() >= deadlineAt) {
forceLogout(LOGOUT_REASON_TIMEOUT);
return;
}
scheduleIdleCheck();
return;
}
if (!reconcileSessionState(Date.now())) return; if (!reconcileSessionState(Date.now())) return;
scheduleIdleCheck(); scheduleIdleCheck();
}; };
@@ -166,34 +128,12 @@ export const initSessionManager = () => {
export const markUserActive = (at: number = Date.now()) => { export const markUserActive = (at: number = Date.now()) => {
const session = useSessionStore(); const session = useSessionStore();
if (session.locked) return;
if (!reconcileSessionState(at)) return; if (!reconcileSessionState(at)) return;
session.recordUserActivity(at); session.recordUserActivity(at);
broadcast({ type: "ACTIVE", at }); broadcast({ type: "ACTIVE", at });
scheduleIdleCheck(); scheduleIdleCheck();
}; };
export const markNetworkActive = (at: number = Date.now()) => {
const session = useSessionStore();
if (session.locked) return;
if (!reconcileSessionState(at)) return;
session.recordNetworkActivity(at);
broadcast({ type: "NETWORK", at });
scheduleIdleCheck();
};
export const lockSession = (reason: "idle" | "extend_failed" | "token_invalid") => {
const session = useSessionStore();
const auth = useAuthStore();
if (session.locked) return;
const email = auth.user?.email || "";
const autoLogoutMs = AUTO_LOGOUT_TIMEOUT_HOURS * 60 * 60 * 1000;
const deadlineAt = getLastActiveAt(session) + autoLogoutMs;
session.lock(reason, email, deadlineAt);
broadcast({ type: "LOCK", reason, email, deadlineAt });
scheduleIdleCheck();
};
const setLogoutReason = (reason?: string) => { const setLogoutReason = (reason?: string) => {
try { try {
if (!reason) { if (!reason) {
@@ -221,7 +161,7 @@ const performLogout = (reason?: string) => {
const auth = useAuthStore(); const auth = useAuthStore();
const session = useSessionStore(); const session = useSessionStore();
auth.logout(); auth.logout();
session.unlock(); session.resetActivity();
clearToken(); clearToken();
router.replace("/login"); router.replace("/login");
}; };
@@ -259,10 +199,10 @@ export const extendAccessToken = async (reason: "early" | "response-401") => {
} catch (err: any) { } catch (err: any) {
const status = err?.response?.status; const status = err?.response?.status;
if (status === 401) { if (status === 401) {
lockSession("extend_failed"); forceLogout(LOGOUT_REASON_AUTH_EXPIRED);
authFailed = true; authFailed = true;
} else if (status === 403) { } else if (status === 403) {
forceLogout(); forceLogout(LOGOUT_REASON_AUTH_EXPIRED);
authFailed = true; authFailed = true;
} }
return { token: null, authFailed }; return { token: null, authFailed };
@@ -278,40 +218,13 @@ export const startTokenKeepAlive = () => {
const token = getToken(); const token = getToken();
if (!token) return; if (!token) return;
const session = useSessionStore(); const session = useSessionStore();
if (session.locked) return;
const expAt = parseJwtExp(token); const expAt = parseJwtExp(token);
if (!expAt) return; if (!expAt) return;
const remaining = expAt - Date.now(); const remaining = expAt - Date.now();
const timeoutMs = IDLE_TIMEOUT_MINUTES * 60 * 1000; const timeoutMs = IDLE_TIMEOUT_MINUTES * 60 * 1000;
const active = Date.now() - getLastActiveAt(session) < timeoutMs; const active = Date.now() - session.lastUserActiveAt < timeoutMs;
if (active && remaining < EXTEND_EARLY_SECONDS * 1000) { if (active && remaining < EXTEND_EARLY_SECONDS * 1000) {
void extendAccessToken("early"); void extendAccessToken("early");
} }
}, 10000); }, 10000);
}; };
export const unlockWithPassword = async (email: string, password: string) => {
const session = useSessionStore();
const auth = useAuthStore();
const { data: loginKey } = await getLoginKey();
const ciphertext = await encryptLoginPayload(loginKey.public_key, {
email,
password,
challenge: loginKey.challenge,
});
const { data } = await unlockSession({
key_id: loginKey.key_id,
challenge: loginKey.challenge,
ciphertext,
});
updateToken(data.accessToken);
session.unlock();
try {
await auth.fetchMe();
} catch {
// token已更新但用户信息拉取失败时,避免界面进入无角色状态
forceLogout();
throw new Error("无法获取用户信息,请重新登录");
}
markUserActive();
};
+8 -5
View File
@@ -82,18 +82,21 @@ describe("auth store logout", () => {
}); });
}); });
it("clears session lock state when logging out", async () => { it("resets session activity state when logging out", async () => {
const { useAuthStore } = await import("./auth"); const { useAuthStore } = await import("./auth");
const { useSessionStore } = await import("./session"); const { useSessionStore } = await import("./session");
const session = useSessionStore(); const session = useSessionStore();
const auth = useAuthStore(); const auth = useAuthStore();
const staleTs = Date.now() - 60_000;
session.lock("token_invalid", "user@example.com", Date.now() + 60_000); session.recordUserActivity(staleTs);
session.setLastExtendAt(staleTs);
session.showTimeoutWarning(staleTs + 30_000);
auth.logout(); auth.logout();
expect(session.locked).toBe(false); expect(session.lastUserActiveAt).toBeGreaterThan(staleTs);
expect(session.lockReason).toBeNull(); expect(session.lastExtendAt).toBe(0);
expect(session.lockEmail).toBe(""); expect(session.timeoutWarningVisible).toBe(false);
}); });
it("uses encrypted login by default outside a secure browser context", async () => { it("uses encrypted login by default outside a secure browser context", async () => {

Some files were not shown because too many files have changed in this diff Show More