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 setters for is_realm_owner and is_moderator.

Shelly6d92534d51eb
diff --git a/zerver/models.py b/zerver/models.py
index db7cae83528..d5d1e630df8 100644
--- a/zerver/models.py
+++ b/zerver/models.py
@@ -1716,6 +1716,15 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings):
def is_realm_owner(self) -> bool:
return self.role == UserProfile.ROLE_REALM_OWNER
+ @is_realm_owner.setter
+ def is_realm_owner(self, value: bool) -> None:
+ if value:
+ self.role = UserProfile.ROLE_REALM_OWNER
+ elif self.role == UserProfile.ROLE_REALM_OWNER:
+ # We need to be careful to not accidentally change
+ # ROLE_GUEST to ROLE_MEMBER here.
+ self.role = UserProfile.ROLE_MEMBER
+
@property
def is_guest(self) -> bool:
return self.role == UserProfile.ROLE_GUEST
@@ -1733,6 +1742,15 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings):
def is_moderator(self) -> bool:
return self.role == UserProfile.ROLE_MODERATOR
+ @is_moderator.setter
+ def is_moderator(self, value: bool) -> None:
+ if value:
+ self.role = UserProfile.ROLE_MODERATOR
+ elif self.role == UserProfile.ROLE_MODERATOR:
+ # We need to be careful to not accidentally change
+ # ROLE_GUEST to ROLE_MEMBER here.
+ self.role = UserProfile.ROLE_MEMBER
+
@property
def is_incoming_webhook(self) -> bool:
return self.bot_type == UserProfile.INCOMING_WEBHOOK_BOT
diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py
index b971a31e470..cd1a0e22f40 100644
--- a/zerver/tests/test_users.py
+++ b/zerver/tests/test_users.py
@@ -95,6 +95,14 @@ class PermissionTest(ZulipTestCase):
self.assertEqual(user_profile.is_guest, False)
self.assertEqual(user_profile.role, UserProfile.ROLE_REALM_ADMINISTRATOR)
+ user_profile.is_realm_owner = False
+ self.assertEqual(user_profile.is_realm_owner, False)
+ self.assertEqual(user_profile.role, UserProfile.ROLE_REALM_ADMINISTRATOR)
+
+ user_profile.is_moderator = False
+ self.assertEqual(user_profile.is_moderator, False)
+ self.assertEqual(user_profile.role, UserProfile.ROLE_REALM_ADMINISTRATOR)
+
user_profile.is_realm_admin = False
self.assertEqual(user_profile.is_realm_admin, False)
self.assertEqual(user_profile.role, UserProfile.ROLE_MEMBER)
@@ -111,6 +119,22 @@ class PermissionTest(ZulipTestCase):
self.assertEqual(user_profile.is_guest, False)
self.assertEqual(user_profile.role, UserProfile.ROLE_MEMBER)
+ user_profile.is_realm_owner = True
+ self.assertEqual(user_profile.is_realm_owner, True)
+ self.assertEqual(user_profile.role, UserProfile.ROLE_REALM_OWNER)
+
+ user_profile.is_realm_owner = False
+ self.assertEqual(user_profile.is_realm_owner, False)
+ self.assertEqual(user_profile.role, UserProfile.ROLE_MEMBER)
+
+ user_profile.is_moderator = True
+ self.assertEqual(user_profile.is_moderator, True)
+ self.assertEqual(user_profile.role, UserProfile.ROLE_MODERATOR)
+
+ user_profile.is_moderator = False
+ self.assertEqual(user_profile.is_moderator, False)
+ self.assertEqual(user_profile.role, UserProfile.ROLE_MEMBER)
+
def test_get_admin_users(self) -> None:
user_profile = self.example_user("hamlet")
do_change_user_role(user_profile, UserProfile.ROLE_MEMBER, acting_user=None)