Compare commits

...

3 commits

Author SHA1 Message Date
Evarin
0065d0cdec Intégration allauth 2018-04-19 12:35:26 +02:00
Evarin
78f1013ab1 Allauth pour gérer les archicubes 2018-04-19 12:34:52 +02:00
Evarin
377cad94ae Début intégration allauth 2018-04-18 23:05:15 +02:00
13 changed files with 171 additions and 8 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)

View file

@ -9,7 +9,7 @@ v1_api.register(api.AuteurResource())
urlpatterns = [ urlpatterns = [
url(r'^$', views.index, name='index'), url(r'^$', views.index, name='index'),
url(r'^perso/$', views.perso, name='perso'), url(r'^compte/profil/$', views.perso, name='perso'),
url(r'^faq/$', views.faq, name='faq'), url(r'^faq/$', views.faq, name='faq'),
url(r'^stage/nouveau/$', views.manage_stage, name='stage_ajout'), url(r'^stage/nouveau/$', views.manage_stage, name='stage_ajout'),
url(r'^stage/(?P<pk>\w+)/$', views.StageView.as_view(), name='stage'), url(r'^stage/(?P<pk>\w+)/$', views.StageView.as_view(), name='stage'),

View file

@ -1,3 +1,4 @@
#coding: utf-8
""" """
Django settings for experiENS project. Django settings for experiENS project.
@ -11,6 +12,7 @@ https://docs.djangoproject.com/en/1.7/ref/settings/
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os import os
from django.core.urlresolvers import reverse_lazy from django.core.urlresolvers import reverse_lazy
from django.contrib import messages
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) BASE_DIR = os.path.dirname(os.path.dirname(__file__))
@ -29,11 +31,20 @@ INSTALLED_APPS = (
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django.contrib.gis', 'django.contrib.gis',
'django.contrib.sites',
'django_elasticsearch_dsl', 'django_elasticsearch_dsl',
'widget_tweaks',
'allauth_ens',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth_cas',
# 'allauth_ens.providers.clipper',
'allauth_archiens',
'tastypie', 'tastypie',
'django_cas_ng',
'braces', 'braces',
'tinymce', 'tinymce',
'taggit', 'taggit',
@ -67,6 +78,7 @@ TEMPLATES = [
'django.template.context_processors.static', 'django.template.context_processors.static',
'django.template.context_processors.tz', 'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages', 'django.contrib.messages.context_processors.messages',
'django.template.context_processors.request',
], ],
}, },
}, },
@ -101,9 +113,12 @@ STATIC_URL = '/static/'
AUTHENTICATION_BACKENDS = ( AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend', 'django.contrib.auth.backends.ModelBackend',
'experiENS.auth.ENSCASBackend', 'allauth.account.auth_backends.AuthenticationBackend',
# 'experiENS.auth.ENSCASBackend',
) )
SITE_ID = 1
CAS_SERVER_URL = "https://cas.eleves.ens.fr/" #SPI CAS CAS_SERVER_URL = "https://cas.eleves.ens.fr/" #SPI CAS
CAS_VERIFY_URL = "https://cas.eleves.ens.fr/" CAS_VERIFY_URL = "https://cas.eleves.ens.fr/"
CAS_IGNORE_REFERER = True CAS_IGNORE_REFERER = True
@ -114,3 +129,19 @@ CAS_VERSION = 'CAS_2_SAML_1_0'
LOGIN_URL = reverse_lazy('login') LOGIN_URL = reverse_lazy('login')
LOGOUT_URL = reverse_lazy('logout') LOGOUT_URL = reverse_lazy('logout')
LOGIN_REDIRECT_URL = reverse_lazy('avisstage:perso')
ACCOUNT_HOME_URL = reverse_lazy('avisstage:perso')
ACCOUNT_DETAILS_URL = reverse_lazy('avisstage:profil_edit')
SOCIALACCOUNT_PROVIDERS = {
# …
'clipper': {
# These settings control whether a message containing a link to
# disconnect from the CAS server is added when users log out.
'MESSAGE_SUGGEST_LOGOUT_ON_LOGOUT': True,
'MESSAGE_SUGGEST_LOGOUT_ON_LOGOUT_LEVEL': messages.INFO,
},
}

View file

@ -2,13 +2,17 @@ from django.conf import settings
from django.conf.urls import include, url from django.conf.urls import include, url
from django.contrib import admin from django.contrib import admin
from django_cas_ng import views as django_cas_views #from django_cas_ng import views as django_cas_views
from allauth_ens.views import capture_login, capture_logout
urlpatterns = [ urlpatterns = [
url(r'^', include('avisstage.urls', namespace='avisstage')), url(r'^', include('avisstage.urls', namespace='avisstage')),
url(r'^compte/', include('allauth.urls')),
url(r'^login/$', django_cas_views.login, name = "login"), url(r'^login/$', capture_login, name="login"),
url(r'^logout/$', django_cas_views.logout, name = "logout"), url(r'^logout/$', capture_logout, name="logout"),
# url(r'^login/$', django_cas_views.login, name = "login"),
# url(r'^logout/$', django_cas_views.logout, name = "logout"),
url(r'^tinymce/', include('tinymce.urls')), url(r'^tinymce/', include('tinymce.urls')),
url(r'^taggit_autosuggest/', include('taggit_autosuggest.urls')), url(r'^taggit_autosuggest/', include('taggit_autosuggest.urls')),
url(r'^admin/', include(admin.site.urls)), url(r'^admin/', include(admin.site.urls)),

View file

@ -1,4 +1,4 @@
django django==1.11.*
django-cas-ng django-cas-ng
django-taggit django-taggit
python-ldap python-ldap
@ -8,4 +8,5 @@ django-taggit-autosuggest
pytz pytz
django-tastypie django-tastypie
lxml lxml
git+https://github.com/sabricot/django-elasticsearch-dsl git+https://github.com/sabricot/django-elasticsearch-dsl#egg=django_elasticsearch_dsl
django-allauth-ens