From 5726ff2692a6b4f47dc4374754da74845e6aba17 Mon Sep 17 00:00:00 2001 From: Evarin Date: Wed, 29 Jan 2020 15:17:57 +0100 Subject: [PATCH] Hotfix allauth deprecation clipper --- avisstage/allauth_adapter.py | 39 +++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/avisstage/allauth_adapter.py b/avisstage/allauth_adapter.py index 130b71d..1935e47 100644 --- a/avisstage/allauth_adapter.py +++ b/avisstage/allauth_adapter.py @@ -1,5 +1,6 @@ from allauth.account.adapter import DefaultAccountAdapter -from allauth_ens.adapter import LongTermClipperAccountAdapter +from allauth.socialaccount.models import SocialAccount +from allauth_ens.adapter import LongTermClipperAccountAdapter, get_ldap_infos class AccountAdapter(DefaultAccountAdapter): def is_open_for_signup(self, request): @@ -18,3 +19,39 @@ class SocialAccountAdapter(LongTermClipperAccountAdapter): # See https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/adapter.py return super().is_open_for_signup(request, sociallogin) + + # TODO : HOTFIX pour un bug d'allauth_ens + # On remplace la déduplication des comptes faites avec "entrance_year" + # par une déduplication sur le nom d'utilisateur + # (Copié de allauth_ens) + def pre_social_login(self, request, sociallogin): + if sociallogin.account.provider != "clipper": + return super(LongTermClipperAccountAdapter, + self).pre_social_login(request, sociallogin) + + clipper_uid = sociallogin.account.uid + try: + old_conn = SocialAccount.objects.get(provider='clipper_inactive', + uid=clipper_uid) + except SocialAccount.DoesNotExist: + return + + ldap_data = get_ldap_infos(clipper_uid) + sociallogin._ldap_data = ldap_data + + if ldap_data is None or 'entrance_year' not in ldap_data: + raise ValueError("No entrance year in LDAP data") + + old_conn_username = old_conn.user.username + + # HOTFIX ICI + if self.get_username(clipper_uid, ldap_data) != old_conn_username: + email = ldap_data.get('email', get_clipper_email(clipper_uid)) + remove_email(old_conn.user, email) + + return + + old_conn.provider = 'clipper' + old_conn.save() + + sociallogin.lookup()