Merge branch 'master' of gpn:/srv/mygpo
[mygpo.git] / mygpo / auth_backends.py
blob7b720edf92165e8107ec66df9d93eba3641d3864
2 # This file is part of my.gpodder.org.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
18 from django.conf import settings
19 from django.contrib.auth.models import User
20 from django.contrib.auth.backends import ModelBackend
21 from django.core.exceptions import ImproperlyConfigured
22 from django.db.models import get_model
24 try:
25 from django.forms.fields import email_re
26 except ImportError:
27 from django.core.validators import email_re
29 class EmailAuthenticationBackend(ModelBackend):
30 def authenticate(self, username=None, password=None):
31 if email_re.search(username):
32 try:
33 user = User.objects.filter(email=username)[0]
34 return user if user.check_password(password) else None
35 except:
36 return None
37 return None
39 def get_user(self, user_id):
40 try:
41 return User.objects.get(pk=user_id)
42 except User.DoesNotExist:
43 return None