models: Add can_invite_others_to_realm for checking who can send invites.

This commit adds can_invite_others_to_realm helper which will be used in
further in next commit when invite_to_realm_policy will be modified to
support all values of COMMON_POLICY_TYPES.

It is important for this commit's correctness that
INVITE_TO_REALM_POLICY_TYPES was initialized to use the same values.
This commit is contained in:
sahil839 2021-04-03 21:35:20 +05:30 committed by Tim Abbott
parent 4c8339fa8c
commit d6165225c4
2 changed files with 16 additions and 1 deletions

View File

@ -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)

View File

@ -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.