From b4e4ca14d55569d557f680f244e3ba2311e964e0 Mon Sep 17 00:00:00 2001 From: Aman Agrawal Date: Mon, 11 Dec 2023 13:24:37 +0000 Subject: [PATCH] models: Store `is_system_bot_realm` information for `RemoteRealm`. This will help us filter out system bot realm and control feature access to it. --- zerver/lib/remote_server.py | 2 ++ zerver/tests/test_push_notifications.py | 3 +++ .../0051_remoterealm_is_system_bot_realm.py | 17 +++++++++++++++++ zilencer/models.py | 2 ++ zilencer/views.py | 11 ++++++++++- 5 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 zilencer/migrations/0051_remoterealm_is_system_bot_realm.py diff --git a/zerver/lib/remote_server.py b/zerver/lib/remote_server.py index 36ee743fe9..7018aad608 100644 --- a/zerver/lib/remote_server.py +++ b/zerver/lib/remote_server.py @@ -75,6 +75,7 @@ class RealmDataForAnalytics(BaseModel): org_type: int = 0 date_created: float deactivated: bool + is_system_bot_realm: bool = False authentication_methods: Dict[str, bool] = Field(default_factory=dict) @@ -302,6 +303,7 @@ def get_realms_info_for_push_bouncer(realm_id: Optional[int] = None) -> List[Rea org_type=realm.org_type, name=realm.name, authentication_methods=realm.authentication_methods_dict(), + is_system_bot_realm=realm.string_id == settings.SYSTEM_BOT_REALM, ) for realm in realms ] diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py index 64c4148114..1ca2c25dbe 100644 --- a/zerver/tests/test_push_notifications.py +++ b/zerver/tests/test_push_notifications.py @@ -1143,6 +1143,7 @@ class AnalyticsBouncerTest(BouncerTestCase): "registration_deactivated", "realm_deactivated", "plan_type", + "is_system_bot_realm", ) ), [ @@ -1158,6 +1159,7 @@ class AnalyticsBouncerTest(BouncerTestCase): "registration_deactivated": False, "realm_deactivated": False, "plan_type": RemoteRealm.PLAN_TYPE_SELF_HOSTED, + "is_system_bot_realm": realm.string_id == "zulipinternal", } for realm in Realm.objects.order_by("id") ], @@ -1217,6 +1219,7 @@ class AnalyticsBouncerTest(BouncerTestCase): RemoteRealmAuditLog.objects.filter( event_type=RemoteRealmAuditLog.REMOTE_REALM_VALUE_UPDATED ) + .exclude(realm_id=get_realm("zulipinternal").id) .order_by("id") .values("event_type", "remote_id", "realm_id", "extra_data") ) diff --git a/zilencer/migrations/0051_remoterealm_is_system_bot_realm.py b/zilencer/migrations/0051_remoterealm_is_system_bot_realm.py new file mode 100644 index 0000000000..c0bcf9a7f1 --- /dev/null +++ b/zilencer/migrations/0051_remoterealm_is_system_bot_realm.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.8 on 2023-12-11 20:38 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("zilencer", "0050_preregistrationremoterealmbillinguser_created_user_and_more"), + ] + + operations = [ + migrations.AddField( + model_name="remoterealm", + name="is_system_bot_realm", + field=models.BooleanField(default=False), + ), + ] diff --git a/zilencer/models.py b/zilencer/models.py index 107fa3f716..bb95174dce 100644 --- a/zilencer/models.py +++ b/zilencer/models.py @@ -121,6 +121,8 @@ class RemoteRealm(models.Model): name = models.TextField(default="") + is_system_bot_realm = models.BooleanField(default=False) + authentication_methods = models.JSONField(default=dict) org_type = models.PositiveSmallIntegerField( diff --git a/zilencer/views.py b/zilencer/views.py index 6daa71325a..a66b9e5212 100644 --- a/zilencer/views.py +++ b/zilencer/views.py @@ -603,6 +603,7 @@ def update_remote_realm_data_for_server( org_type=realm.org_type, name=realm.name, authentication_methods=realm.authentication_methods, + is_system_bot_realm=realm.is_system_bot_realm, ) for realm in server_realms_info if realm.uuid not in already_registered_uuids @@ -629,6 +630,7 @@ def update_remote_realm_data_for_server( ("name", "name"), ("authentication_methods", "authentication_methods"), ("realm_deactivated", "deactivated"), + ("is_system_bot_realm", "is_system_bot_realm"), ]: old_value = getattr(remote_realm, remote_realm_attr) new_value = getattr(realm, realm_dict_key) @@ -659,7 +661,14 @@ def update_remote_realm_data_for_server( RemoteRealm.objects.bulk_update( remote_realms_to_update, - ["host", "realm_deactivated", "name", "authentication_methods", "org_type"], + [ + "host", + "realm_deactivated", + "name", + "authentication_methods", + "org_type", + "is_system_bot_realm", + ], ) RemoteRealmAuditLog.objects.bulk_create(remote_realm_audit_logs)