File size: 783 Bytes
48bb2da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import json
import logging
from datetime import datetime, timezone

from db.init import get_conn

log = logging.getLogger("audit")


def audit(admin_id: str, action: str, target_user_id=None, details=None):
    try:
        with get_conn() as conn:
            conn.execute(
                """INSERT INTO admin_audit(admin_id, action, target_user_id,
                                           timestamp, details)
                   VALUES (?, ?, ?, ?, ?)""",
                (
                    str(admin_id),
                    action,
                    target_user_id,
                    datetime.now(timezone.utc).isoformat(),
                    json.dumps(details or {}),
                ),
            )
    except Exception:
        log.exception("audit write failed")