示例#1
0
def user_get_logs(moderator: ModeratorModel, board: BoardModel, page: int,
                  per_page: int) -> 'List[ModeratorLogModel]':
    action_authorizer.authorize_board_action(
        moderator, board, action_authorizer.ModeratorBoardAction.VIEW_LOG)

    return moderator_logs.get_all_logs_by_board(board, page * per_page,
                                                per_page)
示例#2
0
def user_remove_moderator(moderator: ModeratorModel, board: BoardModel,
                          username: str):
    member = find_moderator_username(username)
    if not member:
        raise ArgumentError('Moderator not found')

    if has_any_of_board_roles(member, board, [roles.BOARD_ROLE_CREATOR]):
        raise ArgumentError('Cannot remove creator')

    if moderator.id == member.id:
        action_authorizer.authorize_board_action(
            moderator, board,
            action_authorizer.ModeratorBoardAction.MODERATOR_REMOVE_SELF)
        board_service.remove_moderator(board, member)
        log(ModeratorLogType.MODERATOR_REMOVE, moderator, board,
            'Removed self')
        return True
    else:
        action_authorizer.authorize_board_action(
            moderator, board,
            action_authorizer.ModeratorBoardAction.MODERATOR_REMOVE)
        board_service.remove_moderator(board, member)
        log(ModeratorLogType.MODERATOR_REMOVE, moderator, board,
            'Removed {}'.format(member.username))
        return False
示例#3
0
def user_update_board_config(moderator: ModeratorModel, board: BoardModel):
    action_authorizer.authorize_board_action(
        moderator, board, action_authorizer.ModeratorBoardAction.CONFIG_UPDATE)

    boards.update_config(board)

    log(ModeratorLogType.CONFIG_UPDATE, moderator, board, 'Config updated')
示例#4
0
def can_update_roles(moderator: ModeratorModel, board: BoardModel) -> bool:
    try:
        action_authorizer.authorize_board_action(
            moderator, board, ModeratorBoardAction.ROLES_UPDATE)
        return True
    except NoPermissionError:
        return False
示例#5
0
def can_remove_moderator(moderator: ModeratorModel, board: BoardModel):
    try:
        action_authorizer.authorize_board_action(
            moderator, board, ModeratorBoardAction.MODERATOR_REMOVE)
        return True
    except NoPermissionError:
        return False
示例#6
0
def user_invite_moderator(moderator: ModeratorModel, board: BoardModel, username: str):
    action_authorizer.authorize_board_action(moderator, board, action_authorizer.ModeratorBoardAction.MODERATOR_ADD)

    invitee = find_moderator_username(username)
    if not invitee:
        raise ArgumentError('Moderator not found')

    board_service.add_moderator(board, invitee)

    log(ModeratorLogType.MODERATOR_INVITE, moderator, board, 'Invited {}'.format(invitee.username))
示例#7
0
def user_invite_moderator(moderator: ModeratorModel, board: BoardModel,
                          username: str):
    action_authorizer.authorize_board_action(
        moderator, board, action_authorizer.ModeratorBoardAction.MODERATOR_ADD)

    invitee = find_moderator_username(username)
    if not invitee:
        raise ArgumentError('Moderator not found')

    board_service.add_moderator(board, invitee)

    log(ModeratorLogType.MODERATOR_INVITE, moderator, board,
        'Invited {}'.format(invitee.username))
示例#8
0
def user_remove_moderator(moderator: ModeratorModel, board: BoardModel, username: str):
    member = find_moderator_username(username)
    if not member:
        raise ArgumentError('Moderator not found')

    if has_any_of_board_roles(member, board, [roles.BOARD_ROLE_CREATOR]):
        raise ArgumentError('Cannot remove creator')

    if moderator.id == member.id:
        action_authorizer.authorize_board_action(moderator, board,
                                                 action_authorizer.ModeratorBoardAction.MODERATOR_REMOVE_SELF)
        board_service.remove_moderator(board, member)
        log(ModeratorLogType.MODERATOR_REMOVE, moderator, board, 'Removed self')
        return True
    else:
        action_authorizer.authorize_board_action(moderator, board,
                                                 action_authorizer.ModeratorBoardAction.MODERATOR_REMOVE)
        board_service.remove_moderator(board, member)
        log(ModeratorLogType.MODERATOR_REMOVE, moderator, board, 'Removed {}'.format(member.username))
        return False
