From c457730e8d49ee507846edeb8a474374f0122f25 Mon Sep 17 00:00:00 2001 From: Loic Baron Date: Tue, 17 Mar 2015 18:52:13 +0100 Subject: [PATCH] delete_local_user available from rest, check if user is a pi of the authority for security --- myslice/urls.py | 1 + portal/actions.py | 67 ++++--- portal/static/js/institution.js | 21 +- .../fed4fire/fed4fire_institution.html | 2 +- portal/templates/institution.html | 2 +- .../smartfire/smartfire_institution.html | 185 ------------------ rest/__init__.py | 5 +- rest/local_user.py | 28 +++ 8 files changed, 88 insertions(+), 223 deletions(-) delete mode 100644 portal/templates/smartfire/smartfire_institution.html create mode 100644 rest/local_user.py diff --git a/myslice/urls.py b/myslice/urls.py index ad151789..f792a057 100644 --- a/myslice/urls.py +++ b/myslice/urls.py @@ -79,6 +79,7 @@ urls = [ (r'^update/(?P[^/]+)/(?P[^/]+)?/?$', 'rest.update.dispatch'), (r'^create/(?P[^/]+)/(?P[^/]+)?/?$', 'rest.create.dispatch'), (r'^delete/(?P[^/]+)/(?P[^/]+)?/?$', 'rest.delete.dispatch'), + (r'^local_user/(?P[^/]+)/?$', 'rest.local_user.dispatch'), (r'^credentials/(?P[^/]+)/?$', 'rest.credentials.dispatch'), (r'^cache/(?P[^/]+)/?$', 'rest.cache.dispatch'), (r'^initscript/(?P[^/]+)/?$', 'rest.initscript.dispatch'), diff --git a/portal/actions.py b/portal/actions.py index 047288d2..82221abc 100644 --- a/portal/actions.py +++ b/portal/actions.py @@ -192,7 +192,7 @@ def is_pi(wsgi_request, user_hrn, authority_hrn): query = Query().get('myslice:user').select(user_fields).filter_by('user_hrn','==',user_hrn) #query = Query.get('myslice:user').filter_by('user_hrn', '==', user_hrn).select('pi_authorities') results = execute_query(wsgi_request, query) - print "is_pi results = ", results + #print "is_pi results = ", results for user_detail in results: if authority_hrn in user_detail['pi_authorities']: return True @@ -305,13 +305,15 @@ def manifold_update_account(request,user_id,account_params): return results #explicitly mention the platform_id -def manifold_delete_account(request, platform_id, user_id, account_params): - query = Query.delete('local:account').filter_by('platform_id', '==', platform_id).filter_by('user_id', '==', user_id).set(account_params).select('user_id') +def manifold_delete_account(request, user_id, platform_id = None): + query = Query.delete('local:account').filter_by('user_id', '==', user_id) + if platform_id is not None: + query.filter_by('platform_id', '==', platform_id) results = execute_admin_query(request,query) return results -def manifold_delete_user(request, user_id, user_params): - query = Query.delete('local:user').filter_by('user_id', '==', user_id).set(user_params).select('user_id') +def manifold_delete_user(request, user_id): + query = Query.delete('local:user').filter_by('user_id', '==', user_id).select('user_id') results = execute_admin_query(request,query) return results @@ -325,6 +327,35 @@ def manifold_add_platform(request, platform_params): result, = results return result['platform_id'] +def delete_local_user(wsgi_request, user_email): + user_query = Query().get('local:user') \ + .filter_by('email', '==', user_email) \ + .select('user_id','config') + user = execute_admin_query(wsgi_request, user_query) + if len(user) == 0: + return False + #raise Exception, "User not found, check local DB" + else: + user_id = user[0]['user_id'] + user_config = json.loads(user[0]['config']) + authority_hrn = user_config.get('authority', None) + + if is_pi(wsgi_request, '$user_hrn', authority_hrn): + # removing from Django auth_user + UserModel = get_user_model() + UserModel._default_manager.filter(email__iexact = user_email).delete() + + # removing manifold account + manifold_delete_account(wsgi_request, user_id) + + # removing manifold user + manifold_delete_user(wsgi_request, user_id) + else: + return False + #raise Exception, "No sufficient rights on authority = ",authority_hrn + + return True + def make_request_user(user): request = {} @@ -658,31 +689,11 @@ def portal_reject_request(wsgi_request, request_ids): msg.send() except Exception, e: print "Failed to send email, please check the mail templates and the SMTP configuration of your server" - - # removing from Django auth_user - UserModel = get_user_model() - UserModel._default_manager.filter(email__iexact = user_email).delete() + # removing from Django portal_pendinguser PendingUser.objects.get(id=request['id']).delete() - # removing from manifold - # removing manifold account - user_query = Query().get('local:user') \ - .filter_by('email', '==', user_email) \ - .select('user_id') - user = execute_admin_query(wsgi_request, user_query) - user_id = user[0]['user_id'] - - platform_query = Query().get('local:platform') \ - .filter_by('platform', '==', 'myslice') \ - .select('platform_id') - platform = execute_admin_query(wsgi_request, platform_query) - platform_id = platform[0]['platform_id'] - account_params = {'user_id':user_id} - manifold_delete_account(request, platform_id, user_id, account_params) - - # removing manifold user - user_params = {'user_id':user_id} - manifold_delete_user(request, user_id, user_params) + + delete_local_user(wsgi_request, user_email) except Exception, e: request_status['SFA authority'] = {'status': False, 'description': str(e)} diff --git a/portal/static/js/institution.js b/portal/static/js/institution.js index e8d5121c..a48a9a4b 100644 --- a/portal/static/js/institution.js +++ b/portal/static/js/institution.js @@ -20,16 +20,27 @@ $(document).ready(function() { $('input:checkbox.user').each(function (index) { if(this.checked){ var record_id = this.id; - $.post("/delete/user/",{'filters':{'user_hrn':this.id}}, function(data) { + var user_email = this.dataset.email; + console.log(this); + // Delete in SFA Registry + $.post("/delete/user/",{'filters':{'user_hrn':record_id}}, function(data) { if(data.success){ - $('tr[id="'+record_id+'"]').fadeOut("slow"); - $('tr[id="'+record_id+'"]').remove(); - mysliceAlert('Success: user deleted','success', true); + $.post("/local_user/delete/",{'filters':{'email':user_email}}, function(data) { + console.log(data); + if (data == true){ + mysliceAlert('Success: user deleted','success', true); + }else{ + mysliceAlert('Local DB Error for: '+record_id,'warning', true); + } + $('tr[id="'+record_id+'"]').fadeOut("slow"); + $('tr[id="'+record_id+'"]').remove(); + }); + //$.post("/delete/local:user/",{'filters':{'user_hrn':this.id}}, function(data) { }else{ mysliceAlert('Rest Error for: '+data.error,'warning', true); //alert("Rest Error for "+record_id+": "+data.error); } - }); + }); } }); }); diff --git a/portal/templates/fed4fire/fed4fire_institution.html b/portal/templates/fed4fire/fed4fire_institution.html index 90da9840..7e9c176e 100644 --- a/portal/templates/fed4fire/fed4fire_institution.html +++ b/portal/templates/fed4fire/fed4fire_institution.html @@ -209,7 +209,7 @@ $(document).ready(function() { $.each( data, function( key, val ) { list_users.push( "
  • " + val.user_email + "
  • " ); user_row = ""; - user_row += ""; + user_row += ""; user_row += ""+val.user_email+""; user_row += ""+val.user_hrn+""; /* diff --git a/portal/templates/institution.html b/portal/templates/institution.html index 30fad6cf..04948553 100644 --- a/portal/templates/institution.html +++ b/portal/templates/institution.html @@ -169,7 +169,7 @@ $(document).ready(function() { $.each( data, function( key, val ) { list_users.push( "
  • " + val.user_email + "
  • " ); user_row = ""; - user_row += ""; + user_row += ""; user_row += ""+val.user_email+""; user_row += ""+val.user_hrn+""; /* diff --git a/portal/templates/smartfire/smartfire_institution.html b/portal/templates/smartfire/smartfire_institution.html deleted file mode 100644 index bd361de2..00000000 --- a/portal/templates/smartfire/smartfire_institution.html +++ /dev/null @@ -1,185 +0,0 @@ -{% extends "layout_wide.html" %} - -{% block head %} - -{% endblock head %} - -{% block content %} -
    -
    -
    - -
    -
    -
    -
    -
    -
    - -
    -
    -
    -
    -
    -
    - -
    -
    -
    Loading Slices
    - - {%if 'is_pi' in pi %} -
    - -
    - {% endif %} -
    -
    - -
    -
    -
    Loading Slices
    - - {% if 'is_pi' in pi %} -
    - {% if 'is_pi' in pi %} - - {% else %} - - {% endif %} - - -
    - {% endif %} -
    -
    -
    -
    -
    - -{% endblock %} diff --git a/rest/__init__.py b/rest/__init__.py index 96b901f7..c3484835 100644 --- a/rest/__init__.py +++ b/rest/__init__.py @@ -1,5 +1,6 @@ from manifold.core.query import Query from manifoldapi.manifoldapi import execute_query +from portal.actions import is_pi from django.http import HttpResponse @@ -45,8 +46,7 @@ class ObjectRequest(object): table = self.type.split(':') prefix = table[0] table = table[1] - - if prefix is 'local': + if prefix == 'local': # XXX TODO: find a generic Query to get the fields like # select column.name from local:object where table == local:user table = self.type.split(':') @@ -74,7 +74,6 @@ class ObjectRequest(object): def setKey(self): # What about key formed of multiple fields??? query = Query.get('local:object').filter_by('table', '==', self.type).select('key') - print query results = execute_query(self.request, query) print "key of object = %s" % results if results : diff --git a/rest/local_user.py b/rest/local_user.py new file mode 100644 index 00000000..82b2c4da --- /dev/null +++ b/rest/local_user.py @@ -0,0 +1,28 @@ +from django.http import HttpResponse +from portal.actions import delete_local_user +import json + +def dispatch(request, action): + + if (action == 'delete') : + res=[] + email = request.POST.get('email') + if not email: + email = request.GET.get('email') + + ret = delete_local_user(request, email) + #if emails : + # for email in emails : + # try : + # clear = clear_user_creds(request, email) + # if clear is not None: + # res.append(clear) + # except : + # pass + # + #if not res : + # ret = { "ret" : 1, "emails" : emails } + #else : + # ret = { "ret" : 0 } + + return HttpResponse(json.dumps(ret), content_type="application/json") -- 2.43.0