de790341327d81bf67eb3c1e91f0111d966fa4f0
[myslice.git] / portal / validationview.py
1 # -*- coding: utf-8 -*-
2 #
3 # portal/views.py: views for the portal application
4 # This file is part of the Manifold project.
5 #
6 # Authors:
7 #   Jordan AugĂ© <jordan.auge@lip6.fr>
8 #   Mohammed Yasin Rahman <mohammed-yasin.rahman@lip6.fr>
9 #   Loic Baron <loic.baron@lip6.fr>
10 # Copyright 2013, UPMC Sorbonne UniversitĂ©s / LIP6
11 #
12 # This program is free software; you can redistribute it and/or modify it under
13 # the terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 3, or (at your option) any later version.
15
16 # This program is distributed in the hope that it will be useful, but WITHOUT
17 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19 # details.
20
21 # You should have received a copy of the GNU General Public License along with
22 # this program; see the file COPYING.  If not, write to the Free Software
23 # Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 import json
26
27 from django.http                import HttpResponseRedirect, HttpResponse
28 from django.shortcuts           import render
29 from django.template.loader     import render_to_string
30
31 from unfold.loginrequired       import FreeAccessView
32 from ui.topmenu                 import topmenu_items_live, the_user
33
34 from portal.event               import Event
35 # presview is put in observation for now
36 #from plugins.pres_view          import PresView
37 from plugins.raw                import Raw
38
39 # these seem totally unused for now
40 #from portal.util                import RegistrationView, ActivationView
41
42 from portal.models              import PendingUser, PendingSlice
43 from portal.actions             import get_request_by_authority
44 from manifold.manifoldapi       import execute_query
45 from manifold.core.query        import Query
46 from unfold.page                import Page
47
48 class ValidatePendingView(FreeAccessView):
49     template_name = "validate_pending.html"
50
51     def get_context_data(self, **kwargs):
52         # We might have slices on different registries with different user accounts 
53         # We note that this portal could be specific to a given registry, to which we register users, but i'm not sure that simplifies things
54         # Different registries mean different identities, unless we identify via SFA HRN or have associated the user email to a single hrn
55
56         #messages.info(self.request, 'You have logged in')
57         page = Page(self.request)
58
59         ctx_my_authorities = {}
60         ctx_delegation_authorities = {}
61
62
63         # The user need to be logged in
64         if the_user(self.request):
65             # Who can a PI validate:
66             # His own authorities + those he has credentials for.
67             # In MySlice we need to look at credentials also.
68             
69
70             # XXX This will have to be asynchroneous. Need to implement barriers,
71             # for now it will be sufficient to have it working statically
72
73             # get user_id to later on query accounts
74             # XXX Having real query plan on local tables would simplify all this
75             # XXX $user_email is still not available for local tables
76             #user_query = Query().get('local:user').filter_by('email', '==', '$user_email').select('user_id')
77             user_query = Query().get('local:user').filter_by('email', '==', the_user(self.request)).select('user_id')
78             user, = execute_query(self.request, user_query)
79             user_id = user['user_id']
80
81             # Query manifold to learn about available SFA platforms for more information
82             # In general we will at least have the portal
83             # For now we are considering all registries
84             all_authorities = []
85             platform_ids = []
86             sfa_platforms_query = Query().get('local:platform').filter_by('gateway_type', '==', 'sfa').select('platform_id', 'platform', 'auth_type')
87             sfa_platforms = execute_query(self.request, sfa_platforms_query)
88             for sfa_platform in sfa_platforms:
89                 print "SFA PLATFORM > ", sfa_platform['platform']
90                 if not 'auth_type' in sfa_platform:
91                     continue
92                 auth = sfa_platform['auth_type']
93                 if not auth in all_authorities:
94                     all_authorities.append(auth)
95                 platform_ids.append(sfa_platform['platform_id'])
96
97             print "W: Hardcoding platform myslice"
98             # There has been a tweak on how new platforms are referencing a
99             # so-called 'myslice' platform for storing authentication tokens.
100             # XXX This has to be removed in final versions.
101             myslice_platforms_query = Query().get('local:platform').filter_by('platform', '==', 'myslice').select('platform_id')
102             myslice_platforms = execute_query(self.request, myslice_platforms_query)
103             if myslice_platforms:
104                 myslice_platform, = myslice_platforms
105                 platform_ids.append(myslice_platform['platform_id'])
106
107             # We can check on which the user has authoritity credentials = PI rights
108             credential_authorities = set()
109             credential_authorities_expired = set()
110
111             # User account on these registries
112             user_accounts_query = Query.get('local:account').filter_by('user_id', '==', user_id).filter_by('platform_id', 'included', platform_ids).select('auth_type', 'config')
113             user_accounts = execute_query(self.request, user_accounts_query)
114             #print "=" * 80
115             #print user_accounts
116             #print "=" * 80
117             for user_account in user_accounts:
118
119                 print "USER ACCOUNT", user_account
120                 if user_account['auth_type'] == 'reference':
121                     continue # we hardcoded the myslice platform...
122
123                 config = json.loads(user_account['config'])
124                 creds = []
125                 print "CONFIG KEYS", config.keys()
126                 if 'authority_credentials' in config:
127                     print "***", config['authority_credentials'].keys()
128                     for authority_hrn, credential in config['authority_credentials'].items():
129                         #if credential is not expired:
130                         credential_authorities.add(authority_hrn)
131                         #else
132                         #    credential_authorities_expired.add(authority_hrn)
133                 if 'delegated_authority_credentials' in config:
134                     print "***", config['delegated_authority_credentials'].keys()
135                     for authority_hrn, credential in config['delegated_authority_credentials'].items():
136                         #if credential is not expired:
137                         credential_authorities.add(authority_hrn)
138                         #else
139                         #    credential_authorities_expired.add(authority_hrn)
140
141             print 'credential_authorities =', credential_authorities
142             print 'credential_authorities_expired =', credential_authorities_expired
143
144             # ** Where am I a PI **
145             # For this we need to ask SFA (of all authorities) = PI function
146             pi_authorities_query = Query.get('user').filter_by('user_hrn', '==', '$user_hrn').select('pi_authorities')
147             pi_authorities_tmp = execute_query(self.request, pi_authorities_query)
148             pi_authorities = set()
149             for pa in pi_authorities_tmp:
150                 pi_authorities |= set(pa['pi_authorities'])
151
152             print "pi_authorities =", pi_authorities
153             
154             # My authorities + I have a credential
155             pi_credential_authorities = pi_authorities & credential_authorities
156             pi_no_credential_authorities = pi_authorities - credential_authorities - credential_authorities_expired
157             pi_expired_credential_authorities = pi_authorities & credential_authorities_expired
158             # Authorities I've been delegated PI rights
159             pi_delegation_credential_authorities = credential_authorities - pi_authorities
160             pi_delegation_expired_authorities = credential_authorities_expired - pi_authorities
161
162             print "pi_credential_authorities =", pi_credential_authorities
163             print "pi_no_credential_authorities =", pi_no_credential_authorities
164             print "pi_expired_credential_authorities =", pi_expired_credential_authorities
165             print "pi_delegation_credential_authorities = ", pi_delegation_credential_authorities
166             print "pi_delegation_expired_authorities = ", pi_delegation_expired_authorities
167
168             # Summary intermediary
169             pi_my_authorities = pi_credential_authorities | pi_no_credential_authorities | pi_expired_credential_authorities
170             pi_delegation_authorities = pi_delegation_credential_authorities | pi_delegation_expired_authorities
171
172             print "--"
173             print "pi_my_authorities = ", pi_my_authorities
174             print "pi_delegation_authorities = ", pi_delegation_authorities
175
176             # Summary all
177             queried_pending_authorities = pi_my_authorities | pi_delegation_authorities
178             print "----"
179             print "queried_pending_authorities = ", queried_pending_authorities
180
181             requests = get_request_by_authority(queried_pending_authorities)
182             for request in requests:
183                 auth_hrn = request['authority_hrn']
184                 print "authority for this request", auth_hrn
185
186                 if auth_hrn in pi_my_authorities:
187                     dest = ctx_my_authorities
188
189                     # define the css class
190                     if auth_hrn in pi_credential_authorities:
191                         request['allowed'] = 'allowed'
192                     elif auth_hrn in pi_expired_credential_authorities:
193                         request['allowed'] = 'expired'
194                     else: # pi_no_credential_authorities
195                         request['allowed'] = 'denied'
196
197                 elif auth_hrn in pi_delegation_authorities:
198                     dest = ctx_delegation_authorities
199
200                     if auth_hrn in pi_delegation_credential_authorities:
201                         request['allowed'] = 'allowed'
202                     else: # pi_delegation_expired_authorities
203                         request['allowed'] = 'expired'
204
205                 else:
206                     continue
207
208                 if not auth_hrn in dest:
209                     dest[auth_hrn] = []
210                 dest[auth_hrn].append(request) 
211         
212         context = super(ValidatePendingView, self).get_context_data(**kwargs)
213         context['my_authorities']   = ctx_my_authorities
214         context['delegation_authorities'] = ctx_delegation_authorities
215
216         # XXX This is repeated in all pages
217         # more general variables expected in the template
218         context['title'] = 'Test view that combines various plugins'
219         # the menu items on the top
220         context['topmenu_items'] = topmenu_items_live('Validation', page) 
221         # so we can sho who is logged
222         context['username'] = the_user(self.request) 
223
224         # XXX We need to prepare the page for queries
225         #context.update(page.prelude_env())
226
227         return context