backends.py 900 B

1234567891011121314151617181920212223
  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, password=None, **kwargs):
  7. if str(username).lower().startswith("adt"):
  8. username = str(username)[3:]
  9. try:
  10. adhesion_id = int(username)
  11. except ValueError:
  12. return None
  13. user_type = ContentType.objects.get_for_model(User)
  14. try:
  15. user = Adhesion.objects.get(adherent_type=user_type, id=adhesion_id).adherent
  16. except Adhesion.DoesNotExist:
  17. User().set_password(password) # https://code.djangoproject.com/ticket/20760
  18. else:
  19. if user.check_password(password) and self.user_can_authenticate(user):
  20. return user