Example #1
0
    def clean(self):
        data = super(AuthForm, self).clean()

        try:
            self.user = User.query().get(username=data["username"])
        except User.DoesNotExist:
            raise ValidationError('User with username "%s" not found.' % data["username"])

        if not self.user.is_valid_password(data["password"]):
            raise ValidationError("Invalid password!")

        return data
def default_user_authentication(username=None, password=None, email=None, **kwargs):
    if app_settings.DISABLE_DEFAULT_AUTHENTICATION:
        return

    try:
        if not username and email:
            fields = {'email':email}
        else:
            fields = {'username':username}

        user = User.query().get(**fields)

        if user.is_valid_password(password):
            return user
    except User.DoesNotExist:
        pass
    def execute(self, *args, **kwargs):
        load_apps()

        from london.apps.auth.models import User

        if kwargs.get('username', None):
            if User.query().filter(username=kwargs['username']).count():
                sys.exit('User "%s" already exists.'%kwargs['username'])
        elif kwargs.get('email', None):
            if User.query().filter(email=kwargs['email']).count():
                sys.exit('User with e-mail "%s" already exists.'%kwargs['email'])
            kwargs['username'] = slugify(kwargs['email'])

        fields = {}

        # Username
        if kwargs.get('username', None):
            fields['username'] = kwargs['username']
        else:
            fields['username'] = raw_input('Username: '******'username'].strip():
                print('Invalid username.')
                sys.exit(1)

        # Password
        if kwargs.get('password', None):
            fields['password'] = kwargs['password']
        else:
            fields['password'] = raw_input('Password (empty for random generation): ')
            if not fields['password']:
                fields['password'] = get_random_string()
                print('The password "%s" was generated.'%fields['password'])
            elif fields['password'] != raw_input('... again, for confirmation: '):
                print('Password not apropriately confirmed.')
                sys.exit(1)

        # E-mail address
        if kwargs.get('email', None):
            fields['email'] = kwargs['email']
        else:
            fields['email'] = raw_input('E-mail address: ')
            if not fields['email'].strip():
                print('Invalid e-mail address.')
                sys.exit(1)

        # Is staff?
        if kwargs['is_staff']:
            fields['is_staff'] = kwargs['is_staff']
        else:
            fields['is_staff'] = raw_input('Can access admin (staff)?: ').lower() == 'yes'

        # Is superuser?
        if kwargs['is_superuser']:
            fields['is_superuser'] = kwargs['is_superuser']
        else:
            fields['is_superuser'] = raw_input('Superuser?: ').lower() == 'yes'

        # Checks if a user with that username already exists.
        if User.query().filter(username=fields['username']).count():
            print('Another user exists with the username "%s".'%fields['username'])
            sys.exit(1)
        
        user = User.query().create(**fields)
        print('The user "%s" was created with password "%s"'%(fields['username'], fields['password']))