backends.py 883 B

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