mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 05:23:35 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user