Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

auth_backends.py 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from django.contrib.auth.backends import ModelBackend
  2. from django.core.exceptions import ImproperlyConfigured
  3. from oscar.apps.customer.utils import normalise_email
  4. from oscar.core.compat import get_user_model
  5. User = get_user_model()
  6. if hasattr(User, 'REQUIRED_FIELDS'):
  7. if not (User.USERNAME_FIELD == 'email' or 'email' in User.REQUIRED_FIELDS):
  8. raise ImproperlyConfigured(
  9. "EmailBackend: Your User model must have an email"
  10. " field with blank=False")
  11. class EmailBackend(ModelBackend):
  12. """
  13. Custom auth backend that uses an email address and password
  14. For this to work, the User model must have an 'email' field
  15. """
  16. def authenticate(self, email=None, password=None, *args, **kwargs):
  17. if email is None:
  18. if 'username' not in kwargs or kwargs['username'] is None:
  19. return None
  20. clean_email = normalise_email(kwargs['username'])
  21. else:
  22. clean_email = normalise_email(email)
  23. # Check if we're dealing with an email address
  24. if '@' not in clean_email:
  25. return None
  26. # Since Django doesn't enforce emails to be unique, we look for all
  27. # matching users and try to authenticate them all. Note that we
  28. # intentionally allow multiple users with the same email address
  29. # (has been a requirement in larger system deployments),
  30. # we just enforce that they don't share the same password.
  31. # We make a case-insensitive match when looking for emails.
  32. matching_users = User.objects.filter(email__iexact=clean_email)
  33. authenticated_users = [
  34. user for user in matching_users if user.check_password(password)]
  35. if len(authenticated_users) == 1:
  36. # Happy path
  37. return authenticated_users[0]
  38. elif len(authenticated_users) > 1:
  39. # This is the problem scenario where we have multiple users with
  40. # the same email address AND password. We can't safely authenticate
  41. # either.
  42. raise User.MultipleObjectsReturned(
  43. "There are multiple users with the given email address and "
  44. "password")
  45. return None
  46. # Deprecated in Oscar 0.8: Spelling
  47. Emailbackend = EmailBackend