mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	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:
		@@ -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):
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user