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'))
username = slugify(user.get_full_name())
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):
email = self.cleaned_data.get('email')

Binary file not shown.

View File

@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\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"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\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"
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."
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"
#: 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
msgid "Save"
msgstr "Envoyer"
@ -462,6 +466,7 @@ msgstr "Conférence"
msgid "Please select a category."
msgstr "Veuillez sélectionner une catégorie."
#: cfp/templates/cfp/staff/conference.html:13
#: cfp/templates/cfp/staff/create_user.html:8
msgid "Add a new user"
msgstr "Ajouter un nouvel utilisateur"
@ -745,11 +750,11 @@ msgstr "Vote mis à jour"
msgid "Decision taken in account"
msgstr "Décision enregistrée"
#: cfp/views.py:208
#: cfp/views.py:214
msgid "Modifications successfully saved."
msgstr "Modification enregistrée avec succès."
#: cfp/views.py:222
#: cfp/views.py:228
msgid "User created successfully."
msgstr "Utilisateur créé avec succès."
@ -830,11 +835,11 @@ msgstr "Certains exposés ne sont pas encore planifiés."
msgid "No rooms."
msgstr "Aucune salle."
#: ponyconf/settings.py:145
#: ponyconf/settings.py:147
msgid "English"
msgstr "Anglais"
#: ponyconf/settings.py:146
#: ponyconf/settings.py:148
msgid "French"
msgstr "Français"
@ -852,6 +857,7 @@ msgstr "Staff"
#: ponyconf/templates/base.html:50
#: ponyconf/templates/registration/login.html:10
#: ponyconf/templates/registration/login.html:23
msgid "Login"
msgstr "Se connecter"
@ -859,10 +865,18 @@ msgstr "Se connecter"
msgid "Powered by"
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
msgid "Password Change"
msgstr "Changement de mot de passe"
#: ponyconf/urls.py:27
msgid "Email address"
msgstr ""
#: proposals/forms.py:46
msgid "Should be less than 255 characters"
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_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'
CRISPY_TEMPLATE_PACK='bootstrap3'

View File

@ -17,11 +17,11 @@
{% bootstrap_form_errors form layout="horizontal" %}
<form method="post" class="form-horizontal">
{% 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" %}
{% buttons layout="horizontal" %}
<button type="submit" class="btn btn-primary">Login</button>
<a class="btn btn-default" href="{% url 'password_reset' %}">Mot de passe oublié ?</a>
<button type="submit" class="btn btn-primary">{% trans "Login" %}</button>
<a class="btn btn-default" href="{% url 'password_reset' %}">{% trans "Forgot password?" %}</a>
{% endbuttons %}
</form>
</div>

View File

@ -15,10 +15,24 @@ Including another URLconf
"""
from django.conf.urls import include, url
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 = [
url(r'^admin/', admin.site.urls),
url(r'accounts/login/', EmailLoginView.as_view()),
url(r'accounts/', include('django.contrib.auth.urls')),
url(r'^', include('cfp.urls')),
]