feat(admin): 完善审计访问上下文与后台布局
This commit is contained in:
@@ -4,8 +4,10 @@ import uuid
|
||||
from typing import Sequence
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.request_context import get_request_audit_context
|
||||
from app.models.audit_log import AuditLog
|
||||
|
||||
|
||||
@@ -19,8 +21,16 @@ async def log_action(
|
||||
detail: str | None,
|
||||
operator_id: uuid.UUID,
|
||||
operator_role: str,
|
||||
client_ip: str | None = None,
|
||||
user_agent: str | None = None,
|
||||
client_type: str | None = None,
|
||||
client_version: str | None = None,
|
||||
client_platform: str | None = None,
|
||||
build_channel: str | None = None,
|
||||
build_commit: str | None = None,
|
||||
auto_commit: bool = True,
|
||||
) -> AuditLog:
|
||||
request_context = get_request_audit_context()
|
||||
log = AuditLog(
|
||||
study_id=study_id,
|
||||
entity_type=entity_type,
|
||||
@@ -29,6 +39,13 @@ async def log_action(
|
||||
detail=detail,
|
||||
operator_id=operator_id,
|
||||
operator_role=operator_role,
|
||||
client_ip=client_ip if client_ip is not None else (request_context.client_ip if request_context else None),
|
||||
user_agent=user_agent if user_agent is not None else (request_context.user_agent if request_context else None),
|
||||
client_type=client_type if client_type is not None else (request_context.client_type if request_context else None),
|
||||
client_version=client_version if client_version is not None else (request_context.client_version if request_context else None),
|
||||
client_platform=client_platform if client_platform is not None else (request_context.client_platform if request_context else None),
|
||||
build_channel=build_channel if build_channel is not None else (request_context.build_channel if request_context else None),
|
||||
build_commit=build_commit if build_commit is not None else (request_context.build_commit if request_context else None),
|
||||
)
|
||||
db.add(log)
|
||||
if auto_commit:
|
||||
@@ -47,6 +64,10 @@ async def list_logs(
|
||||
entity_id: uuid.UUID | None = None,
|
||||
action: str | None = None,
|
||||
operator_id: uuid.UUID | None = None,
|
||||
client_ip: str | None = None,
|
||||
client_type: str | None = None,
|
||||
start_time=None,
|
||||
end_time=None,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
) -> Sequence[AuditLog]:
|
||||
@@ -59,6 +80,24 @@ async def list_logs(
|
||||
stmt = stmt.where(AuditLog.action == action)
|
||||
if operator_id:
|
||||
stmt = stmt.where(AuditLog.operator_id == operator_id)
|
||||
if client_ip:
|
||||
stmt = stmt.where(AuditLog.client_ip.ilike(f"%{client_ip.strip()}%"))
|
||||
if client_type:
|
||||
normalized_client_type = client_type.strip().lower()
|
||||
if normalized_client_type == "unknown":
|
||||
stmt = stmt.where(
|
||||
or_(
|
||||
AuditLog.client_type.is_(None),
|
||||
AuditLog.client_type == "",
|
||||
~AuditLog.client_type.in_(("web", "desktop")),
|
||||
)
|
||||
)
|
||||
else:
|
||||
stmt = stmt.where(AuditLog.client_type == normalized_client_type)
|
||||
if start_time:
|
||||
stmt = stmt.where(AuditLog.created_at >= start_time)
|
||||
if end_time:
|
||||
stmt = stmt.where(AuditLog.created_at <= end_time)
|
||||
stmt = stmt.order_by(AuditLog.created_at.desc()).offset(skip).limit(limit)
|
||||
result = await db.execute(stmt)
|
||||
return result.scalars().all()
|
||||
|
||||
Reference in New Issue
Block a user