You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB

  1. from django.contrib.auth.models import User
  2. from hc.accounts.models import Profile
  3. class BasicBackend(object):
  4. def get_user(self, user_id):
  5. try:
  6. q = User.objects.select_related("profile",
  7. "profile__current_project")
  8. return q.get(pk=user_id)
  9. except User.DoesNotExist:
  10. return None
  11. # Authenticate against the token in user's profile.
  12. class ProfileBackend(BasicBackend):
  13. def authenticate(self, request=None, username=None, token=None):
  14. try:
  15. profiles = Profile.objects.select_related("user")
  16. profile = profiles.get(user__username=username)
  17. except Profile.DoesNotExist:
  18. return None
  19. if not profile.check_token(token, "login"):
  20. return None
  21. return profile.user
  22. class EmailBackend(BasicBackend):
  23. def authenticate(self, request=None, username=None, password=None):
  24. try:
  25. user = User.objects.get(email=username)
  26. except User.DoesNotExist:
  27. return None
  28. if user.check_password(password):
  29. return user