mirror of https://github.com/zulip/zulip.git
users: Update has_permission to accept realm as argument.
This commit helps in using the realm object which has the prefetched group settings so that we can avoid extra queries when calculating fields like can_create_public_streams.
This commit is contained in:
parent
69e88f8787
commit
428d4284f9
|
@ -544,7 +544,7 @@ def fetch_initial_state_data(
|
||||||
)
|
)
|
||||||
|
|
||||||
state["can_create_private_streams"] = settings_user.can_create_private_streams()
|
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()
|
state["can_create_web_public_streams"] = settings_user.can_create_web_public_streams()
|
||||||
# TODO/compatibility: Deprecated in Zulip 5.0 (feature level
|
# TODO/compatibility: Deprecated in Zulip 5.0 (feature level
|
||||||
# 102); we can remove this once we no longer need to support
|
# 102); we can remove this once we no longer need to support
|
||||||
|
|
|
@ -743,7 +743,7 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings):
|
||||||
return True
|
return True
|
||||||
return False
|
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.lib.user_groups import is_user_in_group
|
||||||
from zerver.models import Realm
|
from zerver.models import Realm
|
||||||
|
|
||||||
|
@ -763,7 +763,12 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings):
|
||||||
raise AssertionError("Invalid policy")
|
raise AssertionError("Invalid policy")
|
||||||
|
|
||||||
if policy_name in Realm.REALM_PERMISSION_GROUP_SETTINGS:
|
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)
|
return is_user_in_group(allowed_user_group, self)
|
||||||
|
|
||||||
policy_value = getattr(self.realm, policy_name)
|
policy_value = getattr(self.realm, policy_name)
|
||||||
|
@ -800,8 +805,8 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings):
|
||||||
assert policy_value == Realm.POLICY_FULL_MEMBERS_ONLY
|
assert policy_value == Realm.POLICY_FULL_MEMBERS_ONLY
|
||||||
return not self.is_provisional_member
|
return not self.is_provisional_member
|
||||||
|
|
||||||
def can_create_public_streams(self) -> bool:
|
def can_create_public_streams(self, realm: Optional["Realm"] = None) -> bool:
|
||||||
return self.has_permission("can_create_public_channel_group")
|
return self.has_permission("can_create_public_channel_group", realm)
|
||||||
|
|
||||||
def can_create_private_streams(self) -> bool:
|
def can_create_private_streams(self) -> bool:
|
||||||
return self.has_permission("create_private_stream_policy")
|
return self.has_permission("create_private_stream_policy")
|
||||||
|
|
|
@ -1170,7 +1170,7 @@ class FetchQueriesTest(ZulipTestCase):
|
||||||
# count in production.
|
# count in production.
|
||||||
realm = get_realm_with_settings(realm_id=user.realm_id)
|
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:
|
with mock.patch("zerver.lib.events.always_want") as want_mock:
|
||||||
fetch_initial_state_data(user, realm=realm)
|
fetch_initial_state_data(user, realm=realm)
|
||||||
|
|
||||||
|
|
|
@ -259,7 +259,7 @@ class HomeTest(ZulipTestCase):
|
||||||
self.client_post("/json/bots", bot_info)
|
self.client_post("/json/bots", bot_info)
|
||||||
|
|
||||||
# Verify succeeds once logged-in
|
# 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:
|
with patch("zerver.lib.cache.cache_set") as cache_mock:
|
||||||
result = self._get_home_page(stream="Denmark")
|
result = self._get_home_page(stream="Denmark")
|
||||||
self.check_rendered_logged_in_app(result)
|
self.check_rendered_logged_in_app(result)
|
||||||
|
@ -564,7 +564,7 @@ class HomeTest(ZulipTestCase):
|
||||||
def test_num_queries_for_realm_admin(self) -> None:
|
def test_num_queries_for_realm_admin(self) -> None:
|
||||||
# Verify number of queries for Realm admin isn't much higher than for normal users.
|
# Verify number of queries for Realm admin isn't much higher than for normal users.
|
||||||
self.login("iago")
|
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:
|
with patch("zerver.lib.cache.cache_set") as cache_mock:
|
||||||
result = self._get_home_page()
|
result = self._get_home_page()
|
||||||
self.check_rendered_logged_in_app(result)
|
self.check_rendered_logged_in_app(result)
|
||||||
|
@ -595,7 +595,7 @@ class HomeTest(ZulipTestCase):
|
||||||
self._get_home_page()
|
self._get_home_page()
|
||||||
|
|
||||||
# Then for the second page load, measure the number of queries.
|
# 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()
|
result = self._get_home_page()
|
||||||
|
|
||||||
# Do a sanity check that our new streams were in the payload.
|
# Do a sanity check that our new streams were in the payload.
|
||||||
|
|
Loading…
Reference in New Issue