Exemple #1
0
def test_delete_all_failed_login_attempts_by_user(db, user):
    """Delete all failed login attempts for a specific user."""
    failed_login_attempt = FailedLoginAttempt(user.email, '127.0.0.1')
    failed_login_attempt.save()
    other_failed_login_attempt = FailedLoginAttempt('foo', '127.0.0.1')
    other_failed_login_attempt.save()

    FailedLoginAttempt.delete_all_by_user(user)
    db.session.commit()
    attempts = FailedLoginAttempt.query.all()
    assert failed_login_attempt not in attempts
    assert [other_failed_login_attempt] == attempts
Exemple #2
0
def forget_user(email, dry_run):
    """Remove all traces of a user from the system."""
    user = User.get_by_email(email)
    if user:
        if user.is_admin:
            click.echo('User "{}" is a sysadmin, refusing to delete.'.format(user))
            sys.exit(1)

        if len(User.get_modified_and_created_by_user(user)) > 0:
            click.echo('User "{}" has created or modified users, refusing to delete.'.format(user))
            sys.exit(1)

        if len(Collection.get_modified_and_created_by_user(user)) > 0:
            click.echo('User "{}" has created or modified collections, '
                       'refusing to delete.'.format(user))
            sys.exit(1)

        if len(Permission.get_modified_and_created_by_user(user)) > 0:
            click.echo('User "{}" has created or modified permissions, '
                       'refusing to delete.'.format(user))
            sys.exit(1)

        if dry_run:
            tokens = Token.get_all_by_user(user)
            grants = Grant.get_all_by_user(user)
            failed_login_attempts = FailedLoginAttempt.get_all_by_user(user)
            permissions = user.permissions
            password_resets = user.password_resets
            click.echo('These tokens would be deleted: {}'.format(tokens))
            click.echo('These grants would be deleted: {}'.format(grants))
            click.echo('These failed login attempts would be deleted: {}'.format(
                failed_login_attempts))
            click.echo('These permissions would be deleted: {}'.format(permissions))
            click.echo('These password_resets would be deleted: {}'.format(password_resets))
        else:
            if click.confirm('Are you sure you want to delete all information '
                             'related to user "{}"?'.format(user)):
                Token.delete_all_by_user(user)
                Grant.delete_all_by_user(user)
                Permission.delete_all_by_user(user)
                PasswordReset.delete_all_by_user(user)
                FailedLoginAttempt.delete_all_by_user(user)
                user.delete()

    else:
        click.echo('User "{}" not found. Aborting...'.format(email))
        sys.exit(1)