feat(admin): add admin_actions audit table and log/list functions

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Colin Maudry
2026-07-03 09:38:33 +02:00
parent 0209ec0d5c
commit fb62c28e10
6 changed files with 91 additions and 0 deletions
View File
+26
View File
@@ -0,0 +1,26 @@
import sqlite3
from datetime import datetime, timezone
from src.auth.db import get_conn
def _now() -> str:
return datetime.now(timezone.utc).isoformat()
def log_action(
admin_email: str, action: str, target_user_id: int | None, details: str | None
) -> None:
get_conn().execute(
"INSERT INTO admin_actions (admin_email, action, target_user_id, details, created_at) "
"VALUES (?, ?, ?, ?, ?)",
(admin_email, action, target_user_id, details, _now()),
)
def list_actions(limit: int = 200) -> list[sqlite3.Row]:
return (
get_conn()
.execute("SELECT * FROM admin_actions ORDER BY id DESC LIMIT ?", (limit,))
.fetchall()
)
+10
View File
@@ -32,6 +32,16 @@ _MIGRATIONS: list[tuple[str, str]] = [
"0005_rename_votes_credited_until_to_votes_last_credited_at",
"ALTER TABLE subscriptions RENAME COLUMN votes_credited_until TO votes_last_credited_at",
),
(
"0006_create_admin_actions",
"CREATE TABLE IF NOT EXISTS admin_actions ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"admin_email TEXT NOT NULL, "
"action TEXT NOT NULL, "
"target_user_id INTEGER, "
"details TEXT, "
"created_at TEXT NOT NULL)",
),
]