This commit is contained in:
gr
2026-09-28 10:02:07 +08:00
parent d79a2fa489
commit b112dcc6be
303 changed files with 5039 additions and 49659 deletions
+34
View File
@@ -140,6 +140,19 @@ def pbkdf2_sha256(
return derived.to_bytes(32, "big")
def _sql_like_escape(value: str) -> str:
return (
str(value or "")
.replace("\\", "\\\\")
.replace("%", "\\%")
.replace("_", "\\_")
)
def _sql_like_prefix(value: str) -> str:
return f"{_sql_like_escape(value)}%"
def now_text() -> str:
return datetime.now().astimezone().isoformat(timespec="seconds")
@@ -1446,6 +1459,27 @@ class Database:
).fetchall()
return [{key: row[key] for key in row.keys()} for row in rows]
def audit_entries_for_user(
self, user_id: int, action_prefix: str, *, detail_contains: str = "", limit: int = 20
) -> list[dict[str, Any]]:
"""某个账号在某类动作上的审计记录,用于桌面端展示订单查询留痕。"""
limit = max(1, min(int(limit or 20), 200))
prefix = str(action_prefix or "").strip()
needle = str(detail_contains or "").strip()
with self.connect() as db:
rows = db.execute(
"""SELECT id, action, detail, ip_address, created_at FROM audit_log
WHERE user_id=? AND action LIKE ? ESCAPE '\\' AND detail LIKE ? ESCAPE '\\'
ORDER BY id DESC LIMIT ?""",
(
int(user_id),
_sql_like_prefix(prefix),
f"%{_sql_like_escape(needle)}%" if needle else "%",
limit,
),
).fetchall()
return [{key: row[key] for key in row.keys()} for row in rows]
def model_call_stats(
self, days: int = 7, tenant_id: Any = "default"
) -> dict[str, Any]: