2018-04-22 15:31:41 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-04-24 00:19:42 +02:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
2018-04-29 01:28:09 +02:00
|
|
|
from allauth.account.utils import user_email, user_field, user_username
|
|
|
|
from allauth.socialaccount.adapter import (
|
|
|
|
DefaultSocialAccountAdapter, get_account_adapter, get_adapter,
|
|
|
|
)
|
|
|
|
from allauth.socialaccount.models import SocialAccount
|
2018-04-22 20:13:42 +02:00
|
|
|
|
2018-04-29 01:28:09 +02:00
|
|
|
import ldap
|
|
|
|
|
2018-09-29 23:32:50 +02:00
|
|
|
from .utils import (
|
|
|
|
extract_infos_from_ldap, get_clipper_email, get_ldap_infos, init_ldap,
|
|
|
|
remove_email,
|
|
|
|
)
|
2018-04-22 15:31:41 +02:00
|
|
|
|
2018-06-03 22:10:34 +02:00
|
|
|
User = get_user_model()
|
2018-04-22 15:31:41 +02:00
|
|
|
|
2018-09-29 23:32:50 +02:00
|
|
|
|
2018-04-22 15:31:41 +02:00
|
|
|
class LongTermClipperAccountAdapter(DefaultSocialAccountAdapter):
|
|
|
|
"""
|
2018-04-29 01:28:09 +02:00
|
|
|
A class to manage the fact that people loose their account at the end of
|
2018-04-22 15:31:41 +02:00
|
|
|
their scolarity and that their clipper login might be reused later
|
|
|
|
"""
|
|
|
|
|
|
|
|
def pre_social_login(self, request, sociallogin):
|
2018-06-03 22:10:34 +02:00
|
|
|
"""
|
|
|
|
If a clipper connection has already existed with the uid, it checks
|
|
|
|
that this connection still belongs to the user it was associated with.
|
|
|
|
|
2018-06-16 17:02:32 +02:00
|
|
|
This check is performed by comparing the entrance years provided by the
|
|
|
|
LDAP.
|
2018-06-03 22:10:34 +02:00
|
|
|
|
|
|
|
If the check succeeds, it simply reactivates the clipper connection as
|
|
|
|
belonging to the associated user.
|
|
|
|
|
|
|
|
If the check fails, it frees the elements (as the clipper email
|
|
|
|
address) which will be assigned to the new connection later.
|
|
|
|
"""
|
|
|
|
|
2018-04-24 00:19:42 +02:00
|
|
|
if sociallogin.account.provider != "clipper":
|
2018-04-29 01:28:09 +02:00
|
|
|
return super(LongTermClipperAccountAdapter,
|
|
|
|
self).pre_social_login(request, sociallogin)
|
|
|
|
|
2018-06-03 22:10:34 +02:00
|
|
|
clipper_uid = sociallogin.account.uid
|
2018-04-22 15:31:41 +02:00
|
|
|
try:
|
2018-06-03 22:10:34 +02:00
|
|
|
old_conn = SocialAccount.objects.get(provider='clipper_inactive',
|
|
|
|
uid=clipper_uid)
|
2018-04-22 15:31:41 +02:00
|
|
|
except SocialAccount.DoesNotExist:
|
|
|
|
return
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-28 16:26:52 +02:00
|
|
|
# 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
|
2018-06-03 22:10:34 +02:00
|
|
|
ldap_data = get_ldap_infos(clipper_uid)
|
2018-04-28 16:26:52 +02:00
|
|
|
sociallogin._ldap_data = ldap_data
|
|
|
|
|
2018-06-16 17:02:32 +02:00
|
|
|
if ldap_data is None or 'entrance_year' not in ldap_data:
|
|
|
|
raise ValueError("No entrance year in LDAP data")
|
|
|
|
|
|
|
|
old_conn_entrance_year = (
|
|
|
|
old_conn.extra_data.get('ldap', {}).get('entrance_year'))
|
|
|
|
|
|
|
|
if old_conn_entrance_year != ldap_data['entrance_year']:
|
2018-06-03 22:10:34 +02:00
|
|
|
# We cannot reuse this SocialAccount, so we need to invalidate
|
|
|
|
# the email address of the previous user to prevent conflicts
|
|
|
|
# if a new SocialAccount is created
|
|
|
|
email = ldap_data.get('email', get_clipper_email(clipper_uid))
|
|
|
|
remove_email(old_conn.user, email)
|
2018-09-29 23:32:50 +02:00
|
|
|
|
2018-04-28 16:26:52 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
# The admission year is the same, we can update the model and keep
|
|
|
|
# the previous SocialAccount instance
|
2018-06-03 22:10:34 +02:00
|
|
|
old_conn.provider = 'clipper'
|
|
|
|
old_conn.save()
|
2018-04-28 16:26:52 +02:00
|
|
|
|
|
|
|
# Redo the thing that had failed just before
|
|
|
|
sociallogin.lookup()
|
2018-04-22 15:31:41 +02:00
|
|
|
|
2018-06-03 22:10:34 +02:00
|
|
|
def get_username(self, clipper_uid, data):
|
2018-04-22 17:22:03 +02:00
|
|
|
"""
|
|
|
|
Util function to generate a unique username, by default 'clipper@promo'
|
|
|
|
"""
|
2018-06-03 22:10:34 +02:00
|
|
|
if data is None or 'entrance_year' not in data:
|
2018-04-29 01:28:09 +02:00
|
|
|
raise ValueError("No entrance year in LDAP data")
|
2018-06-03 22:10:34 +02:00
|
|
|
return "{}@{}".format(clipper_uid, data['entrance_year'])
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-22 17:22:03 +02:00
|
|
|
def save_user(self, request, sociallogin, form=None):
|
2018-04-24 00:19:42 +02:00
|
|
|
if sociallogin.account.provider != "clipper":
|
2018-04-29 01:28:09 +02:00
|
|
|
return super(LongTermClipperAccountAdapter,
|
|
|
|
self).save_user(request, sociallogin, form)
|
2018-04-22 17:22:03 +02:00
|
|
|
user = sociallogin.user
|
|
|
|
user.set_unusable_password()
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-06-03 22:10:34 +02:00
|
|
|
clipper_uid = sociallogin.account.uid
|
2018-09-29 23:32:50 +02:00
|
|
|
ldap_data = sociallogin._ldap_data if \
|
|
|
|
hasattr(sociallogin, '_ldap_data') \
|
|
|
|
else get_ldap_infos(clipper_uid)
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-06-03 22:10:34 +02:00
|
|
|
username = self.get_username(clipper_uid, ldap_data)
|
|
|
|
email = ldap_data.get('email', get_clipper_email(clipper_uid))
|
2018-04-22 15:31:41 +02:00
|
|
|
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-29 01:28:09 +02:00
|
|
|
|
2018-06-03 22:26:53 +02:00
|
|
|
# Entrance year and department, if the user has these fields
|
|
|
|
entrance_year = ldap_data.get('entrance_year', '')
|
|
|
|
dep_code = ldap_data.get('department_code', '')
|
|
|
|
dep_fancy = ldap_data.get('department', '')
|
|
|
|
promotion = u'%s %s' % (dep_fancy, entrance_year)
|
|
|
|
user_field(user, 'entrance_year', entrance_year)
|
|
|
|
user_field(user, 'department_code', dep_code)
|
|
|
|
user_field(user, 'department', dep_fancy)
|
|
|
|
user_field(user, 'promotion', promotion)
|
|
|
|
|
2018-04-22 17:22:03 +02:00
|
|
|
# Ignore form
|
|
|
|
get_account_adapter().populate_username(request, user)
|
|
|
|
|
|
|
|
# Save extra data (only once)
|
2018-06-16 17:02:32 +02:00
|
|
|
sociallogin.account.extra_data['ldap'] = ldap_data
|
2018-04-22 17:22:03 +02:00
|
|
|
sociallogin.save(request)
|
|
|
|
sociallogin.account.save()
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-22 15:31:41 +02:00
|
|
|
return user
|
|
|
|
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-22 15:31:41 +02:00
|
|
|
def deprecate_clippers():
|
2018-04-24 00:19:42 +02:00
|
|
|
"""
|
2018-04-29 01:28:09 +02:00
|
|
|
Marks all the SocialAccount with clipper as deprecated, by setting their
|
2018-04-24 00:19:42 +02:00
|
|
|
provider to 'clipper_inactive'
|
|
|
|
"""
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-22 15:31:41 +02:00
|
|
|
clippers = SocialAccount.objects.filter(provider='clipper')
|
|
|
|
c_uids = clippers.values_list('uid', flat=True)
|
2018-04-29 01:28:09 +02:00
|
|
|
|
|
|
|
# Clear old clipper accounts that were replaced by new ones
|
|
|
|
# (to avoid conflicts)
|
|
|
|
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')
|
2018-04-24 00:19:42 +02:00
|
|
|
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-10-21 23:21:47 +02:00
|
|
|
def install_longterm_adapter(fake=False, accounts=None, keep_usernames=False):
|
2018-04-24 00:19:42 +02:00
|
|
|
"""
|
2018-04-29 01:28:09 +02:00
|
|
|
Manages the transition from an older django_cas or an allauth_ens
|
|
|
|
installation without LongTermClipperAccountAdapter
|
2018-10-21 22:27:15 +02:00
|
|
|
|
|
|
|
accounts is an optional dictionary containing the association between
|
|
|
|
clipper usernames and django's User accounts. If not provided, the
|
|
|
|
function will assumer Users' usernames are their clipper uid.
|
2018-04-24 00:19:42 +02:00
|
|
|
"""
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-10-21 22:27:15 +02:00
|
|
|
if accounts is None:
|
|
|
|
accounts = {u.username: u for u in User.objects.all()
|
|
|
|
if u.username.isalnum()}
|
|
|
|
|
2018-09-29 23:32:50 +02:00
|
|
|
ldap_connection = init_ldap()
|
2018-04-24 00:19:42 +02:00
|
|
|
ltc_adapter = get_adapter()
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-09-29 23:32:50 +02:00
|
|
|
info = ldap_connection.search_s(
|
|
|
|
'dc=spi,dc=ens,dc=fr',
|
|
|
|
ldap.SCOPE_SUBTREE,
|
|
|
|
("(|{})".format(''.join(("(uid=%s)" % (un,))
|
|
|
|
for un in accounts.keys()))),
|
|
|
|
['uid',
|
|
|
|
'cn',
|
|
|
|
'mailRoutingAddress',
|
|
|
|
'homeDirectory'])
|
2018-04-24 00:19:42 +02:00
|
|
|
|
|
|
|
logs = {"created": [], "updated": []}
|
|
|
|
cases = []
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-24 00:19:42 +02:00
|
|
|
for userinfo in info:
|
|
|
|
infos = userinfo[1]
|
2018-06-03 22:10:34 +02:00
|
|
|
data = extract_infos_from_ldap(infos)
|
|
|
|
clipper_uid = data['clipper_uid']
|
|
|
|
user = accounts.get(clipper_uid, None)
|
2018-04-24 00:19:42 +02:00
|
|
|
if user is None:
|
|
|
|
continue
|
2018-10-21 23:21:47 +02:00
|
|
|
|
|
|
|
if not keep_usernames:
|
|
|
|
user.username = ltc_adapter.get_username(clipper_uid, data)
|
|
|
|
|
2018-04-24 00:19:42 +02:00
|
|
|
if fake:
|
2018-06-03 22:10:34 +02:00
|
|
|
cases.append(clipper_uid)
|
2018-04-24 00:19:42 +02:00
|
|
|
else:
|
|
|
|
user.save()
|
|
|
|
cases.append(user.username)
|
2018-06-16 17:02:32 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
sa = SocialAccount.objects.get(provider='clipper', uid=clipper_uid)
|
|
|
|
if not sa.extra_data.get('ldap'):
|
|
|
|
sa.extra_data['ldap'] = data
|
|
|
|
if not fake:
|
|
|
|
sa.save(update_fields=['extra_data'])
|
|
|
|
logs["updated"].append((clipper_uid, user.username))
|
|
|
|
except SocialAccount.DoesNotExist:
|
|
|
|
sa = SocialAccount(
|
|
|
|
provider='clipper', uid=clipper_uid,
|
|
|
|
user=user, extra_data={'ldap': data},
|
|
|
|
)
|
|
|
|
if not fake:
|
|
|
|
sa.save()
|
|
|
|
logs["created"].append((clipper_uid, user.username))
|
2018-04-29 01:28:09 +02:00
|
|
|
|
|
|
|
logs["unmodified"] = User.objects.exclude(username__in=cases)\
|
|
|
|
.values_list("username", flat=True)
|
2018-04-24 00:19:42 +02:00
|
|
|
return logs
|