From e4dd65d9f9c7674e4d361cb0ca78fa462d8f2456 Mon Sep 17 00:00:00 2001 From: "John R. Levine" Date: Sat, 3 Nov 2018 09:35:55 +0000 Subject: [PATCH] Allow email as well as username when logging in. Commit ready for merge. - Legacy-Id: 15702 --- ietf/ietfauth/views.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/ietf/ietfauth/views.py b/ietf/ietfauth/views.py index d68f8ec5d..6147bd8e0 100644 --- a/ietf/ietfauth/views.py +++ b/ietf/ietfauth/views.py @@ -41,7 +41,7 @@ import django.core.signing from django import forms from django.contrib import messages from django.conf import settings -from django.contrib.auth import update_session_auth_hash, logout +from django.contrib.auth import update_session_auth_hash, logout, authenticate from django.contrib.auth.decorators import login_required from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.hashers import identify_hasher @@ -586,6 +586,21 @@ def login(request, extra_context=None): form = AuthenticationForm(request, data=request.POST) username = form.data.get('username') user = User.objects.filter(username=username).first() + if not user: + # try to find user ID from the email address + email = Email.objects.filter(address=username).first() + if email: + u2 = email.person.user + # be conservative, only accept this if login is valid + if u2: + pw = form.data.get('password') + au = authenticate(request, username=str(u2), password=pw) + if au: + # kludge to change the querydict + q2 = request.POST.copy() + q2['username'] = str(u2) + request.POST = q2 + user = u2 # if user: try: