added theme
[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_requests
44 from manifold.manifoldapi       import execute_query
45 from manifold.core.query        import Query
46 from unfold.page                import Page
47 from theme import ThemeView
48
49 class ValidatePendingView(FreeAccessView, ThemeView):
50     template_name = "validate_pending.html"
51
52     def get_context_data(self, **kwargs):
53         # We might have slices on different registries with different user accounts 
54         # 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
55         # Different registries mean different identities, unless we identify via SFA HRN or have associated the user email to a single hrn
56
57         #messages.info(self.request, 'You have logged in')
58         page = Page(self.request)
59
60         ctx_my_authorities = {}
61         ctx_delegation_authorities = {}
62         ctx_sub_authorities = {}
63
64
65         # The user need to be logged in
66         if the_user(self.request):
67             # Who can a PI validate:
68             # His own authorities + those he has credentials for.
69             # In MySlice we need to look at credentials also.
70             
71
72             # XXX This will have to be asynchroneous. Need to implement barriers,
73             # for now it will be sufficient to have it working statically
74
75             # get user_id to later on query accounts
76             # XXX Having real query plan on local tables would simplify all this
77             # XXX $user_email is still not available for local tables
78             #user_query = Query().get('local:user').filter_by('email', '==', '$user_email').select('user_id')
79             user_query = Query().get('local:user').filter_by('email', '==', the_user(self.request)).select('user_id')
80             user, = execute_query(self.request, user_query)
81             user_id = user['user_id']
82
83             # Query manifold to learn about available SFA platforms for more information
84             # In general we will at least have the portal
85             # For now we are considering all registries
86             all_authorities = []
87             platform_ids = []
88             sfa_platforms_query = Query().get('local:platform').filter_by('gateway_type', '==', 'sfa').select('platform_id', 'platform', 'auth_type')
89             sfa_platforms = execute_query(self.request, sfa_platforms_query)
90             for sfa_platform in sfa_platforms:
91                 print "SFA PLATFORM > ", sfa_platform['platform']
92                 if not 'auth_type' in sfa_platform:
93                     continue
94                 auth = sfa_platform['auth_type']
95                 if not auth in all_authorities:
96                     all_authorities.append(auth)
97                 platform_ids.append(sfa_platform['platform_id'])
98
99             print "W: Hardcoding platform myslice"
100             # There has been a tweak on how new platforms are referencing a
101             # so-called 'myslice' platform for storing authentication tokens.
102             # XXX This has to be removed in final versions.
103             myslice_platforms_query = Query().get('local:platform').filter_by('platform', '==', 'myslice').select('platform_id')
104             myslice_platforms = execute_query(self.request, myslice_platforms_query)
105             if myslice_platforms:
106                 myslice_platform, = myslice_platforms
107                 platform_ids.append(myslice_platform['platform_id'])
108
109             # We can check on which the user has authoritity credentials = PI rights
110             credential_authorities = set()
111             credential_authorities_expired = set()
112
113             # User account on these registries
114             user_accounts_query = Query.get('local:account').filter_by('user_id', '==', user_id).filter_by('platform_id', 'included', platform_ids).select('auth_type', 'config')
115             user_accounts = execute_query(self.request, user_accounts_query)
116             #print "=" * 80
117             #print user_accounts
118             #print "=" * 80
119             for user_account in user_accounts:
120
121                 print "USER ACCOUNT", user_account
122                 if user_account['auth_type'] == 'reference':
123                     continue # we hardcoded the myslice platform...
124
125                 config = json.loads(user_account['config'])
126                 creds = []
127                 print "CONFIG KEYS", config.keys()
128                 if 'authority_credentials' in config:
129                     print "***", config['authority_credentials'].keys()
130                     for authority_hrn, credential in config['authority_credentials'].items():
131                         #if credential is not expired:
132                         credential_authorities.add(authority_hrn)
133                         #else
134                         #    credential_authorities_expired.add(authority_hrn)
135                 if 'delegated_authority_credentials' in config:
136                     print "***", config['delegated_authority_credentials'].keys()
137                     for authority_hrn, credential in config['delegated_authority_credentials'].items():
138                         #if credential is not expired:
139                         credential_authorities.add(authority_hrn)
140                         #else
141                         #    credential_authorities_expired.add(authority_hrn)
142
143             print 'credential_authorities =', credential_authorities
144             print 'credential_authorities_expired =', credential_authorities_expired
145
146 #            # Using cache manifold-tables to get the list of authorities faster
147 #            all_authorities_query = Query.get('authority').select('name', 'authority_hrn')
148 #            all_authorities = execute_query(self.request, all_authorities_query)
149
150             # ** Where am I a PI **
151             # For this we need to ask SFA (of all authorities) = PI function
152             pi_authorities_query = Query.get('user').filter_by('user_hrn', '==', '$user_hrn').select('pi_authorities')
153             pi_authorities_tmp = execute_query(self.request, pi_authorities_query)
154             pi_authorities = set()
155             for pa in pi_authorities_tmp:
156                 pi_authorities |= set(pa['pi_authorities'])
157
158 #            # include all sub-authorities of the PI
159 #            # if PI on ple, include all sub-auths ple.upmc, ple.inria and so on...
160 #            pi_subauthorities = set()
161 #            for authority in all_authorities:
162 #                authority_hrn = authority['authority_hrn']
163 #                for my_authority in pi_authorities:
164 #                    if authority_hrn.startswith(my_authority) and authority_hrn not in pi_subauthorities:
165 #                        pi_subauthorities.add(authority_hrn)
166
167             #print "pi_authorities =", pi_authorities
168             #print "pi_subauthorities =", pi_subauthorities
169             
170             # My authorities + I have a credential
171             pi_credential_authorities = pi_authorities & credential_authorities
172             pi_no_credential_authorities = pi_authorities - credential_authorities - credential_authorities_expired
173             pi_expired_credential_authorities = pi_authorities & credential_authorities_expired
174             # Authorities I've been delegated PI rights
175             pi_delegation_credential_authorities = credential_authorities - pi_authorities
176             pi_delegation_expired_authorities = credential_authorities_expired - pi_authorities
177
178             #print "pi_credential_authorities =", pi_credential_authorities
179             #print "pi_no_credential_authorities =", pi_no_credential_authorities
180             #print "pi_expired_credential_authorities =", pi_expired_credential_authorities
181             #print "pi_delegation_credential_authorities = ", pi_delegation_credential_authorities
182             #print "pi_delegation_expired_authorities = ", pi_delegation_expired_authorities
183
184             # Summary intermediary
185             pi_my_authorities = pi_credential_authorities | pi_no_credential_authorities | pi_expired_credential_authorities
186             pi_delegation_authorities = pi_delegation_credential_authorities | pi_delegation_expired_authorities
187
188             #print "--"
189             #print "pi_my_authorities = ", pi_my_authorities
190             #print "pi_delegation_authorities = ", pi_delegation_authorities
191             #print "pi_subauthorities = ", pi_subauthorities
192
193             # Summary all
194             queried_pending_authorities = pi_my_authorities | pi_delegation_authorities #| pi_subauthorities
195             #print "----"
196             #print "queried_pending_authorities = ", queried_pending_authorities
197
198 # iterate on the requests and check if the authority matches a prefix startswith an authority on which the user is PI
199             requests = get_requests()
200 #            requests = get_requests(queried_pending_authorities)
201             for request in requests:
202                 auth_hrn = request['authority_hrn']
203                 for my_auth in pi_my_authorities: 
204                     if auth_hrn.startswith(my_auth):
205                         dest = ctx_my_authorities
206                         request['allowed'] = 'allowed'
207                 for my_auth in pi_delegation_authorities:
208                     if auth_hrn.startswith(my_auth):
209                         dest = ctx_delegation_authorities
210                         request['allowed'] = 'allowed'
211                 if auth_hrn in pi_expired_credential_authorities:
212                     request['allowed'] = 'expired'
213                 if 'allowed' not in request:
214                     request['allowed'] = 'denied'
215                #print "authority for this request", auth_hrn
216
217 #                if auth_hrn in pi_my_authorities:
218 #                    dest = ctx_my_authorities
219 #
220 #                    # define the css class
221 #                    if auth_hrn in pi_credential_authorities:
222 #                        request['allowed'] = 'allowed'
223 #                    elif auth_hrn in pi_expired_credential_authorities:
224 #                        request['allowed'] = 'expired'
225 #                    else: # pi_no_credential_authorities
226 #                        request['allowed'] = 'denied'
227 #
228 #                elif auth_hrn in pi_delegation_authorities:
229 #                    dest = ctx_delegation_authorities
230 #
231 #                    if auth_hrn in pi_delegation_credential_authorities:
232 #                        request['allowed'] = 'allowed'
233 #                    else: # pi_delegation_expired_authorities
234 #                        request['allowed'] = 'expired'
235 #
236 #                elif auth_hrn in pi_subauthorities:
237 #                    dest = ctx_sub_authorities
238 #
239 #                    if auth_hrn in pi_subauthorities:
240 #                        request['allowed'] = 'allowed'
241 #                    else: # pi_delegation_expired_authorities
242 #                        request['allowed'] = 'denied'
243 #
244 #                else:
245 #                    continue
246
247                 if not auth_hrn in dest:
248                     dest[auth_hrn] = []
249                 dest[auth_hrn].append(request) 
250         
251         context = super(ValidatePendingView, self).get_context_data(**kwargs)
252         context['my_authorities']   = ctx_my_authorities
253         context['sub_authorities']   = ctx_sub_authorities
254         context['delegation_authorities'] = ctx_delegation_authorities
255
256         # XXX This is repeated in all pages
257         # more general variables expected in the template
258         context['title'] = 'Test view that combines various plugins'
259         # the menu items on the top
260         context['topmenu_items'] = topmenu_items_live('Validation', page) 
261         # so we can sho who is logged
262         context['username'] = the_user(self.request) 
263         
264         context['theme'] = self.theme
265         
266         # XXX We need to prepare the page for queries
267         #context.update(page.prelude_env())
268
269         return context