Beispiel #1
0
def admin_userpermission_change(token, u_id, permission_id):
    '''
    This is the function file for admin_userpermission_change
    '''
    # Checks
    curr_user_id = token_to_user(token)
    check_valid_user(curr_user_id)
    check_valid_user(u_id)

    # Ensure that the permission_id is valid
    if permission_id not in [1, 2, 3]:
        raise ValueError('The permission_id is invalid')

    # This block details permissions

    # The owner can do anything:
    if server_data.data['users'][curr_user_id]['permission'] == 1:
        pass
    # cannot target the owner
    elif server_data.data['users'][u_id]['permission'] == 1:
        raise AccessError('Cannot change the owners permission')
    # must be an admin or owner
    elif not is_slackr_admin(curr_user_id):
        raise AccessError('User does not have the correct permissions')
    # target user alreay has the permission_id
    elif server_data.data['users'][u_id]['permission'] == permission_id:
        raise ValueError('The target user already has that permission')
    else:
        pass

    # set the permission
    server_data.data['users'][u_id]['permission'] = permission_id

    return {}
Beispiel #2
0
def channel_create(token, name, is_public):
    '''
    The following function creates a channel
    '''

    # tests for name
    is_valid_name(name)

    if is_public == "true":
        public_bool = True
    elif is_public == "false":
        public_bool = False
    else:
        raise ValueError("Is_public must be true or false")

    # generates current user id from token
    curr_user_id = token_to_user(token)

    check_valid_user(curr_user_id)

    # generates a channel_id and assigns it to a variable
    channel_id = server_data.data["n_channels"]

    # appending a dictionary containing channel details into "channels"
    server_data.data["channels"].append({
        "channel_id":
        channel_id,
        "name":
        name,
        "members": [{
            "u_id": curr_user_id,
            "channel_permission": 1
        }],
        "messages": [],
        "is_public":
        public_bool,
        "channel_n_messages":
        0
    })

    # add all slackr owner / admins into the channel
    for user in server_data.data['users']:
        if is_slackr_admin(user['u_id']) and (user['u_id'] != curr_user_id):
            server_data.data['channels'][channel_id]['members'].append({
                "u_id":
                user['u_id'],
                "channel_permission":
                1
            })
            server_data.data['users'][user['u_id']]['channels'].append(
                channel_id)

    # appending a dictionary containing channel details into 'channels' within "users"
    server_data.data['users'][curr_user_id]['channels'].append(channel_id)

    # increasing n_channels by one
    server_data.data["n_channels"] = ((server_data.data["n_channels"]) + 1)

    # returning channel_id
    return channel_id
Beispiel #3
0
def test_add_all_slackr_and_admins():
    '''
    Test adding a user who has slackr admin into the channel
    '''
    # set-up
    # create first user with slackr permission
    token = auth_register('*****@*****.**', 'pass123', 'john',
                          'apple')['token']
    creator_id = token_to_user(token)
    assert is_slackr_admin(creator_id)

    # make another user and make channel
    second_token = auth_register('*****@*****.**', 'pass147', 'vicks',
                                 'uwu')['token']
    channel_id_public = channel_create(second_token, 'newChannel', "true")

    # assert that slackr admin is in the channel
    assert check_channel_member(creator_id, channel_id_public)
    reset_data()
Beispiel #4
0
def channel_removeowner(token, channel_id, u_id):
    '''
    Using a valid token, remove a users permission as an owner in a specified channel

    Raises errors if:
    - Channel doesn't exist
    - the user is already an not an owner of that channel
    - token's user doesn't have permission to remove owners
    '''
    is_valid_token(token)
    check_valid_user(u_id)

    token_id = token_to_user(token)

    check_valid_user(token_id)
    check_valid_channel(channel_id)

    if not check_channel_member(u_id, channel_id):
        raise ValueError('Target user is not in the channel')

# User is already not an owner of that channel
    if not is_owner(u_id, channel_id):
        raise ValueError('User is already not an owner')

    curr_user_id = token_to_user(token)

    # When user is not an admin or owner, only owners can remove other owners (AccessError)
    if not is_owner(curr_user_id,
                    channel_id) and not is_slackr_admin(curr_user_id):
        raise AccessError('You do not have permission to remove this owner')

    if server_data.data['users'][u_id]['permission'] == 1:
        raise AccessError('Cannot change the permission of Slackr creator')

# Remove user permission as owner
# get members list (from channel_id)
    members = members_list(channel_id)

    # change user permission to '0'
    for member in members:
        if member['u_id'] == u_id:
            member['channel_permission'] = 0
Beispiel #5
0
def message_edit(token, message_id, message):
    '''
    The following function edits a message
    '''

    curr_user_id = token_to_user(token)
    check_valid_user(curr_user_id)

    # if message too long / not a string
    is_valid_message(message)
    message_dict = get_msg_dict(message_id)

    # check if the message exists
    is_msg_removed(message_id)

    # Check if message is the same
    msg_str = (get_msg_dict(message_id))['message']
    if msg_str == message:
        raise ValueError(f"Message is the same")

    channel = msg_to_channel(message_id)
    check_channel_member(curr_user_id, channel['channel_id'])

    if message_dict['u_id'] == curr_user_id:
        pass
    elif is_slackr_admin(curr_user_id):
        pass
    elif is_owner(curr_user_id, channel['channel_id']):
        pass
    else:
        raise AccessError('User does not have the right permission')

    if message == "":
        message_remove(token, message_id)
    else:
        message_dict['message'] = message

    return {}
Beispiel #6
0
def message_remove(token, message_id):
    '''
    The following function removes a message @@ can change to remove completely
    '''

    curr_user_id = token_to_user(token)
    check_valid_user(curr_user_id)

    message = get_msg_dict(message_id)

    # check if the message exists
    is_msg_removed(message_id)

    channel = msg_to_channel(message_id)
    check_channel_member(curr_user_id, channel['channel_id'])

    # if the message was sent by the user
    if message['u_id'] == curr_user_id:
        pass
    # if the current user is a owner of the channel
    elif is_slackr_admin(curr_user_id):
        pass
    elif is_owner(curr_user_id, channel['channel_id']):
        pass
    else:
        raise AccessError('User does not have the right permission')

    # decrease the channel_n_messages and n_messages
    channel["channel_n_messages"] -= 1

    # deleting message
    for i in range(len(channel['messages'])):
        if channel['messages'][i]['message_id'] == int(message_id):
            del channel['messages'][i]
            break

    return {}
Beispiel #7
0
def channel_addowner(token, channel_id, u_id):
    '''
    Using a valid token, add a user specified by 'u_id' as an owner of a specific channel

    Raises errors if:
    - Channel doesn't exist
    - the user is already an owner of that channel
    - token's user doesn't have permission to assign owners
    '''
    is_valid_token(token)
    check_valid_user(u_id)

    token_id = token_to_user(token)

    check_valid_user(token_id)
    check_valid_channel(channel_id)

    if not check_channel_member(u_id, channel_id):
        raise ValueError('Target user is not in the channel')

# When user is not an admin or owner, only owners can add other owners (AccessError)
    if not is_owner(token_id, channel_id) and not is_slackr_admin(token_id):
        raise AccessError('You do not have permission to assign an owner')

# User is already an owner of that channel
    if is_owner(u_id, channel_id):
        raise ValueError('User is already an owner')

# Change user permission to owner
# get members list (from channel_id)
    members = members_list(channel_id)

    # change user permission to '1'
    for member in members:
        if member['u_id'] == u_id:
            member['channel_permission'] = 1