backends.py 944 B

12345678910111213141516171819202122232425
  1. from django.contrib.auth.models import User
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.contrib.auth.backends import ModelBackend
  4. from .models import Adhesion
  5. class AdhesionBackend(ModelBackend):
  6. def authenticate(self, username=None, password=None):
  7. if not username:
  8. return None
  9. if str(username).lower().startswith("adt"):
  10. username = str(username)[3:]
  11. try:
  12. adhesion_id = int(username)
  13. except ValueError:
  14. return None
  15. user_type = ContentType.objects.get_for_model(User)
  16. try:
  17. user = Adhesion.objects.get(adherent_type=user_type, id=adhesion_id).adherent
  18. except Adhesion.DoesNotExist:
  19. User().set_password(password) # https://code.djangoproject.com/ticket/20760
  20. else:
  21. if user.check_password(password) and self.user_can_authenticate(user):
  22. return user