aee0f09db8
- Datafabrik: Dockerfile fix, agentorkestrering fungerar - Vision: Identify-modell, FAISS, OCR alla testade - API: Alla 7 integrationstester passerade - Upplösare: Entitetsupplösning verifierad
96 lines
3.0 KiB
Python
96 lines
3.0 KiB
Python
"""
|
|
Audit Log API Routes för LandveX Admin Backend.
|
|
Endpoints för att söka och granska audit-loggar.
|
|
"""
|
|
from typing import Optional
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
|
|
from app.dependencies import get_current_user, require_admin
|
|
from app.database import get_db
|
|
from app.schemas import AuditLogListOut
|
|
from app.services.audit_service import AuditService
|
|
from app.models import User
|
|
|
|
router = APIRouter(prefix="/audit-logs", tags=["Audit"])
|
|
|
|
|
|
@router.get("", response_model=AuditLogListOut)
|
|
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),
|
|
entity_id: Optional[str] = Query(None),
|
|
actor_id: Optional[str] = Query(None),
|
|
action: Optional[str] = Query(None),
|
|
current_user: User = Depends(require_admin),
|
|
db=Depends(get_db),
|
|
):
|
|
"""Lista audit-loggar med filtrering. Kräver admin."""
|
|
audit_service = AuditService(db)
|
|
logs = await audit_service.get_recent(limit=limit + offset)
|
|
|
|
# Filtrera i minnet (för små datamängder)
|
|
# I produktion: lägg till databasfiltrering
|
|
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 import AuditAction
|
|
try:
|
|
action_enum = AuditAction(action)
|
|
logs = [l for l in logs if l.action == action_enum]
|
|
except ValueError:
|
|
pass
|
|
|
|
# Apply offset after filtering
|
|
total = len(logs)
|
|
logs = logs[offset:offset + limit]
|
|
|
|
return AuditLogListOut(
|
|
items=[log.to_dict() for log in logs],
|
|
total=total, limit=limit, offset=offset
|
|
)
|
|
|
|
|
|
@router.get("/entity/{entity_type}/{entity_id}", response_model=AuditLogListOut)
|
|
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),
|
|
current_user: User = Depends(require_admin),
|
|
db=Depends(get_db),
|
|
):
|
|
"""Hämta alla audit-loggar för en specifik entitet."""
|
|
audit_service = AuditService(db)
|
|
logs, total = await audit_service.get_by_entity(
|
|
entity_type=entity_type, entity_id=entity_id, limit=limit, offset=offset
|
|
)
|
|
return AuditLogListOut(
|
|
items=[log.to_dict() for log in logs],
|
|
total=total, limit=limit, offset=offset
|
|
)
|
|
|
|
|
|
@router.get("/actor/{actor_id}", response_model=AuditLogListOut)
|
|
async def get_actor_audit_logs(
|
|
actor_id: str,
|
|
limit: int = Query(100, ge=1, le=500),
|
|
offset: int = Query(0, ge=0),
|
|
current_user: User = Depends(require_admin),
|
|
db=Depends(get_db),
|
|
):
|
|
"""Hämta alla audit-loggar för en specifik användare (actor)."""
|
|
audit_service = AuditService(db)
|
|
logs, total = await audit_service.get_by_actor(
|
|
actor_id=actor_id, limit=limit, offset=offset
|
|
)
|
|
return AuditLogListOut(
|
|
items=[log.to_dict() for log in logs],
|
|
total=total, limit=limit, offset=offset
|
|
)
|