audit-log: Move realm event types to AuditLogEventType enum.

Event types moved: REALM_CREATED, REALM_DEFAULT_USER_SETTINGS_CHANGED
REALM_ORG_TYPE_CHANGED, REALM_DOMAIN_ADDED, REALM_DOMAIN_CHANGED
REALM_DOMAIN_REMOVED, REALM_PLAYGROUND_ADDED, REALM_PLAYGROUND_REMOVED
REALM_LINKIFIER_ADDED, REALM_LINKIFIER_CHANGED, REALM_LINKIFIER_REMOVED
REALM_EMOJI_ADDED, REALM_EMOJI_REMOVED, REALM_LINKIFIERS_REORDERED
REALM_IMPORTED
This commit is contained in:
Lauryn Menard 2024-09-03 16:46:18 +02:00 committed by Tim Abbott
parent d2c32f23db
commit 56c8cbde1e
18 changed files with 71 additions and 59 deletions

View File

@ -28,7 +28,7 @@ from corporate.views.support import get_plan_type_string
from zerver.decorator import require_server_admin
from zerver.lib.typed_endpoint import typed_endpoint_without_parameters
from zerver.models import Realm
from zerver.models.realm_audit_logs import RealmAuditLog
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.realms import get_org_type_display_name
@ -187,7 +187,7 @@ def realm_summary_table() -> str:
"active_users_audit_end_time": COUNT_STATS[
"active_users_audit:is_bot:day"
].last_successful_fill(),
"realm_creation_event_type": RealmAuditLog.REALM_CREATED,
"realm_creation_event_type": AuditLogEventType.REALM_CREATED,
},
)
rows = dictfetchall(cursor)

View File

