Fix: minor
[myslice.git] / portal / manageuserview.py
index 59fc1d6..6e55b84 100644 (file)
@@ -41,8 +41,6 @@ class UserView(LoginRequiredAutoLogoutView):
         for user_detail in user_details:
             user_id = user_detail['user_id']
             user_email = user_detail['email'] 
-            print "hello_world"
-            print user_id          
             # different significations of user_status
             if user_detail['status'] == 0: 
                 user_status = 'Disabled'
@@ -216,19 +214,28 @@ class UserView(LoginRequiredAutoLogoutView):
 
 @login_required
 #my_acc form value processing
-def user_process(request):
-    user_query  = Query().get('local:user').select('user_id','email','password','config')
-    user_details = execute_query(request, user_query)
-    
-    account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
-    account_details = execute_query(request, account_query)
+def user_process(request, **kwargs):
+        
+    for key, value in kwargs.iteritems():
+        if key == "email":
+            selected_email=value
 
-    platform_query  = Query().get('local:platform').select('platform_id','platform')
-    platform_details = execute_query(request, platform_query)
+    redirect_url = "/portal/user/"+selected_email
     
+    user_query  = Query().get('local:user').filter_by('email', '==', selected_email).select('user_id','email','password','config')
+    user_details = execute_admin_query(request, user_query)
+
     # getting the user_id from the session
     for user_detail in user_details:
-            user_id = user_detail['user_id']
+        user_id = user_detail['user_id']
+        user_email = user_detail['email']
+
+    account_query  = Query().get('local:account').filter_by('user_id', '==', user_id).select('user_id','platform_id','auth_type','config')
+    account_details = execute_admin_query(request, account_query)
+
+    platform_query  = Query().get('local:platform').select('platform_id','platform')
+    platform_details = execute_admin_query(request, platform_query)
+    
 
     for account_detail in account_details:
         for platform_detail in platform_details:
@@ -238,15 +245,15 @@ def user_process(request):
                 user_params = {'platform_id': platform_id, 'user_id': user_id, 'auth_type': "reference", 'config': '{"reference_platform": "myslice"}'}
                 manifold_add_account(request,user_params)
                 messages.info(request, 'Reference Account is added to the selected platform successfully!')
-                return HttpResponseRedirect("/portal/account/")
+                return HttpResponseRedirect(redirect_url)
 
             # Delete reference account from the platforms
             if 'delete_'+platform_detail['platform'] in request.POST:
                 platform_id = platform_detail['platform_id']
                 user_params = {'user_id':user_id}
                 manifold_delete_account(request,platform_id,user_params)
-                messages.info(request, 'Reference Account is removed from the selected platform')
-                return HttpResponseRedirect("/portal/account/")
+                messages.info(request, 'Refeence Account is removed from the selected platform')
+                return HttpResponseRedirect(redirect_url)
 
             if platform_detail['platform_id'] == account_detail['platform_id']:
                 if 'myslice' in platform_detail['platform']:
@@ -306,23 +313,33 @@ def user_process(request):
                 user_config['config']= '{"firstname":"' + edited_first_name + '", "lastname":"'+ edited_last_name + '", "authority": "Unknown Authority"}'
                 user_params = {'config': user_config['config']} 
         # updating config local:user in manifold       
-        manifold_update_user(request, request.user.email,user_params)
+        manifold_update_user(request, user_email, user_params)
         # this will be depricated, we will show the success msg in same page
         # Redirect to same page with success message
         messages.success(request, 'Sucess: First Name and Last Name Updated.')
-        return HttpResponseRedirect("/portal/account/")       
+        return HttpResponseRedirect(redirect_url)       
     
-    elif 'submit_pass' in request.POST:
-        edited_password = request.POST['password']
+    elif 'submit_auth' in request.POST:
+        edited_auth = request.POST['authority']
         
-        for user_pass in user_details:
-            user_pass['password'] = edited_password
-        #updating password in local:user
-        user_params = { 'password': user_pass['password']}
-        manifold_update_user(request,request.user.email,user_params)
-#        return HttpResponse('Success: Password Changed!!')
-        messages.success(request, 'Sucess: Password Updated.')
-        return HttpResponseRedirect("/portal/account/")
+        config={}
+        for user_config in user_details:
+            if user_config['config']:
+                config = json.loads(user_config['config'])
+                config['firstname'] = config.get('firstname', 'N/A')
+                config['lastname'] = config.get('lastname','N/A')
+                config['authority'] = edited_auth
+                updated_config = json.dumps(config)
+                user_params = {'config': updated_config}
+            else: # it's needed if the config is empty 
+                user_config['config']= '{"firstname": "N/A", "lastname":"N/A", "authority":"' + edited_auth + '"}'
+                user_params = {'config': user_config['config']}
+        # updating config local:user in manifold       
+        manifold_update_user(request, user_email, user_params)
+        # this will be depricated, we will show the success msg in same page
+        # Redirect to same page with success message
+        messages.success(request, 'Sucess: Authority Updated.')
+        return HttpResponseRedirect(redirect_url)
 
 # XXX TODO: Factorize with portal/registrationview.py
 
