Qwann/small fixes #7

Merged
narmanli merged 5 commits from Qwann/small_fixes into master 2018-04-05 23:17:00 +02:00
3 changed files with 38 additions and 2 deletions

View file

@ -1,9 +1,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import ldap
from allauth.account.models import EmailAddress from allauth.account.models import EmailAddress
from allauth.socialaccount.providers.base import ProviderAccount from allauth.socialaccount.providers.base import ProviderAccount
from allauth_cas.providers import CASProvider from allauth_cas.providers import CASProvider
from django.conf import settings
class ClipperAccount(ProviderAccount): class ClipperAccount(ProviderAccount):
pass pass
@ -19,8 +22,40 @@ class ClipperProvider(CASProvider):
return '{}@clipper.ens.fr'.format(uid.strip().lower()) return '{}@clipper.ens.fr'.format(uid.strip().lower())
def extract_common_fields(self, data): def extract_common_fields(self, data):
def get_names(clipper):
assert clipper.isalnum()
try:
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT,
ldap.OPT_X_TLS_NEVER)
l = ldap.initialize("ldaps://ldap.spi.ens.fr:636")
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)
info = l.search_s('dc=spi,dc=ens,dc=fr',
ldap.SCOPE_SUBTREE,
('(uid=%s)' % (clipper,)),
[str("cn"), ])
if len(info) > 0:
fullname = info[0][1].get('cn', [''])[0].decode("utf-8")
first_name, last_name = fullname.split(' ', 1)
return first_name, last_name
except ldap.LDAPError:
pass
return '', ''
common = super(ClipperProvider, self).extract_common_fields(data) common = super(ClipperProvider, self).extract_common_fields(data)
fn, ln = get_names(common['username'])
common['email'] = self.extract_email(data) common['email'] = self.extract_email(data)
common['name'] = fn
common['last_name'] = ln
return common return common
def extract_email_addresses(self, data): def extract_email_addresses(self, data):

View file

@ -84,7 +84,7 @@ AUTH_PASSWORD_VALIDATORS = [
{ {
'NAME': ( 'NAME': (
'django.contrib.auth.password_validation' 'django.contrib.auth.password_validation'
'.UserAttributeSimilarityValidator', '.UserAttributeSimilarityValidator'
), ),
}, },
{ {

View file

@ -47,5 +47,6 @@ setup(
'django-allauth', 'django-allauth',
'django-allauth-cas>=1.0.0b2,<1.1', 'django-allauth-cas>=1.0.0b2,<1.1',
'django-widget-tweaks', 'django-widget-tweaks',
'python-ldap',
], ],
) )