1# Copyright 2013 The Distro Tracker Developers
2# See the COPYRIGHT file at the top-level directory of this distribution and
3# at https://deb.li/DTAuthors
5# This file is part of Distro Tracker. It is subject to the license terms
6# in the LICENSE file found in the top-level directory of this
7# distribution and at https://deb.li/DTLicense. No part of Distro Tracker,
8# including this file, may be copied, modified, propagated, or distributed
9# except according to the terms contained in the LICENSE file.
10"""Django authentication backend based on the email address."""
12from django.contrib.auth import get_user_model
14User = get_user_model()
18 def authenticate(self, request=None, username=None, password=None):
20 Implements the custom authentication method.
22 Since a particular user may have multiple email accounts associated
23 with their account and they should be able to log in using any one of
24 them, this authentication backend first matches the given email to the
25 :class:`django_email_accounts.models.User` instance to which the email
26 is associated and then authenticates the credentials against that user
29 The signature of the method is adapted to take a username argument
30 representing the email of the user. This way, it matches the default
31 Django authentication backend method signature which allows admin users
32 to log in to the admin console using any of their associated emails.
34 :returns: :class:`django_email_accounts.models.User` instance if the
35 authentication is successful, or ``None`` otherwise.
37 email = username
38 # Find a user with the given email
40 user = User.objects.get(emails__email=email)
41 except User.DoesNotExist:
43 user = User.objects.get(main_email=email)
45 except User.DoesNotExist:
46 return None
48 # Check if valid log in details were provided
49 if user.check_password(password): 49 ↛ 52line 49 didn't jump to line 52, because the condition on line 49 was never false
50 return user
52 return None
54 def get_user(self, user_id):
56 return User.objects.get(pk=user_id)
57 except User.DoesNotExist:
58 return None