106 lines
2.9 KiB
Python
106 lines
2.9 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""Protokoll von Anmeldungen und Verwaltungsaktionen.
|
|
|
|
Grundsatz: keine Inhalte, keine E-Mail-Adressen, keine IP-Adressen. Personen
|
|
werden nur über ihre interne Konto-ID referenziert; wird ein Konto gelöscht,
|
|
bleibt der Eintrag ohne Bezug zur Person stehen.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime, timedelta
|
|
from typing import Any
|
|
|
|
from sqlalchemy import delete
|
|
from sqlalchemy.orm import Session
|
|
|
|
from surveybox.models import AuditEvent
|
|
|
|
# Erlaubte Aktionen; die Oberfläche übersetzt sie.
|
|
ACTIONS = {
|
|
"auth.login",
|
|
"auth.login_failed",
|
|
"auth.locked",
|
|
"auth.logout",
|
|
"auth.mfa_failed",
|
|
"auth.recovery_code_used",
|
|
"account.password_changed",
|
|
"account.totp_enabled",
|
|
"account.totp_disabled",
|
|
"account.recovery_codes_generated",
|
|
"account.sessions_revoked",
|
|
"account.setup_completed",
|
|
"admin.user_created",
|
|
"admin.user_updated",
|
|
"admin.link_created",
|
|
"admin.sessions_revoked",
|
|
"admin.mfa_reset",
|
|
"account.passkey_added",
|
|
"account.passkey_removed",
|
|
"auth.passkey_failed",
|
|
"admin.group_created",
|
|
"admin.group_updated",
|
|
"admin.group_deleted",
|
|
"admin.group_member_added",
|
|
"admin.group_member_removed",
|
|
"form.created",
|
|
"form.deleted",
|
|
"form.status_changed",
|
|
"form.copied",
|
|
"form.team_changed",
|
|
"admin.settings_changed",
|
|
"admin.group_role_changed",
|
|
"admin.mail_settings_changed",
|
|
"admin.mail_test",
|
|
"account.reset_requested",
|
|
"account.notifications_changed",
|
|
"account.registered",
|
|
"admin.registration_changed",
|
|
"admin.privacy_changed",
|
|
"form.retention_applied",
|
|
"account.deleted",
|
|
"account.exported",
|
|
"admin.user_exported",
|
|
"admin.guest_exported",
|
|
"guest.exported",
|
|
"admin.branding_changed",
|
|
"admin.appearance_changed",
|
|
"admin.mail_template_changed",
|
|
"form.exported",
|
|
"response.deleted",
|
|
"response.edited",
|
|
"guest.deleted",
|
|
"form.history_restored",
|
|
"form.trash_restored",
|
|
"admin.history_changed",
|
|
"admin.user_unlocked",
|
|
}
|
|
|
|
|
|
def record(
|
|
db: Session,
|
|
action: str,
|
|
*,
|
|
actor_id: int | None = None,
|
|
target_type: str | None = None,
|
|
target_id: str | int | None = None,
|
|
details: dict[str, Any] | None = None,
|
|
) -> None:
|
|
if action not in ACTIONS:
|
|
raise ValueError(f"Unbekannte Protokollaktion: {action}")
|
|
db.add(
|
|
AuditEvent(
|
|
action=action,
|
|
actor_id=actor_id,
|
|
target_type=target_type,
|
|
target_id=None if target_id is None else str(target_id),
|
|
details=details,
|
|
)
|
|
)
|
|
|
|
|
|
def purge_older_than(db: Session, days: int) -> int:
|
|
cutoff = datetime.now(UTC) - timedelta(days=days)
|
|
result = db.execute(delete(AuditEvent).where(AuditEvent.occurred_at < cutoff))
|
|
return int(result.rowcount or 0) # type: ignore[attr-defined]
|