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.
This commit is contained in:
sahil839
2020-04-04 02:23:51 +05:30
committed by Tim Abbott
parent d5f5a99b07
commit 3a7de8ad3b
2 changed files with 28 additions and 22 deletions

View File

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

View File

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