Utilisé l'héritage de modèles entre Chercheur et Personne
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / authentification.py
CommitLineData
9af73c99
AJ
1# -*- encoding: utf-8 -*-
2import hashlib, sys
3
4import settings
5from django.contrib.auth.backends import ModelBackend
6from django.contrib.auth.models import User as DjangoUser, check_password
7
13ec4813 8from chercheurs.models import Personne as RemoteUser
9af73c99
AJ
9
10class CascadeBackend(ModelBackend):
11 def authenticate(self, username=None, password=None):
12 user = None
9af73c99 13 email = username
9af73c99
AJ
14
15 # Cherche les comptes roa+locaux
16 remoteUser = localUser = None
17 try:
13ec4813 18 remoteUser = RemoteUser.objects.get(courriel=email)
92990258
EMS
19 if settings.AUTH_PASSWORD_REQUIRED and not remoteUser.check_password(password):
20 remoteUser = None
9af73c99
AJ
21 except:
22 pass
23 try:
24 localUser = DjangoUser.objects.get (username=username)
25 except: pass
26
27 # Si on a pas besoin du mdp, on doit copier qd meme,
28 # il ne faut jamais retourner un "RemoteUser" ici
29 if not settings.AUTH_PASSWORD_REQUIRED:
30 if remoteUser and not localUser:
31 localUser = DjangoUser (username = username,
92990258
EMS
32 email = username,
33 first_name = remoteUser.prenom,
34 last_name = remoteUser.nom,
35 is_staff = settings.USERS_AS_STAFF,
36 is_active = True,
37 is_superuser = False)
9af73c99
AJ
38 localUser.set_password (password)
39 localUser.save ()
40 user = localUser
41 # Gestion des comptes roa vs. local
42 else:
43 # Local existe pas, on doit de tte facon le creer
44 if not localUser:
45 localUser = DjangoUser (username = username,
46 email = email,
47 is_staff = settings.USERS_AS_STAFF,
48 is_active = True,
49 is_superuser = False)
50 # Cas du compte local seul, on verifie le mot de passe
51 elif not remoteUser:
52 if localUser.check_password (password):
53 user = localUser
54 # Compte roa, on valide le mot de passe distant et on
55 # met a jour la copie locale
56 if remoteUser:
57 localUser.first_name = remoteUser.prenom
58 localUser.last_name = remoteUser.nom
59 # pass distant en md5
60 localUser.set_password (password)
61 localUser.save ()
62 user = localUser
63
64 return user