@@ -346,14 +363,14 @@ def user_process(request):
                         user_params = { 'config': keypair, 'auth_type':'managed'}
                         manifold_update_account(request,user_params)
                         # updating sfa
-                        public_key = public_key.replace('"', '');
-                        user_pub_key = {'keys': public_key}
-                        sfa_update_user(request, user_hrn, user_pub_key)
+                        #public_key = public_key.replace('"', '');
+                        #user_pub_key = {'keys': public_key}
+                        #sfa_update_user(request, user_hrn, user_pub_key)
                         messages.success(request, 'Sucess: New Keypair Generated! Delegation of your credentials will be automatic.')
-                        return HttpResponseRedirect("/portal/account/")
+                        return HttpResponseRedirect(redirect_url)
         else:
             messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
-            return HttpResponseRedirect("/portal/account/")
+            return HttpResponseRedirect(redirect_url)
                        
     elif 'upload_key' in request.POST:
         for account_detail in account_details:
@@ -377,13 +394,13 @@ def user_process(request):
                             user_params = { 'config': file_content, 'auth_type':'user'}
                             manifold_update_account(request,user_params)
                             # updating sfa
-                            user_pub_key = {'keys': file_content}
-                            sfa_update_user(request, user_hrn, user_pub_key)
+                            #user_pub_key = {'keys': file_content}
+                            #sfa_update_user(request, user_hrn, user_pub_key)
                             messages.success(request, 'Publickey uploaded! Please delegate your credentials using SFA: http://trac.myslice.info/wiki/DelegatingCredentials')
-                            return HttpResponseRedirect("/portal/account/")
+                            return HttpResponseRedirect(redirect_url)
                         else:
                             messages.error(request, 'RSA key error: Please upload a valid RSA public key [.txt or .pub].')
-                            return HttpResponseRedirect("/portal/account/")
+                            return HttpResponseRedirect(redirect_url)
         else:
             messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
             return HttpResponseRedirect("/portal/account/")
@@ -401,7 +418,7 @@ def user_process(request):
                         break
         else:
             messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
-            return HttpResponseRedirect("/portal/account/")
+            return HttpResponseRedirect(redirect_url)
                
     elif 'dl_pkey' in request.POST:
         for account_detail in account_details:
@@ -416,7 +433,7 @@ def user_process(request):
                             return response
                         else:
                             messages.error(request, 'Download error: Private key is not stored in the server')
-                            return HttpResponseRedirect("/portal/account/")
+                            return HttpResponseRedirect(redirect_url)
 
         else:
             messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
@@ -441,11 +458,11 @@ def user_process(request):
                             return HttpResponseRedirect("/portal/account/")
                         else:
                             messages.error(request, 'Delete error: Private key is not stored in the server')
-                            return HttpResponseRedirect("/portal/account/")
+                            return HttpResponseRedirect(redirect_url)
                            
         else:
             messages.error(request, 'Account error: You need an account in myslice platform to perform this action')    
-            return HttpResponseRedirect("/portal/account/")
+            return HttpResponseRedirect(redirect_url)
 
     #clear all creds
     elif 'clear_cred' in request.POST:
@@ -466,10 +483,10 @@ def user_process(request):
                             return HttpResponseRedirect("/portal/account/")
                         else:
                             messages.error(request, 'Delete error: Credentials are not stored in the server')
-                            return HttpResponseRedirect("/portal/account/")
+                            return HttpResponseRedirect(redirect_url)
         else:
             messages.error(request, 'Account error: You need an account in myslice platform to perform this action')
-            return HttpResponseRedirect("/portal/account/")
+            return HttpResponseRedirect(redirect_url)
 
 
     # Download delegated_user_cred
@@ -481,10 +498,10 @@ def user_process(request):
             return response
         else:
             messages.error(request, 'Download error: User credential  is not stored in the server')
-            return HttpResponseRedirect("/portal/account/")
+            return HttpResponseRedirect(redirect_url)
         
     else:
         messages.info(request, 'Under Construction. Please try again later!')
-        return HttpResponseRedirect("/portal/account/")
+        return HttpResponseRedirect(redirect_url)