2018-11-11 12:41:48 +00:00
|
|
|
"""passbook core authentication forms"""
|
|
|
|
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 _
|
2019-10-01 08:24:10 +00:00
|
|
|
from structlog import get_logger
|
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
|
2019-02-25 18:43:33 +00:00
|
|
|
from passbook.lib.utils.ui import human_list
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2019-10-04 08:08:53 +00:00
|
|
|
LOGGER = get_logger()
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
|
2018-11-11 12:41:48 +00:00
|
|
|
class LoginForm(forms.Form):
|
|
|
|
"""Allow users to login"""
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
title = _("Log in to your account")
|
2019-02-25 18:43:33 +00:00
|
|
|
uid_field = forms.CharField()
|
2018-11-11 12:41:48 +00:00
|
|
|
remember_me = forms.BooleanField(required=False)
|
|
|
|
|
2019-02-21 15:06:57 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2019-12-31 11:51:16 +00:00
|
|
|
if CONFIG.y("passbook.uid_fields") == ["e-mail"]:
|
|
|
|
self.fields["uid_field"] = forms.EmailField()
|
|
|
|
self.fields["uid_field"].widget.attrs = {
|
|
|
|
"placeholder": _(
|
|
|
|
human_list([x.title() for x in CONFIG.y("passbook.uid_fields")])
|
|
|
|
)
|
2019-02-25 18:43:33 +00:00
|
|
|
}
|
2019-02-21 15:06:57 +00:00
|
|
|
|
2018-11-11 12:41:48 +00:00
|
|
|
def clean_uid_field(self):
|
|
|
|
"""Validate uid_field after EmailValidator if 'email' is the only selected uid_fields"""
|
2019-12-31 11:51:16 +00:00
|
|
|
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
|
|
|
|
2018-12-13 17:02:08 +00:00
|
|
|
|
2018-12-10 12:51:38 +00:00
|
|
|
class SignUpForm(forms.Form):
|
|
|
|
"""SignUp Form"""
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
title = _("Sign Up")
|
|
|
|
name = forms.CharField(
|
|
|
|
label=_("Name"), widget=forms.TextInput(attrs={"placeholder": _("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")}),
|
|
|
|
)
|
2018-12-10 12:51:38 +00:00
|
|
|
|
|
|
|
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
|
2019-12-31 11:51:16 +00:00
|
|
|
if "initial" in kwargs:
|
|
|
|
for field in kwargs.get("initial").keys():
|
|
|
|
self.fields[field].widget.attrs["readonly"] = "readonly"
|
2018-12-10 12:51:38 +00:00
|
|
|
|
|
|
|
def clean_username(self):
|
|
|
|
"""Check if username is used already"""
|
2019-12-31 11:51:16 +00:00
|
|
|
username = self.cleaned_data.get("username")
|
2018-12-10 12:51:38 +00:00
|
|
|
if User.objects.filter(username=username).exists():
|
2020-02-18 20:35:58 +00:00
|
|
|
LOGGER.warning("username already exists", username=username)
|
2018-12-10 12:51:38 +00:00
|
|
|
raise ValidationError(_("Username already exists"))
|
|
|
|
return username
|
|
|
|
|
|
|
|
def clean_email(self):
|
|
|
|
"""Check if email is already used in django or other auth sources"""
|
2019-12-31 11:51:16 +00:00
|
|
|
email = self.cleaned_data.get("email")
|
2018-12-10 12:51:38 +00:00
|
|
|
# Check if user exists already, error early
|
|
|
|
if User.objects.filter(email=email).exists():
|
2020-02-18 20:35:58 +00:00
|
|
|
LOGGER.debug("email already exists", email=email)
|
2018-12-10 12:51:38 +00:00
|
|
|
raise ValidationError(_("Email already exists"))
|
|
|
|
return email
|
|
|
|
|
|
|
|
def clean_password_repeat(self):
|
|
|
|
"""Check if Password adheres to filter and if passwords matche"""
|
2019-12-31 11:51:16 +00:00
|
|
|
password = self.cleaned_data.get("password")
|
|
|
|
password_repeat = self.cleaned_data.get("password_repeat")
|
2019-02-23 19:41:43 +00:00
|
|
|
if password != password_repeat:
|
|
|
|
raise ValidationError(_("Passwords don't match"))
|
2019-12-31 11:51:16 +00:00
|
|
|
return self.cleaned_data.get("password_repeat")
|