示例#9
0
def user_update_roles(moderator: ModeratorModel, board: BoardModel,
                      username: str, new_roles: 'List[str]'):
    action_authorizer.authorize_board_action(
        moderator, board, action_authorizer.ModeratorBoardAction.ROLES_UPDATE)

    subject = find_moderator_username(username)
    if not subject:
        raise ArgumentError('Moderator not found')

    if moderator.id == subject.id:  # and not has_role(moderator, roles.ROLE_ADMIN):
        raise ArgumentError('Cannot change self')

    board_moderator = board_moderators.get_board_moderator(board, subject)
    if not board_moderator:
        raise ArgumentError('Not a mod of that board')

    changed = set(new_roles) ^ set(board_moderator.roles)
    # creator is unchangeable
    if roles.BOARD_ROLE_CREATOR in changed:
        changed.remove(roles.BOARD_ROLE_CREATOR)

    if changed:
        added_roles = []
        removed_roles = []
        for i in changed:
            if i not in board_moderator.roles:
                added_roles.append(i)
            else:
                removed_roles.append(i)

        for add in added_roles:
            action_authorizer.authorize_board_action(
                moderator, board,
                action_authorizer.ModeratorBoardAction.ROLE_ADD,
                (subject, add))
            add_board_role(subject, board, add)
            log(ModeratorLogType.MODERATOR_ROLE_ADD, moderator, board,
                'Added role {} to {}'.format(add, subject.username))

        for remove in removed_roles:
            action_authorizer.authorize_board_action(
                moderator, board,
                action_authorizer.ModeratorBoardAction.ROLE_REMOVE,
                (subject, remove))
            remove_board_role(subject, board, remove)
            log(ModeratorLogType.MODERATOR_ROLE_REMOVE, moderator, board,
                'Removed role {} from {}'.format(remove, subject.username))
示例#10
0
def user_update_roles(moderator: ModeratorModel, board: BoardModel, username: str, new_roles: 'List[str]'):
    action_authorizer.authorize_board_action(moderator, board, action_authorizer.ModeratorBoardAction.ROLES_UPDATE)

    subject = find_moderator_username(username)
    if not subject:
        raise ArgumentError('Moderator not found')

    if moderator.id == subject.id:  # and not has_role(moderator, roles.ROLE_ADMIN):
        raise ArgumentError('Cannot change self')

    board_moderator = board_moderators.get_board_moderator(board, subject)
    if not board_moderator:
        raise ArgumentError('Not a mod of that board')

    changed = set(new_roles) ^ set(board_moderator.roles)
    # creator is unchangeable
    if roles.BOARD_ROLE_CREATOR in changed:
        changed.remove(roles.BOARD_ROLE_CREATOR)

    if changed:
        added_roles = []
        removed_roles = []
        for i in changed:
            if i not in board_moderator.roles:
                added_roles.append(i)
            else:
                removed_roles.append(i)

        for add in added_roles:
            action_authorizer.authorize_board_action(
                moderator, board, action_authorizer.ModeratorBoardAction.ROLE_ADD, (subject, add))
            add_board_role(subject, board, add)
            log(ModeratorLogType.MODERATOR_ROLE_ADD, moderator, board,
                'Added role {} to {}'.format(add, subject.username))

        for remove in removed_roles:
            action_authorizer.authorize_board_action(
                moderator, board, action_authorizer.ModeratorBoardAction.ROLE_REMOVE, (subject, remove))
            remove_board_role(subject, board, remove)
            log(ModeratorLogType.MODERATOR_ROLE_REMOVE, moderator, board,
                'Removed role {} from {}'.format(remove, subject.username))
示例#11
0
def user_get_logs(moderator: ModeratorModel, board: BoardModel, page: int, per_page: int) -> 'List[ModeratorLogModel]':
    action_authorizer.authorize_board_action(moderator, board, action_authorizer.ModeratorBoardAction.VIEW_LOG)

    return moderator_logs.get_all_logs_by_board(board, page * per_page, per_page)
示例#12
0
def user_update_board_config(moderator: ModeratorModel, board: BoardModel):
    action_authorizer.authorize_board_action(moderator, board, action_authorizer.ModeratorBoardAction.CONFIG_UPDATE)

    boards.update_config(board)

    log(ModeratorLogType.CONFIG_UPDATE, moderator, board, 'Config updated')
示例#13
0
def can_remove_moderator(moderator: ModeratorModel, board: BoardModel):
    try:
        action_authorizer.authorize_board_action(moderator, board, ModeratorBoardAction.MODERATOR_REMOVE)
        return True
    except NoPermissionError:
        return False
示例#14
0
def can_update_roles(moderator: ModeratorModel, board: BoardModel) -> bool:
    try:
        action_authorizer.authorize_board_action(moderator, board, ModeratorBoardAction.ROLES_UPDATE)
        return True
    except NoPermissionError:
        return False