authentication with email instead of username

This commit is contained in:
Élie Bouttier 2017-07-31 23:20:16 +02:00
parent 02b017b6e4
commit 35825be038
7 changed files with 59 additions and 12 deletions

View File

@ -52,7 +52,7 @@ class CreateUserForm(forms.ModelForm):
user = User(first_name=self.cleaned_data.get('first_name'), last_name=self.cleaned_data.get('last_name')) user = User(first_name=self.cleaned_data.get('first_name'), last_name=self.cleaned_data.get('last_name'))
username = slugify(user.get_full_name()) username = slugify(user.get_full_name())
if User.objects.filter(username=username).exists(): if User.objects.filter(username=username).exists():
raise forms.ValidationError(_('An user with that firstname and lastname already exists.')) raise forms.ValidationError(_('An user with that firstname and that lastname already exists.'))
def clean_email(self): def clean_email(self):
email = self.cleaned_data.get('email') email = self.cleaned_data.get('email')

Binary file not shown.

View File

@ -7,7 +7,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-07-30 19:44+0000\n" "POT-Creation-Date: 2017-07-31 21:38+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -288,7 +288,11 @@ msgstr "%(name)s a été ajouté aux participants"
msgid "%(name)s is already a participant" msgid "%(name)s is already a participant"
msgstr "%(name)s est déjà participant" msgstr "%(name)s est déjà participant"
#: cfp/forms.py:47 #: cfp/forms.py:55
msgid "An user with that firstname and that lastname already exists."
msgstr "Un utilisateur avec ce prénom et ce nom existe déjà."
#: cfp/forms.py:60
msgid "A user with that email already exists." msgid "A user with that email already exists."
msgstr "Un utilisateur avec cet email existe déjà." msgstr "Un utilisateur avec cet email existe déjà."
@ -448,7 +452,7 @@ msgid "An email has been sent to you with those URLs"
msgstr "Un mail vous a été envoyé avec toutes les URLs" msgstr "Un mail vous a été envoyé avec toutes les URLs"
#: cfp/templates/cfp/propose.html:22 cfp/templates/cfp/speaker.html:21 #: cfp/templates/cfp/propose.html:22 cfp/templates/cfp/speaker.html:21
#: cfp/templates/cfp/staff/conference.html:13 #: cfp/templates/cfp/staff/conference.html:14
#: cfp/templates/cfp/staff/create_user.html:13 #: cfp/templates/cfp/staff/create_user.html:13
msgid "Save" msgid "Save"
msgstr "Envoyer" msgstr "Envoyer"
@ -462,6 +466,7 @@ msgstr "Conférence"
msgid "Please select a category." msgid "Please select a category."
msgstr "Veuillez sélectionner une catégorie." msgstr "Veuillez sélectionner une catégorie."
#: cfp/templates/cfp/staff/conference.html:13
#: cfp/templates/cfp/staff/create_user.html:8 #: cfp/templates/cfp/staff/create_user.html:8
msgid "Add a new user" msgid "Add a new user"
msgstr "Ajouter un nouvel utilisateur" msgstr "Ajouter un nouvel utilisateur"
@ -745,11 +750,11 @@ msgstr "Vote mis à jour"
msgid "Decision taken in account" msgid "Decision taken in account"
msgstr "Décision enregistrée" msgstr "Décision enregistrée"
#: cfp/views.py:208 #: cfp/views.py:214
msgid "Modifications successfully saved." msgid "Modifications successfully saved."
msgstr "Modification enregistrée avec succès." msgstr "Modification enregistrée avec succès."
#: cfp/views.py:222 #: cfp/views.py:228
msgid "User created successfully." msgid "User created successfully."
msgstr "Utilisateur créé avec succès." msgstr "Utilisateur créé avec succès."
@ -830,11 +835,11 @@ msgstr "Certains exposés ne sont pas encore planifiés."
msgid "No rooms." msgid "No rooms."
msgstr "Aucune salle." msgstr "Aucune salle."
#: ponyconf/settings.py:145 #: ponyconf/settings.py:147
msgid "English" msgid "English"
msgstr "Anglais" msgstr "Anglais"
#: ponyconf/settings.py:146 #: ponyconf/settings.py:148
msgid "French" msgid "French"
msgstr "Français" msgstr "Français"
@ -852,6 +857,7 @@ msgstr "Staff"
#: ponyconf/templates/base.html:50 #: ponyconf/templates/base.html:50
#: ponyconf/templates/registration/login.html:10 #: ponyconf/templates/registration/login.html:10
#: ponyconf/templates/registration/login.html:23
msgid "Login" msgid "Login"
msgstr "Se connecter" msgstr "Se connecter"
@ -859,10 +865,18 @@ msgstr "Se connecter"
msgid "Powered by" msgid "Powered by"
msgstr "Propulsé par" msgstr "Propulsé par"
#: ponyconf/templates/registration/login.html:24
msgid "Forgot password?"
msgstr "Mot de passe oublié ?"
#: ponyconf/templates/registration/password_change_form.html:9 #: ponyconf/templates/registration/password_change_form.html:9
msgid "Password Change" msgid "Password Change"
msgstr "Changement de mot de passe" msgstr "Changement de mot de passe"
#: ponyconf/urls.py:27
msgid "Email address"
msgstr ""
#: proposals/forms.py:46 #: proposals/forms.py:46
msgid "Should be less than 255 characters" msgid "Should be less than 255 characters"
msgstr "Texte court, moins de 255 caractères" msgstr "Texte court, moins de 255 caractères"

