From cc793612f096afdf42a12974f4abc533389166a3 Mon Sep 17 00:00:00 2001 From: Prakhar Pratyush Date: Wed, 17 Apr 2024 15:27:49 +0530 Subject: [PATCH] export: Create REALM_EXPORTED audit log for exports via shell. Earlier, we were creating RealmAuditLog with REALM_EXPORTED event_type when export of public data took place via organization settings panel. We were not creating the audit log when the export was executed via shell i.e './manage.py export'. This commit creates the audit log in that case too. It will help during import to distinguish readily between imports from another Zulip server vs imports from another product. --- zerver/lib/export.py | 8 +++++++- zerver/management/commands/export.py | 10 +++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/zerver/lib/export.py b/zerver/lib/export.py index 533d2a3809..997f2cf4da 100644 --- a/zerver/lib/export.py +++ b/zerver/lib/export.py @@ -2418,9 +2418,15 @@ def export_realm_wrapper( def get_realm_exports_serialized(user: UserProfile) -> List[Dict[str, Any]]: + # Exclude exports made via shell. 'acting_user=None', since they + # aren't supported in the current API format. + # + # TODO: We should return those via the API as well, with an + # appropriate way to express for who issued them; this requires an + # API change. all_exports = RealmAuditLog.objects.filter( realm=user.realm, event_type=RealmAuditLog.REALM_EXPORTED - ) + ).exclude(acting_user=None) exports_dict = {} for export in all_exports: export_url = None diff --git a/zerver/management/commands/export.py b/zerver/management/commands/export.py index ac2196d78b..20dd19e7ed 100644 --- a/zerver/management/commands/export.py +++ b/zerver/management/commands/export.py @@ -5,12 +5,13 @@ from typing import Any from django.conf import settings from django.core.management.base import CommandError +from django.utils.timezone import now as timezone_now from typing_extensions import override from zerver.actions.realm_settings import do_deactivate_realm from zerver.lib.export import export_realm_wrapper from zerver.lib.management import ZulipBaseCommand -from zerver.models import Message, Reaction, UserProfile +from zerver.models import Message, Reaction, RealmAuditLog, UserProfile class Command(ZulipBaseCommand): @@ -206,6 +207,13 @@ class Command(ZulipBaseCommand): def percent_callback(bytes_transferred: Any) -> None: print(end=".", flush=True) + RealmAuditLog.objects.create( + acting_user=None, + realm=realm, + event_type=RealmAuditLog.REALM_EXPORTED, + event_time=timezone_now(), + ) + # Allows us to trigger exports separately from command line argument parsing export_realm_wrapper( realm=realm,