Merge branch 'master' into test
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / authentification.py
CommitLineData
9af73c99 1# -*- encoding: utf-8 -*-
2d0d06e4 2import re
695930dd
EMS
3from chercheurs.models import Personne
4from chercheurs.utils import get_django_user_for_email
693c606b 5from auf.django.references.models import Authentification as AUFUser, Employe
2d0d06e4 6from django.conf import settings
9af73c99 7from django.contrib.auth.backends import ModelBackend
2d0d06e4 8from hashlib import md5
9af73c99 9
2d0d06e4
EMS
10class 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
f67f66aa 18 if not settings.AUTH_PASSWORD_REQUIRED or md5(password).hexdigest() == auf_user.motdepasse:
2d0d06e4
EMS
19 return get_django_user_for_email(username)
20
695930dd
EMS
21class PersonneBackend(ModelBackend):
22 """Authentifie un chercheur de Savoirs en partage."""
9af73c99 23
9af73c99 24 def authenticate(self, username=None, password=None):
9af73c99 25 try:
695930dd
EMS
26 personne = Personne.objects.get(courriel=username, actif=True)
27 except Personne.DoesNotExist:
518d0b44 28 return None
695930dd 29 user = get_django_user_for_email(username)
2d0d06e4
EMS
30 if not settings.AUTH_PASSWORD_REQUIRED or user.check_password(password):
31 return user