2018-11-11 12:41:48 +00:00
|
|
|
"""passbook core authentication forms"""
|
2018-12-10 12:51:38 +00:00
|
|
|
from logging import getLogger
|
2018-11-11 12:41:48 +00:00
|
|
|
|
|
|
|
from django import forms
|
2018-12-10 12:51:38 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2018-11-11 12:41:48 +00:00
|
|
|
from django.core.validators import validate_email
|
2018-12-10 12:51:38 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2018-12-10 12:51:38 +00:00
|
|
|
from passbook.core.models import User
|
2018-11-11 12:41:48 +00:00
|
|
|
from passbook.lib.config import CONFIG
|
|
|
|
|
2018-12-10 12:51:38 +00:00
|
|
|
LOGGER = getLogger(__name__)
|
2018-11-11 12:41:48 +00:00
|
|
|
|
|
|
|
class LoginForm(forms.Form):
|
|
|
|
"""Allow users to login"""
|
|
|
|
|
2018-12-10 12:51:38 +00:00
|
|
|
title = _('Log in to your account')
|
|
|
|
uid_field = forms.CharField(widget=forms.TextInput(attrs={'placeholder': _('UID')}))
|
|
|
|
password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': _('Password')}))
|
2018-11-11 12:41:48 +00:00
|
|
|
remember_me = forms.BooleanField(required=False)
|
|
|
|
|
|
|
|
def clean_uid_field(self):
|
|
|
|
"""Validate uid_field after EmailValidator if 'email' is the only selected uid_fields"""
|
|
|
|
if CONFIG.y('passbook.uid_fields') == ['email']:
|
|
|
|
validate_email(self.cleaned_data.get('uid_field'))
|
|
|
|
return self.cleaned_data.get('uid_field')
|
2018-12-10 12:51:38 +00:00
|
|
|
|
|
|
|
class SignUpForm(forms.Form):
|
|
|
|
"""SignUp Form"""
|
|
|
|
|
|
|
|
title = _('Sign Up')
|
|
|
|
first_name = forms.CharField(label=_('First Name'),
|
|
|
|
widget=forms.TextInput(attrs={'placeholder': _('First Name')}))
|
|
|
|
last_name = forms.CharField(label=_('Last Name'),
|
|
|
|
widget=forms.TextInput(attrs={'placeholder': _('Last Name')}))
|
|
|
|
username = forms.CharField(label=_('Username'),
|
|
|
|
widget=forms.TextInput(attrs={'placeholder': _('Username')}))
|
|
|
|
email = forms.EmailField(label=_('E-Mail'),
|
|
|
|
widget=forms.TextInput(attrs={'placeholder': _('E-Mail')}))
|
|
|
|
password = forms.CharField(label=_('Password'),
|
|
|
|
widget=forms.PasswordInput(attrs={'placeholder': _('Password')}))
|
|
|
|
password_repeat = forms.CharField(label=_('Repeat Password'),
|
|
|
|
widget=forms.PasswordInput(attrs={
|
|
|
|
'placeholder': _('Repeat Password')
|
|
|
|
}))
|
|
|
|
# captcha = ReCaptchaField(
|
|
|
|
# required=(not settings.DEBUG and not settings.TEST),
|
|
|
|
# private_key=Setting.get('recaptcha:private'),
|
|
|
|
# public_key=Setting.get('recaptcha:public'))
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2018-12-10 14:26:53 +00:00
|
|
|
# All fields which have initial data supplied are set to read only
|
|
|
|
if 'initial' in kwargs:
|
2018-12-10 14:42:13 +00:00
|
|
|
for field in kwargs.get('initial').keys():
|
2018-12-10 14:26:53 +00:00
|
|
|
self.fields[field].widget.attrs['readonly'] = 'readonly'
|
2018-12-10 12:51:38 +00:00
|
|
|
# TODO: Dynamically add captcha here
|
|
|
|
# if not Setting.get_bool('recaptcha:enabled'):
|
|
|
|
# self.fields.pop('captcha')
|
|
|
|
|
|
|
|
def clean_username(self):
|
|
|
|
"""Check if username is used already"""
|
|
|
|
username = self.cleaned_data.get('username')
|
|
|
|
if User.objects.filter(username=username).exists():
|
|
|
|
LOGGER.warning("Username %s already exists", username)
|
|
|
|
raise ValidationError(_("Username already exists"))
|
|
|
|
return username
|
|
|
|
|
|
|
|
def clean_email(self):
|
|
|
|
"""Check if email is already used in django or other auth sources"""
|
|
|
|
email = self.cleaned_data.get('email')
|
|
|
|
# Check if user exists already, error early
|
|
|
|
if User.objects.filter(email=email).exists():
|
|
|
|
LOGGER.debug("email %s exists in django", email)
|
|
|
|
raise ValidationError(_("Email already exists"))
|
|
|
|
return email
|
|
|
|
|
|
|
|
def clean_password_repeat(self):
|
|
|
|
"""Check if Password adheres to filter and if passwords matche"""
|
|
|
|
# TODO: Password policy? Via Plugin? via Policy?
|
|
|
|
# return check_password(self)
|
|
|
|
return self.cleaned_data.get('password_repeat')
|