您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

38 行
1.2 KiB

  1. import re
  2. from users.models import User
  3. email_re = re.compile(
  4. r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom
  5. # quoted-string, see also http://tools.ietf.org/html/rfc2822#section-3.2.5
  6. r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"'
  7. r')@((?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)$)'
  8. # domain
  9. r'|\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$',
  10. re.IGNORECASE
  11. ) # literal form, ipv4 address (SMTP 4.1.3)
  12. class CombinedAuthBackend(object):
  13. def authenticate(self, username=None, password=None):
  14. is_email = email_re.match(username)
  15. if is_email:
  16. qs = User.objects.filter(email=username)
  17. else:
  18. qs = User.objects.filter(username=username)
  19. try:
  20. user = qs.get()
  21. except User.DoesNotExist:
  22. return None
  23. if user.check_password(password):
  24. return user
  25. return None
  26. def get_user(self, user_id):
  27. try:
  28. return User.objects.get(pk=user_id)
  29. except User.DoesNotExist:
  30. return None