Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
user_groups: Extract has_user_group_access helper.
Zixuan James Li9f7fab421368
diff --git a/zerver/lib/user_groups.py b/zerver/lib/user_groups.pyindex f61fe1199d2..55b69fe50db 100644--- a/zerver/lib/user_groups.py+++ b/zerver/lib/user_groups.py@@ -28,29 +28,42 @@ class UserGroupDict(TypedDict):can_mention_group: int-def access_user_group_by_id(- user_group_id: int, user_profile: UserProfile, *, for_read: bool-) -> UserGroup:- try:- user_group = UserGroup.objects.get(id=user_group_id, realm=user_profile.realm)- except UserGroup.DoesNotExist:- raise JsonableError(_("Invalid user group"))+def has_user_group_access(+ user_group: UserGroup, user_profile: UserProfile, *, for_read: bool+) -> bool: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+ return True+if user_group.is_system_group:- raise JsonableError(_("Insufficient permission"))+ return False+group_member_ids = get_user_group_direct_member_ids(user_group)if (not user_profile.is_realm_adminand not user_profile.is_moderatorand user_profile.id not in group_member_ids):+ return False++ return True+++def access_user_group_by_id(+ user_group_id: int, user_profile: UserProfile, *, for_read: bool+) -> UserGroup:+ try:+ user_group = UserGroup.objects.get(id=user_group_id, realm=user_profile.realm)+ except UserGroup.DoesNotExist:+ raise JsonableError(_("Invalid user group"))++ if not has_user_group_access(user_group, user_profile, for_read=for_read):raise JsonableError(_("Insufficient permission"))+return user_group