Découpler un peu les chercheurs et l'authentification Django
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / authentification.py
1 # -*- encoding: utf-8 -*-
2 import re
3 from chercheurs.models import Personne
4 from chercheurs.utils import get_django_user_for_email
5 from datamaster_modeles.models import Authentification as AUFUser, Employe
6 from django.conf import settings
7 from django.contrib.auth.backends import ModelBackend
8 from hashlib import md5
9
10 class AUFBackend(ModelBackend):
11 """Authentifie un employé de l'AUF."""
12
13 def authenticate(self, username=None, password=None):
14 try:
15 auf_user = AUFUser.objects.get(courriel=username, actif=True)
16 except AUFUser.DoesNotExist:
17 return None
18 if not settings.AUTH_PASSWORD_REQUIRED or md5(password).hexdigest() == auf_user.motdepasse:
19 return get_django_user_for_email(username)
20
21 class PersonneBackend(ModelBackend):
22 """Authentifie un chercheur de Savoirs en partage."""
23
24 def authenticate(self, username=None, password=None):
25 try:
26 personne = Personne.objects.get(courriel=username, actif=True)
27 except Personne.DoesNotExist:
28 return None
29 user = get_django_user_for_email(username)
30 if not settings.AUTH_PASSWORD_REQUIRED or user.check_password(password):
31 return user