def message_pin(token, message_id):
    '''
    Given a message within a channel, mark it as "pinned" to be given special
    display treatment by the frontend
    '''

    channel = get_channel_by_message_id(message_id)
    message_obj = get_message_by_message_id(message_id)
    user = get_user_by_token(token)

    # Message_id does not refer to an existing message
    if not valid_message_id(message_id):
        raise ValueError("Message does not exist")

    # Message_id is already pinned
    if message_obj.is_pinned:
        raise ValueError("Message is currently pinned")

    # User is not a member of the channel
    if not channel.is_member(user.u_id):
        raise AccessError("Authorised user is not a member of the channel")

    # User is not an owner of the channel
    if not channel.is_owner(user.u_id) and \
        not token_is_admin(token) and \
        not token_is_owner(token):
        raise ValueError("User is not an admin")

    # Pin message
    message_obj.pin_message()

    return {}
def message_unpin(token, message_id):
    '''
    Given a message within a channel, remove it's mark as unpinned
    '''

    channel = get_channel_by_message_id(message_id)
    message_obj = get_message_by_message_id(message_id)
    user = get_user_by_token(token)

    # Message_id does not refer to an existing message
    if not valid_message_id(message_id):
        raise ValueError("Message does not exist")

    # Message_id is already unpinned
    if not message_obj.is_pinned:
        raise ValueError("Message is currently unpinned")

    # User is not a member of the channel
    if not channel.is_member(user.u_id):
        raise AccessError("Authorised user is not a member of the channel")

    # User is not an owner of the channel
    if not channel.is_owner(user.u_id) and \
        not token_is_admin(token) and \
        not token_is_owner(token):
        raise ValueError("User is not an admin")

    # Unpinning message
    message_obj.unpin_message()

    return {}
def message_edit(token, message_id, message):
    '''
    Given a message, update it's text with new text. If the new message is an
    empty string, the message is deleted.
    '''

    channel = get_channel_by_message_id(message_id)
    message_obj = get_message_by_message_id(message_id)
    user = get_user_by_token(token)

    # message_id does not refer to an existing message
    if not valid_message_id(message_id):
        raise ValueError("Message does not exist")

    # message is not of appropriate length
    if valid_message(message):
        raise ValueError("Message length too long")

    # User does not have permission to edit message
    if not  message_obj.user_sent_message(user.u_id) and \
            not token_is_admin(token) and \
            not token_is_owner(token) and \
            not channel.is_owner(user.u_id):
        raise AccessError("User does not have permission")

    # Edit channel message
    if not message.strip():
        # If empty message, delete
        channel = get_channel_by_message_id(message_id)
        channel.remove_message(message_id)
    else:
        # Otherwise, edit message
        message_obj.edit_message(message)

    return {}
示例#4
0
def channel_addowner(token, channel_id, u_id):
    '''
    make an user an owner of the channel
    '''

    channel = get_channel_by_channel_id(channel_id)

    # channel_id does not refer to a valid channel
    if channel is None:
        raise ValueError("Channel does not exist")

    # user is already an owner of the channel
    if channel.is_owner(u_id):
        raise ValueError("User is already an owner of the channel")

    # User does not have permission to add owner
    if not channel.is_owner(decode_token(token)) and \
            not token_is_admin(token) and \
            not token_is_owner(token):
        raise AccessError("User is not an owner of the channel or slackrowner")

    # User is added as owner
    channel.add_owner(u_id)

    return {}
def test_token_is_owner():
    '''
    Test if a token is in an owner
    '''

    # Initialisation
    global_var.initialise_all()

    # Creating a user
    user = auth.auth_register("*****@*****.**", "pass123", "Raydon", "Smith")

    with pytest.raises(AccessError, match="Invalid Token"):
        helpers.token_is_owner("-1")

    token = user["token"]
    user = helpers.get_user_by_token(token)
    user.permission = 1
    assert helpers.token_is_owner(token) is True
    user.permission = 0
    assert helpers.token_is_owner(token) is False
示例#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
    '''

    # Getting user object
    user = get_user_by_u_id(u_id)

    # Checking validity of permission change request
    if not user:
        raise ValueError('Invalid User ID')
    if not valid_permission_id(permission_id):
        raise ValueError('Permission ID is invalid')
    if not (token_is_admin(token) or token_is_owner(token)):
        raise AccessError('Current user is not an admin or owner')

    # Changing user's permission
    user.change_permissions(permission_id)

    return {}
def message_remove(token, message_id):
    ''' Given a message ID, the message is removed '''

    channel = get_channel_by_message_id(message_id)
    message = get_message_by_message_id(message_id)
    user = get_user_by_token(token)

    # Message_id does not refer to an existing message
    if not valid_message_id(message_id):
        raise ValueError("Message does not exist")

    # User does not have permission to remove message
    if not message.user_sent_message(user.u_id) and \
        not token_is_admin(token) and \
        not token_is_owner(token) and \
        not channel.is_owner(user.u_id):
        raise AccessError("User does not have permission")

    # Removing message
    channel.remove_message(message_id)

    return {}
示例#8
0
def channel_join(token, channel_id):
    '''
    Given a channel_id of a channel that the authorised user can join
    adds them to that channel
    '''

    channel = get_channel_by_channel_id(channel_id)
    user = get_user_by_token(token)

    # channel_id does not refer to a valid channel
    if channel is None:
        raise ValueError("Channel does not exist")

    # User does not have permission to join channel without invite
    if not channel.is_public and \
         not token_is_admin(token) and \
            not token_is_owner(token):
        raise AccessError("Channel is private and user is not admin")

    # User is added to channel
    channel.add_user(user.u_id)

    return {}
示例#9
0
def channel_removeowner(token, channel_id, u_id):
    '''
    Remove user with user id u_id an owner of this channel
    '''

    channel = get_channel_by_channel_id(channel_id)

    # channel_id does not refer to a valid channel
    if channel is None:
        raise ValueError("Channel does not exist")

    # user is not an owner of the channel
    if not channel.is_owner(u_id):
        raise ValueError("User id is not an owner of the channel")

    # If the user trying to remove owner does not have permission
    if not channel.is_owner(decode_token(token)) and \
        not token_is_admin(token) and \
        not token_is_owner(token):
        raise AccessError("User is not an owner of the channel or slackrowner")

    channel.remove_owner(u_id)

    return {}