Skip to content

Commit e4dd65d

Browse files
committed
Allow email as well as username when logging in. Commit ready for merge.
- Legacy-Id: 15702
1 parent 542a85d commit e4dd65d

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

ietf/ietfauth/views.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from django import forms
4242
from django.contrib import messages
4343
from django.conf import settings
44-
from django.contrib.auth import update_session_auth_hash, logout
44+
from django.contrib.auth import update_session_auth_hash, logout, authenticate
4545
from django.contrib.auth.decorators import login_required
4646
from django.contrib.auth.forms import AuthenticationForm
4747
from django.contrib.auth.hashers import identify_hasher
@@ -586,6 +586,21 @@ def login(request, extra_context=None):
586586
form = AuthenticationForm(request, data=request.POST)
587587
username = form.data.get('username')
588588
user = User.objects.filter(username=username).first()
589+
if not user:
590+
# try to find user ID from the email address
591+
email = Email.objects.filter(address=username).first()
592+
if email:
593+
u2 = email.person.user
594+
# be conservative, only accept this if login is valid
595+
if u2:
596+
pw = form.data.get('password')
597+
au = authenticate(request, username=str(u2), password=pw)
598+
if au:
599+
# kludge to change the querydict
600+
q2 = request.POST.copy()
601+
q2['username'] = str(u2)
602+
request.POST = q2
603+
user = u2
589604
#
590605
if user:
591606
try:

0 commit comments

Comments
 (0)