Projet

Général

Profil

0001-archimed-user-account-webservice-9517.patch

Josué Kouka, 20 janvier 2016 21:14

Télécharger (9,08 ko)

Voir les différences:

Subject: [PATCH] archimed user account webservice (#9517)

 README                            |  5 +++
 mandayejs/applications.py         |  4 ++
 mandayejs/mandaye/utils.py        | 12 ++++++
 mandayejs/mandaye/views.py        | 11 ++---
 mandayejs/settings.py             |  1 +
 mandayejs/sites/archimed/urls.py  | 23 +++++++++++
 mandayejs/sites/archimed/views.py | 84 +++++++++++++++++++++++++++++++++++++++
 mandayejs/urls.py                 |  6 +++
 8 files changed, 138 insertions(+), 8 deletions(-)
 create mode 100644 mandayejs/sites/archimed/urls.py
 create mode 100644 mandayejs/sites/archimed/views.py
README
147 147
        # if user is connected and already associated
148 148
        SITE_FORCE_REDIRECT_LOCATOR = '#logon-form'
149 149

  
150
        # Application's webservices
151
        SITE_WEB_SERVICES = {
152
            'products': '/products/id',
153
        }
154

  
150 155

  
151 156
About Statics
152 157
-------------
mandayejs/applications.py
137 137
    ]
138 138

  
139 139
    SITE_FORCE_REDIRECT_LOCATOR = '.connectBox'
140

  
141
    SITE_WEB_SERVICES = {
142
        'account_details': '/DEFAULT/Ermes/Services/ILSClient.svc/RetrieveAccount',
143
    }
mandayejs/mandaye/utils.py
54 54
    except (IndexError,):
55 55
        return None
56 56

  
57
def get_login_info(request, credentials):
58
    """Returns 
59
    """
60
    app_settings = get_app_settings()
61

  
62
    return {
63
        'address': request.build_absolute_uri(app_settings.SITE_LOGIN_PATH),
64
        'cookies': [],
65
        'locators': [ credentials.to_login_info() ],
66
        'auth_checker': app_settings.SITE_AUTH_CHECKER,
67
        'form_submit_element': app_settings.SITE_FORM_SUBMIT_ELEMENT
68
    }
mandayejs/mandaye/views.py
41 41

  
42 42
from .models import UserCredentials
43 43
from mandayejs.mandaye.forms import FormFactory
44
from mandayejs.mandaye.utils import exec_phantom, cookie_builder
44
from mandayejs.mandaye.utils import exec_phantom, cookie_builder, get_login_info
45 45
from mandayejs.applications import get_app_settings
46 46

  
47 47
app_settings = get_app_settings()
......
140 140
    except (UserCredentials.DoesNotExist,):
141 141
        return HttpResponseRedirect(resolve_url('associate'))
142 142

  
143
    login_info = {
144
        'address': request.build_absolute_uri(app_settings.SITE_LOGIN_PATH),
145
        'cookies': [],
146
        'locators': [ credentials.to_login_info() ],
147
        'auth_checker': getattr(app_settings, 'SITE_AUTH_CHECKER'),
148
        'form_submit_element': getattr(app_settings, 'SITE_FORM_SUBMIT_ELEMENT')
149
    }
143
    login_info = get_login_info(request, credentials)
144

  
150 145
    logger.debug(login_info)
151 146
    login_info['locators'] = [ credentials.to_login_info(decrypt=True)]
152 147
    result = exec_phantom(login_info)
mandayejs/settings.py
54 54
    'django.contrib.sessions',
55 55
    'django.contrib.messages',
56 56
    'django.contrib.staticfiles',
57
    'rest_framework',
57 58
    'mandayejs.mandaye',
58 59
    'mandayejs.sites.duonet',
59 60
    'mandayejs.sites.archimed',
mandayejs/sites/archimed/urls.py
1
# mandayejs - saml reverse proxy
2
# Copyright (C) 2015  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from django.conf.urls import patterns, include, url
18

  
19
from mandayejs.sites.archimed.views import account
20

  
21
urlpatterns = patterns('',
22
    url(r'account/(?P<username>[\w+]*)/$', account, name='archimed-account-detail'),
23
)
mandayejs/sites/archimed/views.py
1
# mandayejs - saml reverse proxy
2
# Copyright (C) 2015  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from __future__ import absolute_import
18

  
19
import logging
20
import requests
21
from requests.cookies import RequestsCookieJar
22

  
23
from django.contrib.auth.models import User
24
from django.shortcuts import get_object_or_404
25
from django.core.exceptions import ImproperlyConfigured
26

  
27
from rest_framework import status
28
from rest_framework.views import APIView
29
from rest_framework.response import Response
30

  
31
from mandayejs.mandaye.models import UserCredentials
32
from mandayejs.mandaye.utils import exec_phantom, cookie_builder, get_login_info
33
from mandayejs.applications import get_app_settings
34

  
35

  
36
class AccountDetails(APIView):
37
    """Archimed user's account details
38
    """
39

  
40
    def get(self, request, *args, **kwargs):
41
        app_settings = get_app_settings()
42
      
43
        try:
44
            ws_uri = app_settings.SITE_WEB_SERVICES['account_details']
45
        except (AttributeError,):
46
            raise ImproperlyConfigured(
47
                    'No SITE_WEB_SERVICES defined in your AppSettings') 
48

  
49
        username = kwargs['username']
50
        user = get_object_or_404(User, username=username)
51
        credentials = get_object_or_404(UserCredentials, user=user)
52

  
53
        login_info = get_login_info(request, credentials)
54
        login_info['locators'] = [ credentials.to_login_info(decrypt=True)]
55
        result = exec_phantom(login_info)
56

  
57
        if result.get('result') != 'ok':
58
            return Response(status=status.HTTP_401_UNAUTHORIZED)
59

  
60
        session = requests.session()
61
        r_cookies = RequestsCookieJar()
62

  
63
        for cookie in result.get('cookies'):
64
            r_cookies.set(
65
                cookie['name'],
66
                cookie['value'],
67
                domain=cookie['domain'],
68
                path=cookie['path'],
69
                secure=cookie['secure']
70
            )
71
        session.cookies = r_cookies
72

  
73
        headers = {
74
            'Content-Type': 'application/json',
75
        }
76
        content = '{"codeConfig":"", "xslPath":"Services/LectorShortAccount.xslt"}'
77
        url = request.build_absolute_uri(ws_uri)
78

  
79
        request_response = session.post(url, headers=headers, data=content, verify=False) 
80
        
81
        return Response(request_response)
82

  
83

  
84
account = AccountDetails.as_view()
mandayejs/urls.py
13 13
#
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
from __future__ import absolute_import
16 17

  
17 18
from django.conf.urls import patterns, include, url
18 19
from django.contrib import admin
19 20
from django.conf import settings
20 21

  
22

  
21 23
urlpatterns = patterns('',
22 24
    url(r'^_mandaye/panel$', 'mandayejs.mandaye.views.panel', name='panel'),
23 25
    url(r'^_mandaye/associate/$', 'mandayejs.mandaye.views.associate', name='associate'),
......
30 32
if 'mellon' in settings.INSTALLED_APPS:
31 33
    urlpatterns += patterns('', url(r'^_mandaye/accounts/mellon/', include('mellon.urls')))
32 34

  
35
if 'mandayejs.sites.archimed' in settings.INSTALLED_APPS:
36
    urlpatterns += patterns('',
37
        url(r'^_mandaye/archimed/', include('mandayejs.sites.archimed.urls')),
38
    )
33
-