Esempio n. 1
0
def users_all(token):
    '''Returns a list of all users

    :param token: jwt token
    :type token: str
    :return: contains u_id, email, name_first, name_last, handle_str for each user
    :rtype: dict
    '''
    check_token(token)
    users = get_users()
    return {
        'users': [
            get_user_information(user_id) for user_id in users
            if not is_user_disabled(user_id)
        ],
    }
Esempio n. 2
0
def check_token(token):
    '''Checks if a jwt token corresponds to a currently logged in user.
    If the user's account has been deleted, invalidates that users token.

    :param token: jwt token
    :type token: str
    :raises AccessError: If the token does not correspond to a logged in user
    :raises AccessError: If the token corresponds to a deleted user
    :return: User id corresponding to the the valid token
    :rtype: int
    '''

    curr_users = get_valid_tokens()
    if not token in curr_users:
        raise AccessError(description="You do not have a valid token")
    u_id = decode(token.encode('utf-8'), SECRET, algorithms=['HS256'])['id']

    if is_user_disabled(u_id):
        invalidate_token(token)
        raise AccessError(description="Your account has been deleted")
    return u_id
Esempio n. 3
0
def get_user_information(u_id):
    '''Returns user information in the correct format for the slackr frontend.

    :param u_id: User id, corresponding to an existing slackr user
    :type u_id: int
    :return: dictionary containing all user information
    :rtype: dict
    '''
    try:
        user = get_users()[u_id]
    except KeyError:
        raise InputError(description='No user exists with that id')
    if is_user_disabled(u_id):
        raise InputError(description="This user has been deleted")
    return {
        'u_id': u_id,
        'email': user['email'],
        'name_first': user['name_first'],
        'name_last': user['name_last'],
        'handle_str': user['handle_str'],
        'profile_img_url': f'{os.getenv("URL")}{user["profile_img_url"]}'
    }