|
@@ -0,0 +1,23 @@
|
|
|
+from django.contrib.auth.models import User
|
|
|
+from django.contrib.contenttypes.models import ContentType
|
|
|
+from django.contrib.auth.backends import ModelBackend
|
|
|
+
|
|
|
+from .models import Adherent
|
|
|
+
|
|
|
+
|
|
|
+class AdherentBackend(ModelBackend):
|
|
|
+ def authenticate(self, username, password=None, **kwargs):
|
|
|
+ if str(username).lower().startswith("adt"):
|
|
|
+ username = str(username)[3:]
|
|
|
+ try:
|
|
|
+ adherent_id = int(username)
|
|
|
+ except ValueError:
|
|
|
+ return None
|
|
|
+ user_type = ContentType.objects.get_for_model(User)
|
|
|
+ try:
|
|
|
+ user = Adherent.objects.get(adherent_type=user_type, id=adherent_id).adherent
|
|
|
+ except Adherent.DoesNotExist:
|
|
|
+ UserModel().set_password(password) # https://code.djangoproject.com/ticket/20760
|
|
|
+ else:
|
|
|
+ if user.check_password(password) and self.user_can_authenticate(user):
|
|
|
+ return user
|