1 from unfold.loginrequired import LoginRequiredAutoLogoutView
3 from manifold.core.query import Query
4 from manifold.manifoldapi import execute_query
5 from portal.actions import manifold_update_user, manifold_update_account
7 from myslice.viewutils import topmenu_items, the_user
9 from django.http import HttpResponse, HttpResponseRedirect
10 from django.contrib import messages
11 from django.contrib.auth.decorators import login_required
16 class AccountView(LoginRequiredAutoLogoutView):
17 template_name = "account-view.html"
19 def dispatch(self, *args, **kwargs):
20 return super(AccountView, self).dispatch(*args, **kwargs)
23 def get_context_data(self, **kwargs):
25 user_query = Query().get('local:user').select('config','email')
26 user_details = execute_query(self.request, user_query)
28 # not always found in user_details...
30 for user_detail in user_details:
31 #email = user_detail['email']
32 if user_detail['config']:
33 config = json.loads(user_detail['config'])
35 platform_query = Query().get('local:platform').select('platform_id','platform')
36 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
37 platform_details = execute_query(self.request, platform_query)
38 account_details = execute_query(self.request, account_query)
40 # initial assignment needed for users having no account
45 platform_name_list = []
46 account_type_list = []
49 for account_detail in account_details:
50 for platform_detail in platform_details:
51 if platform_detail['platform_id'] == account_detail['platform_id']:
52 platform_name = platform_detail['platform']
53 account_type = account_detail['auth_type']
54 account_config = json.loads(account_detail['config'])
56 account_usr_hrn = account_config.get('user_hrn','N/A')
57 account_pub_key = account_config.get('user_public_key','N/A')
59 platform_name_list.append(platform_name)
60 account_type_list.append(account_type)
61 usr_hrn_list.append(account_usr_hrn)
62 pub_key_list.append(account_pub_key)
64 # combining 4 lists into 1 [to render in the template]
65 lst = [{'platform_name': t[0], 'account_type': t[1], 'usr_hrn':t[2], 'usr_pubkey':t[3]}
66 for t in zip(platform_name_list, account_type_list, usr_hrn_list, pub_key_list)]
70 context = super(AccountView, self).get_context_data(**kwargs)
72 context['person'] = self.request.user
73 context ['firstname'] = config.get('firstname',"?")
74 context ['lastname'] = config.get('lastname',"?")
75 context ['fullname'] = context['firstname'] +' '+ context['lastname']
76 context ['authority'] = config.get('authority',"Unknown Authority")
77 #context['users'] = userlist.render(self.request)
79 # XXX This is repeated in all pages
80 # more general variables expected in the template
81 context['title'] = 'Platforms connected to MySlice'
82 # the menu items on the top
83 context['topmenu_items'] = topmenu_items('My Account', self.request)
84 # so we can sho who is logged
85 context['username'] = the_user(self.request)
86 # context ['firstname'] = config['firstname']
87 #context.update(page.prelude_env())
92 #my_acc form value processing
93 def account_process(request):
94 user_query = Query().get('local:user').select('password','config')
95 user_details = execute_query(request, user_query)
97 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
98 account_details = execute_query(request, account_query)
100 platform_query = Query().get('local:platform').select('platform_id','platform')
101 platform_details = execute_query(request, platform_query)
103 # for account_detail in account_details:
104 # if account_detail['platform_id'] == 5:
105 # account_config = json.loads(account_detail['config'])
107 if 'submit_name' in request.POST:
108 edited_first_name = request.POST['fname']
109 edited_last_name = request.POST['lname']
112 for user_config in user_details:
113 #email = user_detail['email']
114 if user_config['config']:
115 config = json.loads(user_config['config'])
116 config['firstname'] = edited_first_name
117 config['lastname'] = edited_last_name
118 config['authority'] = config.get('authority','Unknown Authority')
119 updated_config = json.dumps(config)
120 user_params = {'config': updated_config}
121 else: # it's needed if the config is empty
122 user_config['config']= '{"firstname":"' + edited_first_name + '", "lastname":"'+ edited_last_name + '", "authority": "Unknown Authority"}'
123 user_params = {'config': user_config['config']}
124 # updating config local:user in manifold
125 #user_params = { 'config': updated_config}
126 manifold_update_user(request,user_params)
127 # this will be depricated, we will show the success msg in same page
128 # return HttpResponse('Sucess: First Name and Last Name Updated!')
129 # Redirect to same page with success message
130 messages.success(request, 'Sucess: First Name and Last Name Updated.')
131 return HttpResponseRedirect("/portal/account/")
133 elif 'submit_pass' in request.POST:
134 edited_password = request.POST['password']
136 for user_pass in user_details:
137 user_pass['password'] = edited_password
138 #updating password in local:user
139 user_params = { 'password': user_pass['password']}
140 manifold_update_user(request,user_params)
141 # return HttpResponse('Success: Password Changed!!')
142 messages.success(request, 'Sucess: Password Updated.')
143 return HttpResponseRedirect("/portal/account/")
145 elif 'generate' in request.POST:
146 for account_detail in account_details:
147 for platform_detail in platform_details:
148 if platform_detail['platform_id'] == account_detail['platform_id']:
149 if 'myslice' in platform_detail['platform']:
150 # Generate public and private keys using SFA Library
151 from sfa.trust.certificate import Keypair
152 k = Keypair(create=True)
153 public_key = k.get_pubkey_string()
154 private_key = k.as_pem()
155 private_key = ''.join(private_key.split())
156 public_key = "ssh-rsa " + public_key
157 keypair = '{"user_public_key":"'+ public_key + '", "user_private_key":"'+ private_key + '"}'
158 # keypair = re.sub("\r", "", keypair)
159 # keypair = re.sub("\n", "\\n", keypair)
160 # #keypair = keypair.rstrip('\r\n')
161 # keypair = ''.join(keypair.split())
162 # updating maniolf local:account table
163 user_params = { 'config': keypair, 'auth_type':'managed'}
164 manifold_update_account(request,user_params)
165 messages.success(request, 'Sucess: New Keypair Generated!')
166 return HttpResponseRedirect("/portal/account/")
168 messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
169 return HttpResponseRedirect("/portal/account/")
171 elif 'upload_key' in request.POST:
172 for account_detail in account_details:
173 for platform_detail in platform_details:
174 if platform_detail['platform_id'] == account_detail['platform_id']:
175 if 'myslice' in platform_detail['platform']:
176 up_file = request.FILES['pubkey']
177 file_content = up_file.read()
178 file_name = up_file.name
179 file_extension = os.path.splitext(file_name)[1]
180 allowed_extension = ['.pub','.txt']
181 if file_extension in allowed_extension and re.search(r'ssh-rsa',file_content):
182 file_content = '{"user_public_key":"'+ file_content +'"}'
183 #file_content = re.sub("\r", "", file_content)
184 #file_content = re.sub("\n", "\\n",file_content)
185 file_content = ''.join(file_content.split())
186 #update manifold local:account table
187 user_params = { 'config': file_content, 'auth_type':'user'}
188 manifold_update_account(request,user_params)
189 messages.success(request, 'Publickey uploaded! Please delegate your credentials using SFA: http://trac.myslice.info/wiki/DelegatingCredentials')
190 return HttpResponseRedirect("/portal/account/")
192 messages.error(request, 'RSA key error: Please upload a valid RSA public key [.txt or .pub].')
193 return HttpResponseRedirect("/portal/account/")
195 messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
196 return HttpResponseRedirect("/portal/account/")
198 elif 'dl_pubkey' in request.POST:
199 for account_detail in account_details:
200 for platform_detail in platform_details:
201 if platform_detail['platform_id'] == account_detail['platform_id']:
202 if 'myslice' in platform_detail['platform']:
203 account_config = json.loads(account_detail['config'])
204 public_key = account_config['user_public_key']
205 response = HttpResponse(public_key, content_type='text/plain')
206 response['Content-Disposition'] = 'attachment; filename="pubkey.txt"'
210 messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
211 return HttpResponseRedirect("/portal/account/")
213 elif 'dl_pkey' in request.POST:
214 for account_detail in account_details:
215 for platform_detail in platform_details:
216 if platform_detail['platform_id'] == account_detail['platform_id']:
217 if 'myslice' in platform_detail['platform']:
218 account_config = json.loads(account_detail['config'])
219 if 'user_private_key' in account_config:
220 private_key = account_config['user_private_key']
221 response = HttpResponse(private_key, content_type='text/plain')
222 response['Content-Disposition'] = 'attachment; filename="privkey.txt"'
225 messages.error(request, 'Download error: Private key is not stored in the server')
226 return HttpResponseRedirect("/portal/account/")
229 messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
230 return HttpResponseRedirect("/portal/account/")
232 elif 'delete' in request.POST:
233 for account_detail in account_details:
234 for platform_detail in platform_details:
235 if platform_detail['platform_id'] == account_detail['platform_id']:
236 if 'myslice' in platform_detail['platform']:
237 account_config = json.loads(account_detail['config'])
238 if 'user_private_key' in account_config:
241 messages.error(request, 'Delete error: Private key is not stored in the server')
242 return HttpResponseRedirect("/portal/account/")
245 messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
246 return HttpResponseRedirect("/portal/account/")
250 messages.info(request, 'Under Construction. Please try again later!')
251 return HttpResponseRedirect("/portal/account/")