@ -270,7 +270,7 @@ def do_create_realm(
# do_create_user(..., realm_creation=True).
acting_user=None,
realm=realm,
event_type=RealmAuditLog.REALM_CREATED,
event_type=AuditLogEventType.REALM_CREATED,
event_time=realm.date_created,
extra_data={
"how_realm_creator_found_zulip": how_realm_creator_found_zulip,

View File

@ -552,7 +552,9 @@ def do_create_user(
# If this user just created a realm, make sure they are
# properly tagged as the creator of the realm.
realm_creation_audit_log = (
RealmAuditLog.objects.filter(event_type=RealmAuditLog.REALM_CREATED, realm=realm)
RealmAuditLog.objects.filter(
event_type=AuditLogEventType.REALM_CREATED, realm=realm
)
.order_by("id")
.last()
)

View File

@ -3,6 +3,7 @@ from django.utils.timezone import now as timezone_now
from zerver.actions.realm_settings import do_set_realm_property
from zerver.models import Realm, RealmAuditLog, RealmDomain, UserProfile
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.realms import RealmDomainDict, get_realm_domains
from zerver.models.users import active_user_ids
from zerver.tornado.django_api import send_event_on_commit
@ -20,7 +21,7 @@ def do_add_realm_domain(
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_DOMAIN_ADDED,
event_type=AuditLogEventType.REALM_DOMAIN_ADDED,
event_time=timezone_now(),
extra_data={
"realm_domains": get_realm_domains(realm),
@ -54,7 +55,7 @@ def do_change_realm_domain(
RealmAuditLog.objects.create(
realm=realm_domain.realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_DOMAIN_CHANGED,
event_type=AuditLogEventType.REALM_DOMAIN_CHANGED,
event_time=timezone_now(),
extra_data={
"realm_domains": get_realm_domains(realm_domain.realm),
@ -85,7 +86,7 @@ def do_remove_realm_domain(realm_domain: RealmDomain, *, acting_user: UserProfil
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_DOMAIN_REMOVED,
event_type=AuditLogEventType.REALM_DOMAIN_REMOVED,
event_time=timezone_now(),
extra_data={
"realm_domains": get_realm_domains(realm),

View File

@ -12,6 +12,7 @@ from zerver.lib.thumbnail import THUMBNAIL_ACCEPT_IMAGE_TYPES, BadImageError
from zerver.lib.upload import upload_emoji_image
from zerver.lib.upload.base import INLINE_MIME_TYPES
from zerver.models import Realm, RealmAuditLog, RealmEmoji, UserProfile
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.realm_emoji import EmojiInfo, get_all_custom_emoji_for_realm
from zerver.models.users import active_user_ids
from zerver.tornado.django_api import send_event_on_commit
@ -54,7 +55,7 @@ def check_add_realm_emoji(
RealmAuditLog.objects.create(
realm=realm,
acting_user=author,
event_type=RealmAuditLog.REALM_EMOJI_ADDED,
event_type=AuditLogEventType.REALM_EMOJI_ADDED,
event_time=timezone_now(),
extra_data={
"realm_emoji": dict(sorted(realm_emoji_dict.items())),
@ -75,7 +76,7 @@ def do_remove_realm_emoji(realm: Realm, name: str, *, acting_user: UserProfile |
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_EMOJI_REMOVED,
event_type=AuditLogEventType.REALM_EMOJI_REMOVED,
event_time=timezone_now(),
extra_data={
"realm_emoji": dict(sorted(realm_emoji_dict.items())),

View File

@ -7,6 +7,7 @@ from zerver.lib.exceptions import JsonableError
from zerver.lib.types import LinkifierDict
from zerver.models import Realm, RealmAuditLog, RealmFilter, UserProfile
from zerver.models.linkifiers import flush_linkifiers, linkifiers_for_realm
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.users import active_user_ids
from zerver.tornado.django_api import send_event_on_commit
@ -46,7 +47,7 @@ def do_add_linkifier(
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_LINKIFIER_ADDED,
event_type=AuditLogEventType.REALM_LINKIFIER_ADDED,
event_time=timezone_now(),
extra_data={
"realm_linkifiers": realm_linkifiers,
@ -84,7 +85,7 @@ def do_remove_linkifier(
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_LINKIFIER_REMOVED,
event_type=AuditLogEventType.REALM_LINKIFIER_REMOVED,
event_time=timezone_now(),
extra_data={
"realm_linkifiers": realm_linkifiers,
@ -118,7 +119,7 @@ def do_update_linkifier(
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_LINKIFIER_CHANGED,
event_type=AuditLogEventType.REALM_LINKIFIER_CHANGED,
event_time=timezone_now(),
extra_data={
"realm_linkifiers": realm_linkifiers,
@ -173,7 +174,7 @@ def check_reorder_linkifiers(
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_LINKIFIERS_REORDERED,
event_type=AuditLogEventType.REALM_LINKIFIERS_REORDERED,
event_time=timezone_now(),
extra_data={
"realm_linkifiers": realm_linkifiers,

View File

@ -5,6 +5,7 @@ from django.utils.timezone import now as timezone_now
from zerver.lib.exceptions import ValidationFailureError
from zerver.lib.types import RealmPlaygroundDict
from zerver.models import Realm, RealmAuditLog, RealmPlayground, UserProfile
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.realm_playgrounds import get_realm_playgrounds
from zerver.models.users import active_user_ids
from zerver.tornado.django_api import send_event_on_commit
@ -43,7 +44,7 @@ def check_add_realm_playground(
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_PLAYGROUND_ADDED,
event_type=AuditLogEventType.REALM_PLAYGROUND_ADDED,
event_time=timezone_now(),
extra_data={
"realm_playgrounds": realm_playgrounds,
@ -75,7 +76,7 @@ def do_remove_realm_playground(
RealmAuditLog.objects.create(
realm=realm,
acting_user=acting_user,
event_type=RealmAuditLog.REALM_PLAYGROUND_REMOVED,
event_type=AuditLogEventType.REALM_PLAYGROUND_REMOVED,
event_time=timezone_now(),
extra_data={
"realm_playgrounds": realm_playgrounds,

View File

@ -470,7 +470,7 @@ def do_set_realm_user_default_setting(
RealmAuditLog.objects.create(
realm=realm,
event_type=RealmAuditLog.REALM_DEFAULT_USER_SETTINGS_CHANGED,
event_type=AuditLogEventType.REALM_DEFAULT_USER_SETTINGS_CHANGED,
event_time=event_time,
acting_user=acting_user,
extra_data={
@ -687,7 +687,7 @@ def do_change_realm_org_type(
realm.save(update_fields=["org_type"])
RealmAuditLog.objects.create(
event_type=RealmAuditLog.REALM_ORG_TYPE_CHANGED,
event_type=AuditLogEventType.REALM_ORG_TYPE_CHANGED,
realm=realm,
event_time=timezone_now(),
acting_user=acting_user,

View File

@ -1390,11 +1390,11 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea
# log event, even if the export was generated by an export tool
# that does not create RealmAuditLog events.
if not RealmAuditLog.objects.filter(
realm=realm, event_type=RealmAuditLog.REALM_CREATED
realm=realm, event_type=AuditLogEventType.REALM_CREATED
).exists():
RealmAuditLog.objects.create(
realm=realm,
event_type=RealmAuditLog.REALM_CREATED,
event_type=AuditLogEventType.REALM_CREATED,
event_time=realm.date_created,
# Mark these as backfilled, since they weren't created
# when the realm was actually created, and thus do not
@ -1669,7 +1669,7 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea
# system if someone tries to signup just after doing import.
RealmAuditLog.objects.create(
realm=realm,
event_type=RealmAuditLog.REALM_IMPORTED,
event_type=AuditLogEventType.REALM_IMPORTED,
event_time=timezone_now(),
extra_data={
RealmAuditLog.ROLE_COUNT: realm_user_count_by_role(realm),

View File

@ -14,6 +14,7 @@ from zerver.models import (
)
from zerver.models.clients import get_client
from zerver.models.presence import PresenceSequence
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.users import get_system_bot
from zproject.backends import all_default_backend_names
@ -37,7 +38,7 @@ def create_internal_realm() -> None:
realm.save()
RealmAuditLog.objects.create(
realm=realm, event_type=RealmAuditLog.REALM_CREATED, event_time=realm.date_created
realm=realm, event_type=AuditLogEventType.REALM_CREATED, event_time=realm.date_created
)
RealmUserDefault.objects.create(realm=realm)
create_system_user_groups_for_realm(realm)

View File

@ -50,6 +50,21 @@ class AuditLogEventType(IntEnum):
REALM_REACTIVATION_EMAIL_SENT = 212
REALM_SPONSORSHIP_PENDING_STATUS_CHANGED = 213
REALM_SUBDOMAIN_CHANGED = 214
REALM_CREATED = 215
REALM_DEFAULT_USER_SETTINGS_CHANGED = 216
REALM_ORG_TYPE_CHANGED = 217
REALM_DOMAIN_ADDED = 218
REALM_DOMAIN_CHANGED = 219
REALM_DOMAIN_REMOVED = 220
REALM_PLAYGROUND_ADDED = 221
REALM_PLAYGROUND_REMOVED = 222
REALM_LINKIFIER_ADDED = 223
REALM_LINKIFIER_CHANGED = 224
REALM_LINKIFIER_REMOVED = 225
REALM_EMOJI_ADDED = 226
REALM_EMOJI_REMOVED = 227
REALM_LINKIFIERS_REORDERED = 228
REALM_IMPORTED = 229
class AbstractRealmAuditLog(models.Model):
@ -71,22 +86,6 @@ class AbstractRealmAuditLog(models.Model):
extra_data = models.JSONField(default=dict, encoder=DjangoJSONEncoder)
# Event types
REALM_CREATED = 215
REALM_DEFAULT_USER_SETTINGS_CHANGED = 216
REALM_ORG_TYPE_CHANGED = 217
REALM_DOMAIN_ADDED = 218
REALM_DOMAIN_CHANGED = 219
REALM_DOMAIN_REMOVED = 220
REALM_PLAYGROUND_ADDED = 221
REALM_PLAYGROUND_REMOVED = 222
REALM_LINKIFIER_ADDED = 223
REALM_LINKIFIER_CHANGED = 224
REALM_LINKIFIER_REMOVED = 225
REALM_EMOJI_ADDED = 226
REALM_EMOJI_REMOVED = 227
REALM_LINKIFIERS_REORDERED = 228
REALM_IMPORTED = 229
SUBSCRIPTION_CREATED = 301
SUBSCRIPTION_ACTIVATED = 302
SUBSCRIPTION_DEACTIVATED = 303
@ -162,7 +161,7 @@ class AbstractRealmAuditLog(models.Model):
AuditLogEventType.USER_ROLE_CHANGED,
AuditLogEventType.REALM_DEACTIVATED,
AuditLogEventType.REALM_REACTIVATED,
REALM_IMPORTED,
AuditLogEventType.REALM_IMPORTED,
]
HOW_REALM_CREATOR_FOUND_ZULIP_OPTIONS = {

View File

@ -829,7 +829,7 @@ class TestRealmAuditLog(ZulipTestCase):
self.assertEqual(
RealmAuditLog.objects.filter(
realm=user.realm,
event_type=RealmAuditLog.REALM_DOMAIN_ADDED,
event_type=AuditLogEventType.REALM_DOMAIN_ADDED,
event_time__gte=now,
acting_user=user,
extra_data=expected_extra_data,
@ -850,7 +850,7 @@ class TestRealmAuditLog(ZulipTestCase):
self.assertEqual(
RealmAuditLog.objects.filter(
realm=user.realm,
event_type=RealmAuditLog.REALM_DOMAIN_CHANGED,
event_type=AuditLogEventType.REALM_DOMAIN_CHANGED,
event_time__gte=now,
acting_user=user,
extra_data=expected_extra_data,
@ -871,7 +871,7 @@ class TestRealmAuditLog(ZulipTestCase):
self.assertEqual(
RealmAuditLog.objects.filter(
realm=user.realm,
event_type=RealmAuditLog.REALM_DOMAIN_REMOVED,
event_type=AuditLogEventType.REALM_DOMAIN_REMOVED,
event_time__gte=now,
acting_user=user,
extra_data=expected_extra_data,
@ -903,7 +903,7 @@ class TestRealmAuditLog(ZulipTestCase):
self.assertEqual(
RealmAuditLog.objects.filter(
realm=user.realm,
event_type=RealmAuditLog.REALM_PLAYGROUND_ADDED,
event_type=AuditLogEventType.REALM_PLAYGROUND_ADDED,
event_time__gte=now,
acting_user=user,
extra_data=expected_extra_data,
@ -930,7 +930,7 @@ class TestRealmAuditLog(ZulipTestCase):
self.assertEqual(
RealmAuditLog.objects.filter(
realm=user.realm,
event_type=RealmAuditLog.REALM_PLAYGROUND_REMOVED,
event_type=AuditLogEventType.REALM_PLAYGROUND_REMOVED,
event_time__gte=now,
acting_user=user,
extra_data=expected_extra_data,
@ -961,7 +961,7 @@ class TestRealmAuditLog(ZulipTestCase):
self.assertEqual(
RealmAuditLog.objects.filter(
realm=user.realm,
event_type=RealmAuditLog.REALM_LINKIFIER_ADDED,
event_type=AuditLogEventType.REALM_LINKIFIER_ADDED,
event_time__gte=now,
acting_user=user,
extra_data=expected_extra_data,
@ -989,7 +989,7 @@ class TestRealmAuditLog(ZulipTestCase):
self.assertEqual(
RealmAuditLog.objects.filter(
realm=user.realm,
event_type=RealmAuditLog.REALM_LINKIFIER_CHANGED,
event_type=AuditLogEventType.REALM_LINKIFIER_CHANGED,
event_time__gte=now,
acting_user=user,
extra_data=expected_extra_data,
@ -1014,7 +1014,7 @@ class TestRealmAuditLog(ZulipTestCase):
self.assertEqual(
RealmAuditLog.objects.filter(
realm=user.realm,
event_type=RealmAuditLog.REALM_LINKIFIER_REMOVED,
event_type=AuditLogEventType.REALM_LINKIFIER_REMOVED,
event_time__gte=now,
acting_user=user,
extra_data=expected_extra_data,
@ -1058,7 +1058,7 @@ class TestRealmAuditLog(ZulipTestCase):
self.assertEqual(
RealmAuditLog.objects.filter(
realm=user.realm,
event_type=RealmAuditLog.REALM_EMOJI_ADDED,
event_type=AuditLogEventType.REALM_EMOJI_ADDED,
event_time__gte=now,
acting_user=user,
extra_data=expected_extra_data,
@ -1089,7 +1089,7 @@ class TestRealmAuditLog(ZulipTestCase):
self.assertEqual(
RealmAuditLog.objects.filter(
realm=user.realm,
event_type=RealmAuditLog.REALM_EMOJI_REMOVED,
event_type=AuditLogEventType.REALM_EMOJI_REMOVED,
event_time__gte=now,
acting_user=user,
extra_data=expected_extra_data,

View File

@ -3853,7 +3853,7 @@ class RealmPropertyActionTest(BaseAction):
self.assertEqual(
RealmAuditLog.objects.filter(
realm=self.user_profile.realm,
event_type=RealmAuditLog.REALM_DEFAULT_USER_SETTINGS_CHANGED,
event_type=AuditLogEventType.REALM_DEFAULT_USER_SETTINGS_CHANGED,
event_time__gte=now,
acting_user=self.user_profile,
).count(),
@ -3874,7 +3874,7 @@ class RealmPropertyActionTest(BaseAction):
self.assertEqual(
RealmAuditLog.objects.filter(
realm=self.user_profile.realm,
event_type=RealmAuditLog.REALM_DEFAULT_USER_SETTINGS_CHANGED,
event_type=AuditLogEventType.REALM_DEFAULT_USER_SETTINGS_CHANGED,
event_time__gte=now,
acting_user=self.user_profile,
extra_data={

View File

@ -1325,7 +1325,7 @@ class RealmImportExportTest(ExportFile):
event_type__in=[
AuditLogEventType.REALM_PLAN_TYPE_CHANGED,
RealmAuditLog.STREAM_CREATED,
RealmAuditLog.REALM_IMPORTED,
AuditLogEventType.REALM_IMPORTED,
]
)
realmauditlog_event_type = {log.event_type for log in realmauditlogs}

View File

@ -1006,7 +1006,7 @@ class RealmTest(ZulipTestCase):
do_change_realm_org_type(realm, Realm.ORG_TYPES["government"]["id"], acting_user=iago)
realm = get_realm("zulip")
realm_audit_log = RealmAuditLog.objects.filter(
event_type=RealmAuditLog.REALM_ORG_TYPE_CHANGED
event_type=AuditLogEventType.REALM_ORG_TYPE_CHANGED
).last()
assert realm_audit_log is not None
expected_extra_data = {
@ -1375,7 +1375,9 @@ class RealmTest(ZulipTestCase):
self.assertTrue(
RealmAuditLog.objects.filter(
realm=realm, event_type=RealmAuditLog.REALM_CREATED, event_time=realm.date_created
realm=realm,
event_type=AuditLogEventType.REALM_CREATED,
event_time=realm.date_created,
).exists()
)
@ -1421,7 +1423,9 @@ class RealmTest(ZulipTestCase):
self.assertTrue(
RealmAuditLog.objects.filter(
realm=realm, event_type=RealmAuditLog.REALM_CREATED, event_time=realm.date_created
realm=realm,
event_type=AuditLogEventType.REALM_CREATED,
event_time=realm.date_created,
).exists()
)

View File

@ -7,6 +7,7 @@ from typing_extensions import override
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import RealmAuditLog, RealmFilter
from zerver.models.linkifiers import url_template_validator
from zerver.models.realm_audit_logs import AuditLogEventType
class RealmFilterTest(ZulipTestCase):
@ -278,7 +279,7 @@ class RealmFilterTest(ZulipTestCase):
"""Check if the audit log created orders the linkifiers correctly"""
extra_data = (
RealmAuditLog.objects.filter(
acting_user=iago, event_type=RealmAuditLog.REALM_LINKIFIERS_REORDERED
acting_user=iago, event_type=AuditLogEventType.REALM_LINKIFIERS_REORDERED
)
.latest("event_time")
.extra_data
@ -307,7 +308,7 @@ class RealmFilterTest(ZulipTestCase):
reorder_verify_succeed([])
self.assertEqual(
RealmAuditLog.objects.filter(
realm=iago.realm, event_type=RealmAuditLog.REALM_LINKIFIERS_REORDERED
realm=iago.realm, event_type=AuditLogEventType.REALM_LINKIFIERS_REORDERED
).count(),
0,
)

View File

@ -79,6 +79,7 @@ from zerver.models import (
UserMessage,
UserProfile,
)
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.realms import get_realm
from zerver.models.recipients import get_direct_message_group_user_ids
from zerver.models.streams import get_stream
@ -1448,7 +1449,7 @@ class RealmCreationTest(ZulipTestCase):
self.assertEqual("new organizations", messages[0].topic_name())
realm_creation_audit_log = RealmAuditLog.objects.get(
realm=realm, event_type=RealmAuditLog.REALM_CREATED
realm=realm, event_type=AuditLogEventType.REALM_CREATED
)
self.assertEqual(realm_creation_audit_log.acting_user, user)
self.assertEqual(realm_creation_audit_log.event_time, realm.date_created)

View File

@ -1358,8 +1358,8 @@ class SlackImporter(ZulipTestCase):
RealmAuditLog.SUBSCRIPTION_CREATED,
AuditLogEventType.REALM_PLAN_TYPE_CHANGED,
AuditLogEventType.REALM_PROPERTY_CHANGED,
RealmAuditLog.REALM_CREATED,
RealmAuditLog.REALM_IMPORTED,
AuditLogEventType.REALM_CREATED,
AuditLogEventType.REALM_IMPORTED,
RealmAuditLog.USER_GROUP_CREATED,
RealmAuditLog.USER_GROUP_DIRECT_USER_MEMBERSHIP_ADDED,
RealmAuditLog.USER_GROUP_DIRECT_SUBGROUP_MEMBERSHIP_ADDED,