2018-04-22 15:31:41 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import ldap
|
|
|
|
|
|
|
|
from allauth.account.utils import user_email, user_field, user_username
|
2018-04-22 17:22:03 +02:00
|
|
|
from allauth.account.models import EmailAddress
|
|
|
|
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter, get_account_adapter
|
2018-04-22 15:31:41 +02:00
|
|
|
from allauth.socialaccount.models import SocialAccount
|
2018-04-22 20:13:42 +02:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
import six
|
2018-04-22 15:31:41 +02:00
|
|
|
|
|
|
|
DEPARTMENTS_LIST = (
|
|
|
|
('phy', u'Physique'),
|
|
|
|
('maths', u'Maths'),
|
|
|
|
('bio', u'Biologie'),
|
|
|
|
('chimie', u'Chimie'),
|
|
|
|
('geol', u'Géosciences'),
|
|
|
|
('dec', u'DEC'),
|
|
|
|
('info', u'Informatique'),
|
|
|
|
('litt', u'Littéraire'),
|
|
|
|
('guests', u'Pensionnaires étrangers'),
|
|
|
|
('pei', u'PEI'),
|
|
|
|
)
|
|
|
|
|
2018-04-22 20:13:42 +02:00
|
|
|
def _init_ldap():
|
|
|
|
server = getattr(settings, "LDAP_SERVER", "ldaps://ldap.spi.ens.fr:636")
|
|
|
|
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT,
|
|
|
|
ldap.OPT_X_TLS_NEVER)
|
|
|
|
l = ldap.initialize(server)
|
|
|
|
l.set_option(ldap.OPT_REFERRALS, 0)
|
|
|
|
l.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
|
|
|
|
l.set_option(ldap.OPT_X_TLS, ldap.OPT_X_TLS_DEMAND)
|
|
|
|
l.set_option(ldap.OPT_X_TLS_DEMAND, True)
|
|
|
|
l.set_option(ldap.OPT_DEBUG_LEVEL, 255)
|
|
|
|
l.set_option(ldap.OPT_NETWORK_TIMEOUT, 10)
|
|
|
|
l.set_option(ldap.OPT_TIMEOUT, 10)
|
|
|
|
return l
|
|
|
|
|
2018-04-22 15:31:41 +02:00
|
|
|
def get_ldap_infos(clipper):
|
|
|
|
assert clipper.isalnum()
|
|
|
|
data = {'email':'{}@clipper.ens.fr'.format(clipper.strip().lower())}
|
|
|
|
try:
|
2018-04-22 20:13:42 +02:00
|
|
|
l = _init_ldap()
|
2018-04-22 15:31:41 +02:00
|
|
|
|
|
|
|
info = l.search_s('dc=spi,dc=ens,dc=fr',
|
|
|
|
ldap.SCOPE_SUBTREE,
|
|
|
|
('(uid=%s)' % (clipper,)),
|
|
|
|
[str("cn"),
|
|
|
|
str("mailRoutingAddress"),
|
|
|
|
str("homeDirectory") ])
|
|
|
|
|
|
|
|
if len(info) > 0:
|
|
|
|
infos = info[0][1]
|
|
|
|
|
|
|
|
# Name
|
|
|
|
data['name'] = infos.get('cn', [''])[0].decode("utf-8")
|
|
|
|
|
|
|
|
# Parsing homeDirectory to get entrance year and departments
|
|
|
|
annee = '00'
|
|
|
|
promotion = 'Inconnue'
|
|
|
|
|
|
|
|
if 'homeDirectory' in infos:
|
|
|
|
dirs = infos['homeDirectory'][0].split('/')
|
|
|
|
if dirs[1] == 'users':
|
|
|
|
annee = dirs[2]
|
|
|
|
dep = dirs[3]
|
|
|
|
dep = dict(DEPARTMENTS_LIST).get(dep.lower(), '')
|
|
|
|
promotion = u'%s %s' % (dep, annee)
|
|
|
|
data['annee'] = annee
|
|
|
|
data['promotion'] = promotion
|
|
|
|
|
|
|
|
# Mail
|
|
|
|
pmail = infos.get('mailRoutingAddress', [])
|
|
|
|
if len(pmail) > 0 :
|
|
|
|
data['email'] = pmail[0]
|
|
|
|
|
|
|
|
except ldap.LDAPError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
class LongTermClipperAccountAdapter(DefaultSocialAccountAdapter):
|
|
|
|
"""
|
|
|
|
A class to manage the fact that people loose their account at the end of
|
|
|
|
their scolarity and that their clipper login might be reused later
|
|
|
|
"""
|
|
|
|
|
|
|
|
def pre_social_login(self, request, sociallogin):
|
|
|
|
clipper = sociallogin.account.uid
|
|
|
|
try:
|
2018-04-23 00:01:27 +02:00
|
|
|
a = SocialAccount.objects.get(provider='clipper_inactive',
|
2018-04-22 15:31:41 +02:00
|
|
|
uid=clipper)
|
|
|
|
# An account with that uid was registered, but potentially
|
|
|
|
# deprecated at the beginning of the year
|
|
|
|
# We need to check that the user is still the same as before
|
|
|
|
ldap_data = get_ldap_infos(clipper)
|
|
|
|
self._ldap_data = ldap_data
|
2018-04-22 17:22:03 +02:00
|
|
|
|
|
|
|
if a.user.username != self.get_username(clipper, ldap_data):
|
2018-04-22 15:31:41 +02:00
|
|
|
# The admission year is different
|
|
|
|
# We need a new SocialAccount
|
2018-04-22 17:22:03 +02:00
|
|
|
# But before that, we need to invalidate the email address of
|
|
|
|
# the previous user
|
|
|
|
email = ldap_data.get('email')
|
|
|
|
u_mails = EmailAddress.objects.filter(user=a.user)
|
|
|
|
try:
|
|
|
|
clipper_mail = u_mails.get(email=email)
|
|
|
|
if clipper_mail.primary:
|
|
|
|
n_mails = u_mails.filter(primary=False)
|
|
|
|
if n_mails.exists():
|
|
|
|
n_mails[0].set_as_primary()
|
|
|
|
else:
|
|
|
|
user_email(a.user, '')
|
|
|
|
a.user.save()
|
|
|
|
clipper_mail.delete()
|
|
|
|
except EmailAddress.DoesNotExist:
|
|
|
|
pass
|
2018-04-22 15:31:41 +02:00
|
|
|
return
|
|
|
|
|
2018-04-22 17:22:03 +02:00
|
|
|
# The admission year is the same, we can update the model and keep
|
|
|
|
# the previous SocialAccount instance
|
2018-04-22 15:31:41 +02:00
|
|
|
a.provider = 'clipper'
|
|
|
|
a.save()
|
2018-04-22 17:22:03 +02:00
|
|
|
|
|
|
|
# Redo the thing that had failed just before
|
2018-04-22 15:31:41 +02:00
|
|
|
sociallogin.lookup()
|
2018-04-22 17:22:03 +02:00
|
|
|
|
2018-04-22 15:31:41 +02:00
|
|
|
except SocialAccount.DoesNotExist:
|
|
|
|
return
|
|
|
|
|
2018-04-22 17:22:03 +02:00
|
|
|
def get_username(self, clipper, data):
|
|
|
|
"""
|
|
|
|
Util function to generate a unique username, by default 'clipper@promo'
|
|
|
|
This is used to disambiguate and recognize if the person is the same
|
|
|
|
"""
|
|
|
|
return "{}@{}".format(clipper, data.get('annee', '00'))
|
|
|
|
|
|
|
|
def save_user(self, request, sociallogin, form=None):
|
|
|
|
user = sociallogin.user
|
|
|
|
user.set_unusable_password()
|
2018-04-22 15:31:41 +02:00
|
|
|
|
|
|
|
clipper = sociallogin.account.uid
|
2018-04-22 17:22:03 +02:00
|
|
|
ldap_data = self._ldap_data if hasattr(self, '_ldap_data') \
|
|
|
|
else get_ldap_infos(clipper)
|
|
|
|
|
|
|
|
username = self.get_username(clipper, ldap_data)
|
2018-04-22 15:31:41 +02:00
|
|
|
email = ldap_data.get('email')
|
|
|
|
name = ldap_data.get('name')
|
|
|
|
user_username(user, username or '')
|
|
|
|
user_email(user, email or '')
|
|
|
|
name_parts = (name or '').split(' ')
|
2018-04-22 17:22:03 +02:00
|
|
|
user_field(user, 'first_name', name_parts[0])
|
|
|
|
user_field(user, 'last_name', ' '.join(name_parts[1:]))
|
2018-04-23 00:01:27 +02:00
|
|
|
|
2018-04-22 17:22:03 +02:00
|
|
|
# Ignore form
|
|
|
|
get_account_adapter().populate_username(request, user)
|
|
|
|
|
|
|
|
# Save extra data (only once)
|
|
|
|
sociallogin.account.extra_data = sociallogin.extra_data = ldap_data
|
|
|
|
sociallogin.save(request)
|
|
|
|
sociallogin.account.save()
|
|
|
|
|
2018-04-22 15:31:41 +02:00
|
|
|
return user
|
|
|
|
|
|
|
|
def deprecate_clippers():
|
|
|
|
clippers = SocialAccount.objects.filter(provider='clipper')
|
|
|
|
c_uids = clippers.values_list('uid', flat=True)
|
|
|
|
|
|
|
|
# Clear old clipper accounts that wer replaced by new ones (o avoid conflicts)
|
2018-04-22 20:13:42 +02:00
|
|
|
SocialAccount.objects.filter(provider='clipper_inactive', uid__in=c_uids).delete()
|
2018-04-22 15:31:41 +02:00
|
|
|
|
|
|
|
# Deprecate accounts
|
2018-04-22 20:13:42 +02:00
|
|
|
clippers.update(provider='clipper_inactive')
|