Squashed 'core/' content from commit 92ec910

git-subtree-dir: core
git-subtree-split: 92ec910a132e379a3a6e442a75bcb07cac0f0010
This commit is contained in:
Du Wenbo
2026-04-04 18:17:10 +08:00
commit 026c837b91
227 changed files with 39179 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.user import AuditLog
async def log_audit(
db: AsyncSession,
user_id: int | None,
action: str,
resource: str,
detail: str = "",
ip_address: str | None = None,
):
"""Write an audit log entry.
Args:
db: async database session (must be flushed/committed by caller)
user_id: ID of the acting user (None for system actions)
action: one of login, create, update, delete, export, view,
acknowledge, resolve
resource: one of user, device, alarm, report, system, auth
detail: human-readable description
ip_address: client IP if available
"""
entry = AuditLog(
user_id=user_id,
action=action,
resource=resource,
detail=detail,
ip_address=ip_address,
)
db.add(entry)
# Don't flush here — let the caller's transaction handle it