Merge branch 'onelab' of ssh://git.onelab.eu/git/myslice into onelab
[myslice.git] / portal / managementtabrequests.py
1 from django.template                 import RequestContext
2 from django.shortcuts                import render_to_response
3
4 from manifold.core.query             import Query, AnalyzedQuery
5 from manifoldapi.manifoldapi         import execute_query
6
7 from django.views.generic.base      import TemplateView
8
9 from unfold.loginrequired           import LoginRequiredView
10 from django.http                    import HttpResponse
11 from django.shortcuts               import render
12
13 from manifold.core.query            import Query, AnalyzedQuery
14 from manifoldapi.manifoldapi        import execute_query
15
16 from portal.actions                 import get_requests
17
18 from myslice.theme import ThemeView
19
20 import json
21
22 class ManagementRequestsView (LoginRequiredView, ThemeView):
23     template_name = "management-tab-requests.html"
24     
25     def get_context_data(self, **kwargs):
26        
27         ctx_my_authorities = {}
28         ctx_delegation_authorities = {}
29         ctx_sub_authorities = {}
30         dest = {}
31
32
33         # The user need to be logged in
34         if (self.request.user):
35            
36             user_query = Query().get('local:user').filter_by('email', '==', self.request.user.email).select('user_id')
37             user, = execute_query(self.request, user_query)
38             user_id = user['user_id']
39
40             # Query manifold to learn about available SFA platforms for more information
41             # In general we will at least have the portal
42             # For now we are considering all registries
43             all_authorities = []
44             platform_ids = []
45             sfa_platforms_query = Query().get('local:platform').filter_by('gateway_type', '==', 'sfa').select('platform_id', 'platform', 'auth_type')
46             sfa_platforms = execute_query(self.request, sfa_platforms_query)
47             for sfa_platform in sfa_platforms:
48                 print "SFA PLATFORM > ", sfa_platform['platform']
49                 if not 'auth_type' in sfa_platform:
50                     continue
51                 auth = sfa_platform['auth_type']
52                 if not auth in all_authorities:
53                     all_authorities.append(auth)
54                 platform_ids.append(sfa_platform['platform_id'])
55
56             print "W: Hardcoding platform myslice"
57             # There has been a tweak on how new platforms are referencing a
58             # so-called 'myslice' platform for storing authentication tokens.
59             # XXX This has to be removed in final versions.
60             myslice_platforms_query = Query().get('local:platform').filter_by('platform', '==', 'myslice').select('platform_id')
61             myslice_platforms = execute_query(self.request, myslice_platforms_query)
62             if myslice_platforms:
63                 myslice_platform, = myslice_platforms
64                 platform_ids.append(myslice_platform['platform_id'])
65
66             # We can check on which the user has authoritity credentials = PI rights
67             credential_authorities = set()
68             credential_authorities_expired = set()
69
70             # User account on these registries
71             user_accounts_query = Query.get('local:account').filter_by('user_id', '==', user_id).filter_by('platform_id', 'included', platform_ids).select('auth_type', 'config')
72             user_accounts = execute_query(self.request, user_accounts_query)
73             
74             for user_account in user_accounts:
75
76                 print "USER ACCOUNT", user_account
77                 if user_account['auth_type'] == 'reference':
78                     continue # we hardcoded the myslice platform...
79
80                 config = json.loads(user_account['config'])
81                 creds = []
82                 print "CONFIG KEYS", config.keys()
83                 if 'authority_credentials' in config:
84                     print "***", config['authority_credentials'].keys()
85                     for authority_hrn, credential in config['authority_credentials'].items():
86                         credential_authorities.add(authority_hrn)
87                 if 'delegated_authority_credentials' in config:
88                     print "***", config['delegated_authority_credentials'].keys()
89                     for authority_hrn, credential in config['delegated_authority_credentials'].items():
90                         credential_authorities.add(authority_hrn)
91
92             print 'credential_authorities =', credential_authorities
93             print 'credential_authorities_expired =', credential_authorities_expired
94
95             # ** Where am I a PI **
96             # For this we need to ask SFA (of all authorities) = PI function
97             pi_authorities_query = Query.get('user').filter_by('user_hrn', '==', '$user_hrn').select('pi_authorities')
98             pi_authorities_tmp = execute_query(self.request, pi_authorities_query)
99             pi_authorities = set()
100             try:
101                 for pa in pi_authorities_tmp:
102                     pi_authorities |= set(pa['pi_authorities'])
103             except:
104                 print 'No pi_authorities'
105
106             pi_credential_authorities = pi_authorities & credential_authorities
107             pi_no_credential_authorities = pi_authorities - credential_authorities - credential_authorities_expired
108             pi_expired_credential_authorities = pi_authorities & credential_authorities_expired
109             # Authorities I've been delegated PI rights
110             pi_delegation_credential_authorities = credential_authorities - pi_authorities
111             pi_delegation_expired_authorities = credential_authorities_expired - pi_authorities
112
113             # Summary intermediary
114             pi_my_authorities = pi_credential_authorities | pi_no_credential_authorities | pi_expired_credential_authorities
115             pi_delegation_authorities = pi_delegation_credential_authorities | pi_delegation_expired_authorities
116
117             # Summary all
118             queried_pending_authorities = pi_my_authorities | pi_delegation_authorities #| pi_subauthorities
119
120             # iterate on the requests and check if the authority matches a prefix 
121             # startswith an authority on which the user is PI
122             requests = get_requests()
123             for r in requests:
124                 auth_hrn = r['authority_hrn']
125                 for my_auth in pi_my_authorities: 
126                     if auth_hrn.startswith(my_auth):
127                         dest = ctx_my_authorities
128                         r['allowed'] = 'allowed'
129                 for my_auth in pi_delegation_authorities:
130                     if auth_hrn.startswith(my_auth):
131                         dest = ctx_delegation_authorities
132                         r['allowed'] = 'allowed'
133                 if auth_hrn in pi_expired_credential_authorities:
134                     r['allowed'] = 'expired'
135                 if 'allowed' not in r:
136                     ## TEMP FIX for allowing new authority registration
137                     #r['allowed'] = 'denied'
138                     r['allowed'] = 'allowed'
139
140                 if not auth_hrn in dest:
141                     dest[auth_hrn] = []
142                 dest[auth_hrn].append(r)
143                 
144               
145 #         env = {}
146 #         env['my_authorities']   = ctx_my_authorities
147 #         env['sub_authorities']   = ctx_sub_authorities
148 #         env['delegation_authorities'] = ctx_delegation_authorities
149
150 #         # XXX This is repeated in all pages
151 #         # more general variables expected in the template
152 #         # the menu items on the top
153 #         #env['topmenu_items'] = topmenu_items_live('Validation', page) 
154 #         # so we can sho who is logged
155 #         env['username'] = request.user
156 #         env['pi'] = "is_pi"       
157 #         env['theme'] = self.theme
158 #         env['section'] = "Requests"
159         
160         context = super(ManagementRequestsView, self).get_context_data(**kwargs)
161         
162             
163         context['my_authorities']   = ctx_my_authorities
164         context['sub_authorities']   = ctx_sub_authorities
165         context['delegation_authorities'] = ctx_delegation_authorities
166
167         # XXX This is repeated in all pages
168         # more general variables expected in the template
169         context['title'] = 'Test view that combines various plugins'
170         # the menu items on the top
171         #context['topmenu_items'] = topmenu_items_live('Validation', page) 
172         # so we can sho who is logged
173         context['username'] = self.request.user 
174         context['pi'] = "is_pi"       
175         context['theme'] = self.theme
176         context['section'] = "Requests"
177         # XXX We need to prepare the page for queries
178         #context.update(page.prelude_env())
179
180         return context
181     
182         #return render_to_response(self.template, env, context_instance=RequestContext(request))