16
ponyconf/backends.py Normal file
View File

@ -0,0 +1,16 @@
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend
class EmailBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
if not username:
return None
UserModel = get_user_model()
try:
user = UserModel._default_manager.get(email__iexact=username)
except UserModel.DoesNotExist:
UserModel().set_password(password) # https://code.djangoproject.com/ticket/20760
else:
if user.check_password(password) and self.user_can_authenticate(user):
return user

View File

@ -210,7 +210,10 @@ SELECT2_JS = 'select2/dist/js/select2.min.js'
SELECT2_CSS = 'select2/dist/css/select2.min.css' SELECT2_CSS = 'select2/dist/css/select2.min.css'
SELECT2_I18N_PATH = 'select2/dist/js/i18n' SELECT2_I18N_PATH = 'select2/dist/js/i18n'
#AUTHENTICATION_BACKENDS = ['yeouia.backends.YummyEmailOrUsernameInsensitiveAuth'] AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'ponyconf.backends.EmailBackend',
]
LOGOUT_REDIRECT_URL = 'home' LOGOUT_REDIRECT_URL = 'home'
CRISPY_TEMPLATE_PACK='bootstrap3' CRISPY_TEMPLATE_PACK='bootstrap3'

View File

@ -17,11 +17,11 @@
{% bootstrap_form_errors form layout="horizontal" %} {% bootstrap_form_errors form layout="horizontal" %}
<form method="post" class="form-horizontal"> <form method="post" class="form-horizontal">
{% csrf_token %} {% csrf_token %}
{% bootstrap_field form.username layout="horizontal" placeholder="Nom dutilisateur, e-mail ou numéro dadhérent" %} {% bootstrap_field form.username layout="horizontal" %}
{% bootstrap_field form.password layout="horizontal" %} {% bootstrap_field form.password layout="horizontal" %}
{% buttons layout="horizontal" %} {% buttons layout="horizontal" %}
<button type="submit" class="btn btn-primary">Login</button> <button type="submit" class="btn btn-primary">{% trans "Login" %}</button>
<a class="btn btn-default" href="{% url 'password_reset' %}">Mot de passe oublié ?</a> <a class="btn btn-default" href="{% url 'password_reset' %}">{% trans "Forgot password?" %}</a>
{% endbuttons %} {% endbuttons %}
</form> </form>
</div> </div>

View File

@ -15,10 +15,24 @@ Including another URLconf
""" """
from django.conf.urls import include, url from django.conf.urls import include, url
from django.contrib import admin from django.contrib import admin
from django.contrib.auth.views import LoginView
from django.contrib.auth.forms import AuthenticationForm
from django.utils.translation import ugettext_lazy as _
class EmailAuthenticationForm(AuthenticationForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['username'].label = _('Email address')
class EmailLoginView(LoginView):
authentication_form = EmailAuthenticationForm
urlpatterns = [ urlpatterns = [
url(r'^admin/', admin.site.urls), url(r'^admin/', admin.site.urls),
url(r'accounts/login/', EmailLoginView.as_view()),
url(r'accounts/', include('django.contrib.auth.urls')), url(r'accounts/', include('django.contrib.auth.urls')),
url(r'^', include('cfp.urls')), url(r'^', include('cfp.urls')),
] ]