aee0f09db8
- Datafabrik: Dockerfile fix, agentorkestrering fungerar - Vision: Identify-modell, FAISS, OCR alla testade - API: Alla 7 integrationstester passerade - Upplösare: Entitetsupplösning verifierad
118 lines
3.5 KiB
Python
118 lines
3.5 KiB
Python
"""
|
|
LandveX Admin Backend — Audit Log API Routes
|
|
Endpoints for querying audit logs
|
|
"""
|
|
from typing import Optional, Dict, Any
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Depends, Query, HTTPException, status
|
|
|
|
from app.core.security import get_current_user, require_admin
|
|
from app.api.dependencies import get_audit_service, get_request_context
|
|
from app.services.audit_service import AuditService
|
|
from app.schemas.common import AuditLogResponse
|
|
|
|
|
|
router = APIRouter(prefix="/audit-logs", tags=["audit"])
|
|
|
|
|
|
@router.get(
|
|
"",
|
|
summary="List audit logs",
|
|
description="Query audit logs with filtering"
|
|
)
|
|
async def list_audit_logs(
|
|
limit: int = Query(50, ge=1, le=200),
|
|
offset: int = Query(0, ge=0),
|
|
entity_type: Optional[str] = Query(None, description="Filter by entity type"),
|
|
entity_id: Optional[str] = Query(None, description="Filter by entity ID"),
|
|
actor_id: Optional[str] = Query(None, description="Filter by actor"),
|
|
action: Optional[str] = Query(None, description="Filter by action type"),
|
|
audit_service: AuditService = Depends(get_audit_service),
|
|
current_user: Dict[str, Any] = Depends(require_admin)
|
|
):
|
|
"""List audit logs with filtering.
|
|
|
|
Admin access required.
|
|
"""
|
|
logs = await audit_service.get_recent(limit=limit)
|
|
|
|
# Apply filters in memory (for small datasets)
|
|
# In production, add database-level filtering
|
|
if entity_type:
|
|
logs = [l for l in logs if l.entity_type == entity_type]
|
|
if entity_id:
|
|
logs = [l for l in logs if l.entity_id == entity_id]
|
|
if actor_id:
|
|
logs = [l for l in logs if l.actor_id == actor_id]
|
|
if action:
|
|
from app.models.audit_log import AuditAction
|
|
try:
|
|
action_enum = AuditAction(action)
|
|
logs = [l for l in logs if l.action == action_enum]
|
|
except ValueError:
|
|
pass
|
|
|
|
return {
|
|
"items": [log.to_dict() for log in logs],
|
|
"total": len(logs),
|
|
"limit": limit,
|
|
"offset": offset
|
|
}
|
|
|
|
|
|
@router.get(
|
|
"/entity/{entity_type}/{entity_id}",
|
|
summary="Get entity audit history",
|
|
description="Get all audit logs for a specific entity"
|
|
)
|
|
async def get_entity_audit_logs(
|
|
entity_type: str,
|
|
entity_id: str,
|
|
limit: int = Query(100, ge=1, le=500),
|
|
offset: int = Query(0, ge=0),
|
|
audit_service: AuditService = Depends(get_audit_service),
|
|
current_user: Dict[str, Any] = Depends(require_admin)
|
|
):
|
|
"""Get audit logs for a specific entity."""
|
|
logs, total = await audit_service.get_by_entity(
|
|
entity_type=entity_type,
|
|
entity_id=entity_id,
|
|
limit=limit,
|
|
offset=offset
|
|
)
|
|
|
|
return {
|
|
"items": [log.to_dict() for log in logs],
|
|
"total": total,
|
|
"limit": limit,
|
|
"offset": offset
|
|
}
|
|
|
|
|
|
@router.get(
|
|
"/actor/{actor_id}",
|
|
summary="Get actor audit history",
|
|
description="Get all audit logs for a specific actor (user)"
|
|
)
|
|
async def get_actor_audit_logs(
|
|
actor_id: str,
|
|
limit: int = Query(100, ge=1, le=500),
|
|
offset: int = Query(0, ge=0),
|
|
audit_service: AuditService = Depends(get_audit_service),
|
|
current_user: Dict[str, Any] = Depends(require_admin)
|
|
):
|
|
"""Get audit logs for a specific actor."""
|
|
logs, total = await audit_service.get_by_actor(
|
|
actor_id=actor_id,
|
|
limit=limit,
|
|
offset=offset
|
|
)
|
|
|
|
return {
|
|
"items": [log.to_dict() for log in logs],
|
|
"total": total,
|
|
"limit": limit,
|
|
"offset": offset
|
|
}
|