auth_backends.py 690 Bytes
Newer Older
luxcem's avatar
luxcem committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User


class EmailOrUsernameModelBackend(ModelBackend):
    """
    Authentification backend to allow email
    overide AUTHENTICATION_BACKENDS with
    fabauth.auth_backends.EmailOrUsernameModelBackend
    """
    def authenticate(self, username=None, password=None, **kwargs):
        if "@" in username:
            kwargs = {"email": username}
        else:
            kwargs = {"username": username}
        try:
            user = User.objects.get(**kwargs)
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            return None