Allauth pour gérer les archicubes

This commit is contained in:
Evarin 2018-04-19 12:34:52 +02:00
parent 377cad94ae
commit 78f1013ab1
9 changed files with 127 additions and 0 deletions

View file

View file

@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
# Register your models here.

8
allauth_archiens/apps.py Normal file
View file

@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.apps import AppConfig
class AllauthArchiensConfig(AppConfig):
name = 'allauth_archiens'

View file

View file

@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.

View file

@ -0,0 +1,80 @@
from allauth_ens.providers.clipper.provider import ClipperProvider
import ldap
class LongTermClipperProvider(ClipperProvider):
id = 'longterm_clipper'
def extract_uid(self, data):
# Normalize UID
uid, _ = data
uid = uid.lower().strip()
more_data = self.get_ldap_infos(uid)
uid = "%s_%s" % (uid, more_data.get("annee", "00"))
print(uid)
return uid
def get_ldap_infos(self, clipper):
if hasattr(self, "_ldap_infos"):
return self._ldap_infos
data = {}
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"),
str("mailRoutingAddress"),
str("homeDirectory") ])
if len(info) > 0:
infos = info[0][1]
# Nom
data['nom'] = infos.get('cn', [''])[0].decode("utf-8")
# Parsing du homeDirectory pour la promotion
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(DEPARTEMENTS_DEFAUT).get(dep.lower(), '')
promotion = u'%s %s' % (dep, annee)
data['annee'] = annee
data['promotion'] = promotion
# Mail
pmail = infos.get('mailRoutingAddress', [])
data['email'] = '{}@clipper.ens.fr'.format(clipper.strip().lower()) if len(pmail) == 0 else pmail[0]
except ldap.LDAPError:
pass
self._ldap_infos = data
return data
def extract_common_fields(self, data):
common = super(ClipperProvider, self).extract_common_fields(data)
infos = self.get_ldap_infos(common['username'])
common['email'] = infos.get('email',
'{}@clipper.ens.fr'.format(common['username'].strip().lower()))
common['name'] = infos.get('nom', common['username'])
return common
provider_classes = [LongTermClipperProvider]

View file

@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.test import TestCase
# Create your tests here.

6
allauth_archiens/urls.py Normal file
View file

@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from allauth_cas.urls import default_urlpatterns
from .provider import LongTermClipperProvider
urlpatterns = default_urlpatterns(LongTermClipperProvider)

15
allauth_archiens/views.py Normal file
View file

@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
from allauth_cas import views
from .provider import LongTermClipperProvider
class LongTermClipperCASAdapter(views.CASAdapter):
provider_id = LongTermClipperProvider.id
url = 'https://cas.eleves.ens.fr'
version = 3
login = views.CASLoginView.adapter_view(LongTermClipperCASAdapter)
callback = views.CASCallbackView.adapter_view(LongTermClipperCASAdapter)
logout = views.CASLogoutView.adapter_view(LongTermClipperCASAdapter)