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.

38 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", "profile__current_project")
  7. return q.get(pk=user_id)
  8. except User.DoesNotExist:
  9. return None
  10. # Authenticate against the token in user's profile.
  11. class ProfileBackend(BasicBackend):
  12. def authenticate(self, request=None, username=None, token=None):
  13. try:
  14. profiles = Profile.objects.select_related("user")
  15. profile = profiles.get(user__username=username)
  16. except Profile.DoesNotExist:
  17. return None
  18. if not profile.check_token(token, "login"):
  19. return None
  20. return profile.user
  21. class EmailBackend(BasicBackend):
  22. def authenticate(self, request=None, username=None, password=None):
  23. try:
  24. user = User.objects.get(email=username)
  25. except User.DoesNotExist:
  26. return None
  27. if user.check_password(password):
  28. return user