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.
This commit is contained in:
Sahil Batra 2022-02-21 11:58:42 +05:30 committed by Tim Abbott
parent 6bdd7c7515
commit a46501353d
1 changed files with 21 additions and 10 deletions

View File

@ -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):