summaryrefslogtreecommitdiffstats
path: root/django/sp_sso/invite/utils.py
blob: 4f49b39512260ce1e63d51d621e7a60d33528266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import json
import logging

from urllib2 import build_opener, HTTPHandler, Request, urlopen

pocform = 'invitation'
wcs = 'http://forms-condorcet.dev.entrouvert.org/'
posturl = wcs+'/api/formdefs/'+pocform+'/submit'

INVITATION_ATTRIBUTES_MAPPING = {
    'ep_principal_name': 'hote_identite',
    '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/'

logger = logging.getLogger('django')

def get_invitaton_attributes_mapping():
    """ Full copy of the invitation attributes mapping dictionary """
    return INVITATION_ATTRIBUTES_MAPPING.copy()

def get_additional_prefilled_fields():
    """
    Returns a full copy of the additional fields to be prefilled with the
    user's fetched attributes.
    """
    return ADDITIONAL_PREFILLED_FIELDS[:]

def do_invite(invitation):
    """
    Sends invitation data to the invitation w.c.s. form.
    Invitations will then be sent as part of the w.c.s. workflow.
    """
    opener = build_opener(HTTPHandler)
    # Generate a JSON to bind against the wcs ReST API
    form = {}
    # Forge a HTTP POST request
    form["data"] = invitation
    data = json.dumps(form)

    req = Request(posturl, data)
    req.add_header("Content-Type", "application/json")
    req.add_header("Accept", "application/json")

    ret = 0
    try:
        opener.open(req)
    except :
        ret = -1

    return ret


def get_affectations_from_eppn(eppn):
    """
    Returns a tuple (<Institution identifier>, <Researcher unit identifier>)
    for a given eduPersonPrincipalName (a.k.a. `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 Exception, e:
            logger.error('%s' % e)
            return ('', '')