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.

users: Update has_permission to accept realm as argument.

Sahil Batra428d4284f9e4
diff --git a/zerver/lib/events.py b/zerver/lib/events.py
index f43b6e6e389..7a0555a6f05 100644
--- a/zerver/lib/events.py
+++ b/zerver/lib/events.py
@@ -544,7 +544,7 @@ def fetch_initial_state_data(
)
state["can_create_private_streams"] = settings_user.can_create_private_streams()
- state["can_create_public_streams"] = settings_user.can_create_public_streams()
+ state["can_create_public_streams"] = settings_user.can_create_public_streams(realm)
state["can_create_web_public_streams"] = settings_user.can_create_web_public_streams()
# TODO/compatibility: Deprecated in Zulip 5.0 (feature level
# 102); we can remove this once we no longer need to support
diff --git a/zerver/models/users.py b/zerver/models/users.py
index 468b7d526eb..0d1698a2201 100644
--- a/zerver/models/users.py
+++ b/zerver/models/users.py
@@ -743,7 +743,7 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings):
return True
return False
- def has_permission(self, policy_name: str) -> bool:
+ def has_permission(self, policy_name: str, realm: Optional["Realm"] = None) -> bool:
from zerver.lib.user_groups import is_user_in_group
from zerver.models import Realm
@@ -763,7 +763,12 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings):
raise AssertionError("Invalid policy")
if policy_name in Realm.REALM_PERMISSION_GROUP_SETTINGS:
- allowed_user_group = getattr(self.realm, policy_name)
+ if realm is None:
+ # realm is passed by the caller only when we optimize
+ # the number of database queries by fetching the group
+ # setting fields using select_related.
+ realm = self.realm
+ allowed_user_group = getattr(realm, policy_name)
return is_user_in_group(allowed_user_group, self)
policy_value = getattr(self.realm, policy_name)
@@ -800,8 +805,8 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings):
assert policy_value == Realm.POLICY_FULL_MEMBERS_ONLY
return not self.is_provisional_member
- def can_create_public_streams(self) -> bool:
- return self.has_permission("can_create_public_channel_group")
+ def can_create_public_streams(self, realm: Optional["Realm"] = None) -> bool:
+ return self.has_permission("can_create_public_channel_group", realm)
def can_create_private_streams(self) -> bool:
return self.has_permission("create_private_stream_policy")
diff --git a/zerver/tests/test_event_system.py b/zerver/tests/test_event_system.py
index 2098fe68b04..5ab5f9f01d2 100644
--- a/zerver/tests/test_event_system.py
+++ b/zerver/tests/test_event_system.py
@@ -1170,7 +1170,7 @@ class FetchQueriesTest(ZulipTestCase):
# count in production.
realm = get_realm_with_settings(realm_id=user.realm_id)
- with self.assert_database_query_count(44):
+ with self.assert_database_query_count(43):
with mock.patch("zerver.lib.events.always_want") as want_mock:
fetch_initial_state_data(user, realm=realm)
diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py
index 15a9fc4f56a..3bed3f358c1 100644
--- a/zerver/tests/test_home.py
+++ b/zerver/tests/test_home.py
@@ -259,7 +259,7 @@ class HomeTest(ZulipTestCase):
self.client_post("/json/bots", bot_info)
# Verify succeeds once logged-in
- with self.assert_database_query_count(55):
+ with self.assert_database_query_count(54):
with patch("zerver.lib.cache.cache_set") as cache_mock:
result = self._get_home_page(stream="Denmark")
self.check_rendered_logged_in_app(result)
@@ -564,7 +564,7 @@ class HomeTest(ZulipTestCase):
def test_num_queries_for_realm_admin(self) -> None:
# Verify number of queries for Realm admin isn't much higher than for normal users.
self.login("iago")
- with self.assert_database_query_count(55):
+ with self.assert_database_query_count(54):
with patch("zerver.lib.cache.cache_set") as cache_mock:
result = self._get_home_page()
self.check_rendered_logged_in_app(result)
@@ -595,7 +595,7 @@ class HomeTest(ZulipTestCase):
self._get_home_page()
# Then for the second page load, measure the number of queries.
- with self.assert_database_query_count(50):
+ with self.assert_database_query_count(49):
result = self._get_home_page()
# Do a sanity check that our new streams were in the payload.