mirror of
https://github.com/zulip/zulip.git
synced 2025-11-15 11:22:04 +00:00
users: Clarify readability issues related to access_user_by_id.
zerver/lib/users.py has a function named access_user_by_id, which is used in /users views to fetch a user by it's id. Along with fetching the user this function also does important validations regarding checking of required permissions for fetching the target user. In an attempt to solve the above problem this commit introduces following changes: 1. Make all the parameters except user_profile, target_user_id to be keyword only. 2. Use for_admin parameter instead of read_only. 3. Adds a documentary note to the function describing the reason for changes along with recommended way to call this function in future. 4. Changes in views and tests to call this function in this changed format. Changes were tested using ./tools/test-backend. Fixes #17111.
This commit is contained in:
committed by
Tim Abbott
parent
abff97df39
commit
0e6343c071
@@ -222,18 +222,28 @@ def access_bot_by_id(user_profile: UserProfile, user_id: int) -> UserProfile:
|
||||
raise JsonableError(_("Insufficient permission"))
|
||||
return target
|
||||
|
||||
def access_user_by_id(user_profile: UserProfile, user_id: int,
|
||||
allow_deactivated: bool=False, allow_bots: bool=False,
|
||||
read_only: bool=False) -> UserProfile:
|
||||
def access_user_by_id(
|
||||
user_profile: UserProfile,
|
||||
target_user_id: int,
|
||||
*,
|
||||
allow_deactivated: bool=False,
|
||||
allow_bots: bool=False,
|
||||
for_admin: bool,
|
||||
) -> UserProfile:
|
||||
"""Master function for accessing another user by ID in API code;
|
||||
verifies the user ID is in the same realm, and if requested checks
|
||||
for administrative privileges, with flags for various special
|
||||
cases.
|
||||
"""
|
||||
try:
|
||||
target = get_user_profile_by_id_in_realm(user_id, user_profile.realm)
|
||||
target = get_user_profile_by_id_in_realm(target_user_id, user_profile.realm)
|
||||
except UserProfile.DoesNotExist:
|
||||
raise JsonableError(_("No such user"))
|
||||
if target.is_bot and not allow_bots:
|
||||
raise JsonableError(_("No such user"))
|
||||
if not target.is_active and not allow_deactivated:
|
||||
raise JsonableError(_("User is deactivated"))
|
||||
if read_only:
|
||||
if not for_admin:
|
||||
# Administrative access is not required just to read a user.
|
||||
return target
|
||||
if not user_profile.can_admin_user(target):
|
||||
|
||||
Reference in New Issue
Block a user