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.

user_groups: Reduce necessary nesting inside try-block.

Zixuan James Li37b3507b8695
diff --git a/zerver/lib/user_groups.py b/zerver/lib/user_groups.py
index 9015e1e0abf..e8cad2ff628 100644
--- a/zerver/lib/user_groups.py
+++ b/zerver/lib/user_groups.py
@@ -33,24 +33,24 @@ def access_user_group_by_id(
) -> UserGroup:
try:
user_group = UserGroup.objects.get(id=user_group_id, realm=user_profile.realm)
- if for_read and not user_profile.is_guest:
- # Everyone is allowed to read a user group and check who
- # are its members. Guests should be unable to reach this
- # code path, since they can't access user groups API
- # endpoints, but we check for guests here for defense in
- # depth.
- return user_group
- if user_group.is_system_group:
- raise JsonableError(_("Insufficient permission"))
- group_member_ids = get_user_group_direct_member_ids(user_group)
- if (
- not user_profile.is_realm_admin
- and not user_profile.is_moderator
- and user_profile.id not in group_member_ids
- ):
- raise JsonableError(_("Insufficient permission"))
except UserGroup.DoesNotExist:
raise JsonableError(_("Invalid user group"))
+ if for_read and not user_profile.is_guest:
+ # Everyone is allowed to read a user group and check who
+ # are its members. Guests should be unable to reach this
+ # code path, since they can't access user groups API
+ # endpoints, but we check for guests here for defense in
+ # depth.
+ return user_group
+ if user_group.is_system_group:
+ raise JsonableError(_("Insufficient permission"))
+ group_member_ids = get_user_group_direct_member_ids(user_group)
+ if (
+ not user_profile.is_realm_admin
+ and not user_profile.is_moderator
+ and user_profile.id not in group_member_ids
+ ):
+ raise JsonableError(_("Insufficient permission"))
return user_group