From a46501353d3da0b8b69f94cc8ee32db7184fa18f Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Mon, 21 Feb 2022 11:58:42 +0530 Subject: [PATCH] users: Extract access_user_common function. This function is currently used by access_user_by_id and would be used by access_user_by_email to be added in further commits. --- zerver/lib/users.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/zerver/lib/users.py b/zerver/lib/users.py index b9892124bf..ec778a4884 100644 --- a/zerver/lib/users.py +++ b/zerver/lib/users.py @@ -241,6 +241,25 @@ def access_bot_by_id(user_profile: UserProfile, user_id: int) -> UserProfile: return target +def access_user_common( + target: UserProfile, + user_profile: UserProfile, + allow_deactivated: bool, + allow_bots: bool, + for_admin: bool, +) -> UserProfile: + 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 not for_admin: + # Administrative access is not required just to read a user. + return target + if not user_profile.can_admin_user(target): + raise JsonableError(_("Insufficient permission")) + return target + + def access_user_by_id( user_profile: UserProfile, target_user_id: int, @@ -258,16 +277,8 @@ def access_user_by_id( 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 not for_admin: - # Administrative access is not required just to read a user. - return target - if not user_profile.can_admin_user(target): - raise JsonableError(_("Insufficient permission")) - return target + + return access_user_common(target, user_profile, allow_deactivated, allow_bots, for_admin) class Accounts(TypedDict):