Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

models: Add can_invite_others_to_realm for checking who can send invites.

sahil839d6165225c4ee
diff --git a/zerver/models.py b/zerver/models.py
index c52a2e0f537..b09ea99708d 100644
--- a/zerver/models.py
+++ b/zerver/models.py
@@ -1522,7 +1522,11 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):
return False
def has_permission(self, policy_name: str) -> bool:
- if policy_name not in ["create_stream_policy", "invite_to_stream_policy"]:
+ if policy_name not in [
+ "create_stream_policy",
+ "invite_to_stream_policy",
+ "invite_to_realm_policy",
+ ]:
raise AssertionError("Invalid policy")
if self.is_realm_admin:
@@ -1553,6 +1557,9 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):
def can_subscribe_other_users(self) -> bool:
return self.has_permission("invite_to_stream_policy")
+ def can_invite_others_to_realm(self) -> bool:
+ return self.has_permission("invite_to_realm_policy")
+
def can_access_public_streams(self) -> bool:
return not (self.is_guest or self.realm.is_zephyr_mirror_realm)
diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py
index 49f171b641a..bf5786ab31a 100644
--- a/zerver/tests/test_signup.py
+++ b/zerver/tests/test_signup.py
@@ -1232,6 +1232,14 @@ class InviteUserTest(InviteUserBase):
self.assertTrue(find_key_by_email(email2))
self.check_sent_emails([email, email2], custom_from_name="Hamlet")
+ def test_can_invite_others_to_realm(self) -> None:
+ othello = self.example_user("othello")
+
+ def validation_func() -> bool:
+ return othello.can_invite_others_to_realm()
+
+ self.check_has_permission_policies(othello, "invite_to_realm_policy", validation_func)
+
def test_require_realm_admin(self) -> None:
"""
The invite_to_realm_policy realm setting works properly.