Kairo-Jinkendo/backend/services/audit.py
Lars 0baa5952bc
All checks were successful
Deploy Development / deploy (push) Successful in 47s
Test Suite / pytest-backend (push) Successful in 4m38s
Test Suite / lint-backend (push) Successful in 2s
Test Suite / compose-smoke (push) Has been skipped
Test Suite / k6 /api/health Baseline (push) Successful in 18s
Test Suite / playwright-smoke (push) Successful in 13s
fix(AP1.7b): Audit user_id NULL bei Service-Token statt leerer UUID
Operational convert-backlog und patch-recurring nutzen ctx.user_id or None;
log_audit normalisiert leere Strings fuer audit_log.user_id.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-28 09:33:35 +02:00

31 lines
758 B
Python

"""Minimal audit logging for critical auth actions (AP0.2)."""
from __future__ import annotations
import json
from typing import Any, Optional
from db import get_connection
def log_audit(
action: str,
*,
user_id: Optional[str] = None,
tenant_id: Optional[str] = None,
details: Optional[dict[str, Any]] = None,
) -> None:
conn = get_connection()
try:
with conn.cursor() as cur:
cur.execute(
"""
INSERT INTO audit_log (action, user_id, tenant_id, details)
VALUES (%s, %s, %s, %s::jsonb)
""",
(action, user_id or None, tenant_id, json.dumps(details or {})),
)
conn.commit()
finally:
conn.close()