summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Marillonnet <pmarillonnet@entrouvert.com>2017-06-28 20:16:05 (GMT)
committerPaul Marillonnet <pmarillonnet@entrouvert.com>2017-06-28 20:37:17 (GMT)
commite3238650273dd708a9022fc77fad02ee0d4d2a2c (patch)
treec123bef9ac544a25f28b8951fc1ffe07f01d0cdd
parent574904e80bf147f5b969c339174b5c7a78307ead (diff)
downloadpaul-synchro-e3238650273dd708a9022fc77fad02ee0d4d2a2c.zip
paul-synchro-e3238650273dd708a9022fc77fad02ee0d4d2a2c.tar.gz
paul-synchro-e3238650273dd708a9022fc77fad02ee0d4d2a2c.tar.bz2
POC Campus Condorcet : mise à jour du formulaire d'invitation
-rw-r--r--django/sp_sso/invite/forms.py17
-rw-r--r--django/sp_sso/invite/utils.py31
-rw-r--r--django/sp_sso/invite/views.py27
3 files changed, 58 insertions, 17 deletions
diff --git a/django/sp_sso/invite/forms.py b/django/sp_sso/invite/forms.py
index 455ac94..7ca07a2 100644
--- a/django/sp_sso/invite/forms.py
+++ b/django/sp_sso/invite/forms.py
@@ -1,7 +1,8 @@
from django.utils.translation import ugettext_lazy as _
from django import forms
-from .utils import get_invitaton_attributes_mapping
+from .utils import get_invitaton_attributes_mapping, get_additional_prefilled_fields
+
class InvitationForm(forms.Form):
@@ -12,7 +13,7 @@ class InvitationForm(forms.Form):
self.fields['email'].help_text = _(
_('Blank-separated emails if multiple adresses.'))
# Pre-filled fields are readonly
- for prefilled_field in get_invitaton_attributes_mapping().values():
+ for prefilled_field in get_invitaton_attributes_mapping().values() + get_additional_prefilled_fields():
if kwargs['initial'].get(prefilled_field):
self.fields[prefilled_field].widget.attrs['readonly'] = True
@@ -30,5 +31,17 @@ class InvitationForm(forms.Form):
hote_identite = forms.CharField(
max_length=100, label=_("Your RENATER identifier"))
+ hote_nom = forms.CharField(
+ max_length=100, label=_("Your lastname"))
+
+ hote_prenom = forms.CharField(
+ max_length=100, label=_("Your firstname"))
+
+ hote_etablissement = forms.CharField(
+ max_length=100, label=_("Your institution"))
+
+ hote_unite = forms.CharField(
+ max_length=100, label=_("Your research unit"))
+
hote_courriel = forms.CharField(
max_length=100, label=_("Your email address"))
diff --git a/django/sp_sso/invite/utils.py b/django/sp_sso/invite/utils.py
index d9921f5..dbe2ac5 100644
--- a/django/sp_sso/invite/utils.py
+++ b/django/sp_sso/invite/utils.py
@@ -1,6 +1,6 @@
import json
-from urllib2 import build_opener, HTTPHandler, Request
+from urllib2 import build_opener, HTTPHandler, Request, urlopen
pocform = 'invitation'
wcs = 'http://forms-condorcet.dev.entrouvert.org/'
@@ -8,11 +8,20 @@ posturl = wcs+'/api/formdefs/'+pocform+'/submit'
INVITATION_ATTRIBUTES_MAPPING = {
'ep_principal_name': 'hote_identite',
- 'email': 'hote_courriel'
+ 'email': 'hote_courriel',
+ 'prenom': 'hote_prenom',
+ 'nom': 'hote_nom'
}
+ADDITIONAL_PREFILLED_FIELDS = ['hote_etablissement', 'hote_unite']
+
+PASSERELLE_PEOPLE_QUERY = 'http://dir-condorcet.dev.entrouvert.org/ldapquery/condorcet/run/6/'
+
def get_invitaton_attributes_mapping():
- return INVITATION_ATTRIBUTES_MAPPING
+ return INVITATION_ATTRIBUTES_MAPPING.copy()
+
+def get_additional_prefilled_fields():
+ return ADDITIONAL_PREFILLED_FIELDS[:]
def do_invite(invitation):
opener = build_opener(HTTPHandler)
@@ -33,3 +42,19 @@ def do_invite(invitation):
ret = -1
return ret
+
+
+def get_affectations_from_eppn(eppn):
+ if eppn:
+ try:
+ ldapquery = urlopen(PASSERELLE_PEOPLE_QUERY)
+ jsonresult = json.loads(ldapquery.read())
+
+ for user in jsonresult['content']:
+ if user.get('attributes', {}).get('eduPersonPrincipalName') == eppn:
+ return (user['attributes'].get('supannEtablissement', [''])[0],
+ user['attributes'].get('supannEntiteAffectation', [''])[0])
+ raise Exception('Invalid user')
+ # fall back in except block
+ except:
+ return ('', '')
diff --git a/django/sp_sso/invite/views.py b/django/sp_sso/invite/views.py
index 9ac1f68..463f450 100644
--- a/django/sp_sso/invite/views.py
+++ b/django/sp_sso/invite/views.py
@@ -1,12 +1,14 @@
+import logging
from django.views.generic import FormView
from django.shortcuts import render, redirect
from django.utils.translation import ugettext as _
from .forms import InvitationForm
-from .utils import do_invite, get_invitaton_attributes_mapping
-from saml.utils import ldap_contains_user
+from .utils import do_invite, get_invitaton_attributes_mapping, get_affectations_from_eppn
+
+
+logger = logging.getLogger('django')
-# Create your views here.
def invitation_sent(request):
return render(
@@ -30,18 +32,18 @@ class InvitationFormView(FormView):
# User is logged in the identity provider
if 'mellon_session' in self.request.session:
+ data = self.request.session['mellon_session']
# Fetch SSO attributes
for attribute_key, attribute_value in \
get_invitaton_attributes_mapping().items():
+ if data.get(attribute_key):
+ attribute_element = data.get(attribute_key)[0]
+ initial[attribute_value] = attribute_element
- attribute_element = self.request.session['mellon_session'].get(
- attribute_key,
- ['']
- )[0]
- initial[attribute_value] = attribute_element
+ etablissement, unite = get_affectations_from_eppn(data.get('ep_principal_name',[None])[0])
- initial['hote_etablissement'] = 'null'
- initial['hote_unite'] = 'null'
+ initial['hote_etablissement'] = etablissement
+ initial['hote_unite'] = unite
initial['hote_commentaire'] = '''EduPersonPrincipalName de
l\'invitant'''
return initial
@@ -49,13 +51,14 @@ class InvitationFormView(FormView):
def form_valid(self, form):
invitation = {}
post_dict = self.request.POST
+ logger.info("Invitation POST data : {}".format(''))
hote_identite = post_dict.get('hote_identite')
# User cannot invite if she's not in the destination directory yet
- if not hote_identite or not ldap_contains_user(
+ """if not hote_identite or not ldap_contains_user(
{'ep_principal_name':hote_identite}):
- return redirect('not_registered')
+ return redirect('not_registered')"""
# Craft any relevant invitation data
for form_attribute in self.form_class.base_fields.keys():