예제 #1
0
def channel_addowner(token, channel_id, u_id):
    '''
    This will set the user with u_id as the owner of target channel.
    Token refers to one of the owner of target channel.
    It will return an empty dictionaty.

    Args:
        param1: authorised user's token.
        param2: target channel.
        param3: new owner's u_id

    Returns:
        This will return an empty dictionary.

    Raises:
        InputError:
            1. Channel ID is not a valid channel
            2. When user with user id u_id is already an owner of the channel
        AccessError:
            1. when the authorised user is not an owner of
                the flockr, or an owner of this channel
            2. given token does not refer to a valid token
    '''
    auth_user = get_user_from_token(token)
    channel = get_channel_from_id(channel_id)
    invited_user = get_user_from_id(u_id)
    # access error when the token does not refer to a valid token
    if auth_user is None:
        raise AccessError(description='Invalid token')

    # input error when Channel ID is not a valid channel
    if channel is None:
        raise InputError(description='Invalid channel_id')

    # input error when u_id does not refer to a valid user
    if invited_user is None:
        raise InputError(description='Invalid u_id')

    # input error when When user with user id u_id
    # is already an owner of the channel
    if invited_user['u_id'] in channel['owner_members']:
        raise InputError(description='Already an owner')

    # access error when the authorised user is not
    # an owner of the flockr, or an owner of this channel
    if is_user_an_owner(token, channel_id) is False:
        raise AccessError(description='Not permitted to add')

    channel['owner_members'].append(u_id)
    return {}
예제 #2
0
def user_profile(token, u_id):
    '''
    This function is for showing user's profile.
    It will return the information of user's file.

    Args:
        param1: authorised user's token
        param2: authorised user's u_id

    Returns:
        a dictionary containing profile
        {
            'user': {
                'u_id': 1,
                'email': '*****@*****.**',
                'name_first': 'Hayden',
                'name_last': 'Jacobs',
                'handle_str': 'hjacobs',
                'profile_img_url' : img_url,
            },
        }

    Raises:
        InputError: User with u_id is not a valid user
        AccessError: given token does not refer to a valid user
    '''
    request_user = get_user_from_token(token)
    target_user = get_user_from_id(u_id)
    # raise AccessError when given token does not refer to a valid user
    if request_user is None:
        raise AccessError(description='Invalid token')

    # raise InputError when given u_id is not correct
    if target_user is None:
        raise InputError(description='Invalid u_id')

    return {
        'user': {
            'u_id': target_user['u_id'],
            'email': target_user['email'],
            'name_first': target_user['name_first'],
            'name_last': target_user['name_last'],
            'handle_str': target_user['handle'],
            'profile_img_url': target_user['profile_img_url'],
        },
    }
예제 #3
0
def channel_invite(token, channel_id, u_id):
    '''
    This will invite a user (with user id u_id) to join a channel with channel_id.
    Once invited the user is added to the channel immediately.

    Args:
        param1: invitor's token.
        param2: target channel.
        param3: invited user's u_id

    Returns:
        This will return an empty dictionary.

    Raises:
        InputError:
            1. channel_id does not refer to a valid channel.
            2. u_id does not refer to a valid user.
        AccessError:
            1. the authorised user is not already a member of the channel.
            2. given token does not refer to a valid token
    '''
    auth_user = get_user_from_token(token)
    invited_user = get_user_from_id(u_id)
    channel = get_channel_from_id(channel_id)

    # access error when given token does not refer to a valid user
    if auth_user is None:
        raise AccessError(description='Invalid Token')
    # input error when u_id does not refer to a valid user
    if invited_user is None:
        raise InputError(description='Invalid u_id')
    # input error when channel_id does not refer to a valid channel.
    if channel is None:
        raise InputError(description='Invalid channel_id')

    # accesss error when the authorised user is not a member of the channel
    if auth_user['u_id'] not in channel['all_members']:
        raise AccessError(description='Not a member')

    # invited_user is already in channel
    if invited_user['u_id'] in channel['all_members']:
        return {}

    channel['all_members'].append(u_id)
    invited_user['channels'].append(channel_id)
    return {}
예제 #4
0
def channel_removeowner(token, channel_id, u_id):
    '''
    This will remove the user with u_id from the owners of target channel.
    If u_id refers to the owner of flockr, it will ignore the request.
    Token refers to one of the owner of target channel.
    It will return an empty dictionaty.

    Args:
        param1: authorised user's token.
        param2: target channel.
        param3: the user's u_id who is removed from owners

    Returns:
        This will return an empty dictionary.

    Raises:
        InputError:
            1. Channel ID is not a valid channel
            2. When user with user id u_id is not an owner of the channel
        AccessError:
            1. when the authorised user is not an owner of
                the flockr, or an owner of this channel
            2. given token does not refer to a valid token
    '''
    auth_user = get_user_from_token(token)
    channel = get_channel_from_id(channel_id)
    removed_user = get_user_from_id(u_id)
    # access error when given token does not refer to a valid user
    if auth_user is None:
        raise AccessError(description='Invalid token')
    # input error when Channel ID is not a valid channel
    if channel is None:
        raise InputError(description='Invalid channel_id')
    # input error when u_id does not refer to a valid user
    if removed_user is None:
        raise InputError(description='Invalid u_id')
    # input error when user with user id u_id is not an owner of the channel
    if removed_user['u_id'] not in channel['owner_members']:
        raise InputError(description='Not a owner of channel')

    # accesss error when the authorised user is not
    # an owner of the flockr, or an owner of this channel
    if is_user_an_owner(token, channel_id) is False:
        raise AccessError(description='Not permitted to remove')
    channel['owner_members'].remove(u_id)
    return {}
예제 #5
0
def member_initial(u_id):
    '''
    This is a simple helper function.
    It will return a member type data with given u_id if it exists in data,
    else return False

    Args:
        param1: u_id

    Returns:
        This will return member(dictionary) if u_id refers to a valid user in data,
        else return False.

    Raises:
        this will not raise any error
    '''
    user = get_user_from_id(u_id)
    return {
        'u_id': user['u_id'],
        'name_first': user['name_first'],
        'name_last': user['name_last'],
        'profile_img_url': user['profile_img_url'],
    }
예제 #6
0
def admin_userpermission_change(token, u_id, permission_id):
    """
        Given a user by their user ID, set their permissions to new permissions
        described by permission_id.

        :param token: The token of any Flockr owner
        :type token: str

        :param u_id: The user ID of the user whose permissions we want to change
        :type u_id: int

        :param permission_id: The ID of the new permission to be set on the user
        (1 for owner, 2 for member)
        :type permission_id: int
    """
    # check u_id validity
    user = get_user_from_id(u_id)
    if user is None:
        raise InputError(description=f"Cannot find user with ID of {u_id}")

    # check permission_id validity
    if permission_id not in [1, 2]:
        raise InputError(description="Invalid permission code")

    # check token validity
    if get_user_from_token(token) is None:
        raise AccessError(description="Unauthorised access")

    # check if token refers to an owner
    admin = get_user_from_token(token)
    if admin['permission_id'] != 1:
        raise AccessError(description="Members cannot modify permissions")

    # change permission of u_id user to permission_id
    user['permission_id'] = permission_id

    return {}