realm_export: Add a RealmAuditLog entry for delete operation.

This commit adds a RealmAuditLog entry for when someone deletes
an export.

This helps to track the acting_user.
This commit is contained in:
Prakhar Pratyush 2024-10-05 01:41:05 +05:30 committed by Tim Abbott
parent 07dcee36b2
commit cf879a5f48
4 changed files with 21 additions and 3 deletions

View File

@ -1,8 +1,10 @@
from django.db import transaction
from django.utils.timezone import now as timezone_now
from zerver.lib.export import get_realm_exports_serialized
from zerver.lib.upload import delete_export_tarball
from zerver.models import Realm, RealmExport
from zerver.models import Realm, RealmAuditLog, RealmExport, UserProfile
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.tornado.django_api import send_event_on_commit
@ -11,7 +13,8 @@ def notify_realm_export(realm: Realm) -> None:
send_event_on_commit(realm, event, realm.get_human_admin_users().values_list("id", flat=True))
def do_delete_realm_export(export_row: RealmExport) -> None:
@transaction.atomic(durable=True)
def do_delete_realm_export(export_row: RealmExport, acting_user: UserProfile) -> None:
export_path = export_row.export_path
assert export_path is not None
@ -21,3 +24,11 @@ def do_delete_realm_export(export_row: RealmExport) -> None:
export_row.date_deleted = timezone_now()
export_row.save(update_fields=["status", "date_deleted"])
notify_realm_export(export_row.realm)
RealmAuditLog.objects.create(
acting_user=acting_user,
realm=export_row.realm,
event_type=AuditLogEventType.REALM_EXPORT_DELETED,
event_time=export_row.date_deleted,
extra_data={"realm_export_id": export_row.id},
)

View File

@ -66,6 +66,7 @@ class AuditLogEventType(IntEnum):
REALM_EMOJI_REMOVED = 227
REALM_LINKIFIERS_REORDERED = 228
REALM_IMPORTED = 229
REALM_EXPORT_DELETED = 230
SUBSCRIPTION_CREATED = 301
SUBSCRIPTION_ACTIVATED = 302

View File

@ -3422,6 +3422,12 @@ class NormalActionsTest(BaseAction):
has_failed_timestamp=False,
)
audit_log = RealmAuditLog.objects.last()
assert audit_log is not None
self.assertEqual(audit_log.event_type, AuditLogEventType.REALM_EXPORT_DELETED)
self.assertEqual(audit_log.acting_user, self.user_profile)
self.assertEqual(audit_log.extra_data["realm_export_id"], export_row_id)
def test_notify_realm_export_on_failure(self) -> None:
do_change_user_role(
self.user_profile, UserProfile.ROLE_REALM_ADMINISTRATOR, acting_user=None

View File

@ -106,7 +106,7 @@ def delete_realm_export(request: HttpRequest, user: UserProfile, export_id: int)
raise JsonableError(_("Export failed, nothing to delete"))
if export_row.status in [RealmExport.REQUESTED, RealmExport.STARTED]:
raise JsonableError(_("Export still in progress"))
do_delete_realm_export(export_row)
do_delete_realm_export(export_row, user)
return json_success(request)