4 # this somehow is not used anymore - should it not be ?
5 from django.core.context_processors import csrf
6 from django.http import HttpResponseRedirect
7 from django.contrib.auth import authenticate, login, logout
8 from django.template import RequestContext
9 from django.shortcuts import render_to_response
10 from django.shortcuts import render
12 from unfold.loginrequired import FreeAccessView
13 from ui.topmenu import topmenu_items, the_user
15 from manifold.core.query import Query
16 from manifoldapi.manifoldapi import execute_query
17 from manifoldapi.manifoldresult import ManifoldResult
19 from myslice.configengine import ConfigEngine
20 from myslice.theme import ThemeView
22 from portal.account import Account, get_expiration
23 from portal.models import PendingSlice
24 from portal.actions import authority_check_pis, get_jfed_identity, get_myslice_account
26 from myslice.settings import logger
30 class HomeView (FreeAccessView, ThemeView):
31 template_name = 'home-view.html'
33 # expose this so we can mention the backend URL on the welcome page
34 def default_env (self):
36 'MANIFOLD_URL':ConfigEngine().manifold_url(),
39 def post (self,request):
40 env = self.default_env()
41 env['theme'] = self.theme
42 env['section'] = "Dashboard"
44 username = request.POST.get('username')
45 password = request.POST.get('password')
47 # pass request within the token, so manifold session key can be attached to the request session.
48 token = {'username': username, 'password': password, 'request': request}
50 # our authenticate function returns either
51 # . a ManifoldResult - when something has gone wrong, like e.g. backend is unreachable
52 # . a django User in case of success
53 # . or None if the backend could be reached but the authentication failed
54 auth_result = authenticate(token=token)
55 # use one or two columns for the layout - not logged in users will see the login prompt
56 # high-level errors, like connection refused or the like
57 if isinstance (auth_result, ManifoldResult):
58 manifoldresult = auth_result
59 # let's use ManifoldResult.__repr__
60 env['state']="%s"%manifoldresult
62 # user was authenticated at the backend
63 elif auth_result is not None:
65 if user is not None and user.is_active:
68 if request.user.is_authenticated():
70 env['person'] = self.request.user
71 env['username'] = self.request.user
74 activity.user.login(self.request)
76 ## check user is pi or not
80 account_detail = get_myslice_account(self.request)
81 if 'config' in account_detail and account_detail['config'] is not '':
82 account_config = json.loads(account_detail['config'])
83 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
84 acc_user_cred = account_config.get('delegated_user_credential','N/A')
86 #if acc_auth_cred=={} or acc_auth_cred=='N/A':
90 user_email = str(self.request.user)
91 #pi = authority_check_pis(self.request, user_email)
93 # check if the user has creds or not
94 if acc_user_cred == {} or acc_user_cred == 'N/A':
95 user_cred = 'no_creds'
97 exp_date = get_expiration(acc_user_cred, 'timestamp')
98 if exp_date < time.time():
99 user_cred = 'creds_expired'
101 user_cred = 'has_creds'
103 # list the pending slices of this user
105 for slices in PendingSlice.objects.filter(type_of_nodes__iexact=self.request.user).all():
106 pending_slices.append(slices.slice_name)
108 env['pending_slices'] = pending_slices
110 env['user_cred'] = user_cred
111 except Exception as e:
114 env['state'] = "Your session has expired"
119 activity.user.login(self.request, "notactive")
120 env['state'] = "Your account is not active, please contact the site admin."
121 env['layout_1_or_2']="layout-unfold2.html"
123 jfed_identity = get_jfed_identity(request)
124 if jfed_identity is not None:
126 encoded_jfed_identity = base64.b64encode(jfed_identity)
127 env['jfed_identity'] = encoded_jfed_identity
129 env['jfed_identity'] = None
134 activity.user.login(self.request, "error")
135 env['state'] = "Your username and/or password were incorrect."
137 env['next'] = request.POST.get('next',None)
138 env['request'] = request
140 return HttpResponseRedirect(env['next'])
141 return render_to_response(self.template,env, context_instance=RequestContext(request))
143 def get (self, request, state=None):
144 env = self.default_env()
148 if request.user.is_authenticated():
149 jfed_identity = get_jfed_identity(request)
150 if jfed_identity is not None:
152 encoded_jfed_identity = base64.b64encode(jfed_identity)
153 env['jfed_identity'] = encoded_jfed_identity
155 env['jfed_identity'] = None
157 ## check user is pi or not
158 platform_details = {}
162 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
163 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
164 # XXX Something like an invalid session seems to make the execute fail sometimes, and thus gives an error on the main page
166 account_detail = get_myslice_account(self.request)
167 if 'config' in account_detail and account_detail['config'] is not '':
168 account_config = json.loads(account_detail['config'])
169 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
170 acc_user_cred = account_config.get('delegated_user_credential','N/A')
172 #if acc_auth_cred=={} or acc_auth_cred=='N/A':
176 user_email = str(self.request.user)
177 #pi = authority_check_pis(self.request, user_email)
178 # check if the user has creds or not
179 if acc_user_cred == {} or acc_user_cred == 'N/A':
180 user_cred = 'no_creds'
182 exp_date = get_expiration(acc_user_cred, 'timestamp')
183 if exp_date < time.time():
184 user_cred = 'creds_expired'
186 user_cred = 'has_creds'
188 # list the pending slices of this user
190 for slices in PendingSlice.objects.filter(type_of_nodes__iexact=self.request.user).all():
191 pending_slices.append(slices.slice_name)
193 env['pending_slices'] = pending_slices
195 env['user_cred'] = user_cred
196 env['person'] = self.request.user
199 except Exception as e:
202 env['state'] = "Your session has expired"
204 env['theme'] = self.theme
205 env['section'] = "Dashboard"
206 logger.debug("homeview called")
207 env['next'] = request.GET.get('next',None)
208 env['username']=the_user(request)
209 env['topmenu_items'] = topmenu_items(None, request)
210 env['request'] = request
211 if state: env['state'] = state
212 elif not env['username']: env['state'] = None
213 # use one or two columns for the layout - not logged in users will see the login prompt
214 return render_to_response(self.template, env, context_instance=RequestContext(request))