From 3a7de8ad3ba8fb4423d46a72fdc5d7065723c029 Mon Sep 17 00:00:00 2001 From: sahil839 Date: Sat, 4 Apr 2020 02:23:51 +0530 Subject: [PATCH] models: Add has_permission as a generic function for different policies. This commit removes can_create_streams and can_subscribe_other_users to use has_permission as a generic function in UserProfile model for these settings policies. Relevant changes are made to events.py to avoid duplication at some places. --- zerver/lib/events.py | 22 +++++++++++++++------- zerver/models.py | 28 +++++++++++++--------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/zerver/lib/events.py b/zerver/lib/events.py index 2978964cf3..a7136d7d29 100644 --- a/zerver/lib/events.py +++ b/zerver/lib/events.py @@ -529,14 +529,22 @@ def apply_event(state: Dict[str, Any], state['plan_includes_wide_organization_logo'] = event['value'] != Realm.LIMITED state['realm_upload_quota'] = event['extra_data']['upload_quota'] - # Tricky interaction: Whether we can create streams can get changed here. - if (field in ['realm_create_stream_policy', - 'realm_waiting_period_threshold']) and 'can_create_streams' in state: - state['can_create_streams'] = user_profile.can_create_streams() + policy_permission_dict = {'create_stream_policy': 'can_create_streams', + 'invite_to_stream_policy': 'can_subscribe_other_users'} + + # Tricky interaction: Whether we can create streams and can subscribe other users + # can get changed here. + + if field == 'realm_waiting_period_threshold': + for policy, permission in policy_permission_dict.items(): + if permission in state: + state[permission] = user_profile.has_permission(policy) + + if event['property'] in policy_permission_dict.keys(): + if policy_permission_dict[event['property']] in state: + state[policy_permission_dict[event['property']]] = user_profile.has_permission( + event['property']) - if (field in ['realm_invite_to_stream_policy', - 'realm_waiting_period_threshold']) and 'can_subscribe_other_users' in state: - state['can_subscribe_other_users'] = user_profile.can_subscribe_other_users() elif event['op'] == "update_dict": for key, value in event['data'].items(): state['realm_' + key] = value diff --git a/zerver/models.py b/zerver/models.py index 76bab65ace..71ebb70e8d 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -1159,31 +1159,29 @@ class UserProfile(AbstractBaseUser, PermissionsMixin): return True return False - def can_create_streams(self) -> bool: + def has_permission(self, policy_name: str) -> bool: + if policy_name not in ['create_stream_policy', 'invite_to_stream_policy']: + raise AssertionError("Invalid policy") + if self.is_realm_admin: return True - if self.realm.create_stream_policy == Realm.POLICY_ADMINS_ONLY: + + policy_value = getattr(self.realm, policy_name) + if policy_value == Realm.POLICY_ADMINS_ONLY: return False + if self.is_guest: return False - if self.realm.create_stream_policy == Realm.POLICY_MEMBERS_ONLY: + if policy_value == Realm.POLICY_MEMBERS_ONLY: return True return not self.is_new_member + def can_create_streams(self) -> bool: + return self.has_permission('create_stream_policy') + def can_subscribe_other_users(self) -> bool: - if self.is_realm_admin: - return True - if self.realm.invite_to_stream_policy == Realm.POLICY_ADMINS_ONLY: - return False - if self.is_guest: - return False - - if self.realm.invite_to_stream_policy == Realm.POLICY_MEMBERS_ONLY: - return True - - assert self.realm.invite_to_stream_policy == Realm.POLICY_FULL_MEMBERS_ONLY - return not self.is_new_member + return self.has_permission('invite_to_stream_policy') def can_access_public_streams(self) -> bool: return not (self.is_guest or self.realm.is_zephyr_mirror_realm)