Added new setting, widget, form and auth backend to allow login via user email address

To enable:
AUTHENTICATION_BACKENDS = ('common.auth.email_auth_backend.EmailAuthBackend',)
COMMON_LOGIN_METHOD = 'email'
This commit is contained in:
Roberto Rosario
2011-08-05 23:36:00 -04:00
parent acd6348dd7
commit 36c7beca84
7 changed files with 92 additions and 5 deletions

View File

@@ -2,9 +2,12 @@ from django import forms
from django.utils.translation import ugettext_lazy as _
from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import authenticate
from common.utils import return_attrib
from common.widgets import DetailSelectMultiple, PlainWidget, TextAreaDiv
from common.widgets import DetailSelectMultiple, PlainWidget, \
TextAreaDiv, EmailInput
class DetailForm(forms.ModelForm):
@@ -115,3 +118,29 @@ class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name', 'last_name')
class EmailAuthenticationForm(AuthenticationForm):
"""
Override the default authentication form to use email address
authentication
"""
email = forms.CharField(label=_(u'Email'), max_length=75,
widget=EmailInput()
)
def clean(self):
email = self.cleaned_data.get('email')
password = self.cleaned_data.get('password')
if email and password:
self.user_cache = authenticate(email=email, password=password)
if self.user_cache is None:
raise forms.ValidationError(_('Please enter a correct email and password. Note that the password fields is case-sensitive.'))
elif not self.user_cache.is_active:
raise forms.ValidationError(_('This account is inactive.'))
self.check_for_test_cookie()
return self.cleaned_data
# Remove the inherited username field
EmailAuthenticationForm.base_fields.keyOrder = ['email', 'password']