X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=portal%2Factions.py;h=b3ad5d6d6f0e0a41e9815bb222b69b96d986f4c2;hb=c37af65dca2c61a28b5a34a244a9a9707c02b5e8;hp=047288d220b1948150a28fc23d184d952ae91c5f;hpb=8de1053981b548a3d4791132c4d60fdf860221c3;p=myslice.git diff --git a/portal/actions.py b/portal/actions.py index 047288d2..b3ad5d6d 100644 --- a/portal/actions.py +++ b/portal/actions.py @@ -14,7 +14,7 @@ from django.core.mail import EmailMultiAlternatives, send_mail from myslice.theme import ThemeView from myslice.configengine import ConfigEngine - +from myslice.settings import logger theme = ThemeView() @@ -25,6 +25,61 @@ import activity.slice # XXX tmp sfa dependency, should be moved to SFA gateway #from sfa.util.xrn import Xrn +def get_myslice_platform(request): + platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled','config').filter_by('platform','==','myslice') + platform_details = execute_query(request, platform_query) + if isinstance(platform_details,list): + for platform_detail in platform_details: + return platform_detail + else: + return None + +def get_myslice_account(request): + try: + platform_myslice = get_myslice_platform(request) + account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config').filter_by('platform_id','==',platform_myslice['platform_id']) + account_details = execute_query(request, account_query) + if isinstance(account_details,list): + for account_detail in account_details: + return account_detail + else: + return None + except Exception as e: + print e + return None + +def get_registry_url(request): + try: + platform_detail = get_myslice_platform(request) + platform_config = json.loads(platform_detail['config']) + import socket + hostname = socket.gethostbyaddr(socket.gethostname())[0] + registry = platform_config.get('registry','N/A') + if 'localhost' in registry: + port = registry.split(':')[-1:][0] + registry = "http://" + hostname +':'+ port + return registry + except Exception as e: + print e + return None + +def get_jfed_identity(request): + try: + account_detail = get_myslice_account(request) + account_config = json.loads(account_detail['config']) + if 'user_private_key' in account_config: + private_key = account_config['user_private_key'] + user_hrn = account_config.get('user_hrn','N/A') + platform_detail = get_myslice_platform(request) + registry = get_registry_url(request) + #registry = 'http://sfa-fed4fire.pl.sophia.inria.fr:12345/' + jfed_identity = user_hrn + '\n' + registry + '\n' + private_key + return jfed_identity + else: + return None + except Exception as e: + print e + return None # Get the list of pis in a given authority def authority_get_pis(request, authority_hrn): @@ -81,8 +136,8 @@ def authority_check_pis(request, user_email): pi_status = True return pi_status - except Exception,e: - print "Exception in actions.py in authority_check_pis %s" % e + except Exception as e: + logger.error("Exception in actions.py in authority_check_pis {}".format(e)) return None @@ -98,8 +153,8 @@ def authority_add_pis(request, authority_hrn,user_hrn): results = execute_query(request,query) newpis = authority_get_pis (request, authority_hrn) return newpis - except Exception,e: - print "Exception in actions.py in authority_add_pis %s" % e + except Exception as e: + logger.error("Exception in actions.py in authority_add_pis {}".format(e)) return None @@ -115,14 +170,14 @@ def authority_remove_pis(request, authority_hrn,user_hrn): results = execute_query(request,query) newpis = authority_get_pis (request, authority_hrn) return newpis - except Exception,e: - print "Exception in actions.py in authority_remove_pis %s" % e + except Exception as e: + logger.error("Exception in actions.py in authority_remove_pis {}".format(e)) return None def authority_get_pi_emails(request, authority_hrn): pi_users = authority_get_pis(request,authority_hrn) - print "pi_users = %s" % pi_users + logger.info("pi_users = %s" % pi_users) if any(pi['pi_users'] == None or not pi['pi_users'] for pi in pi_users): #theme.template_name = 'email_default_recipients.txt' @@ -174,13 +229,13 @@ def clear_user_creds(request, user_email): else: return None - except Exception,e: - print "Exception in actions.py in clear_user_creds %s" % e + except Exception as e: + logger.error("Exception in actions.py in clear_user_creds {}".format(e)) return None def is_pi(wsgi_request, user_hrn, authority_hrn): # authorities from user where user_hrn == "ple.upmc.jordan_auge" - print "#### actions.py is_pi authority_hrn = ", authority_hrn + logger.debug("#### actions.py is_pi authority_hrn = {}".format(authority_hrn)) try: # CACHE PB with fields page = Page(wsgi_request) @@ -192,12 +247,11 @@ 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 for user_detail in results: if authority_hrn in user_detail['pi_authorities']: return True - except Exception,e: - print "Exception in actions.py in is_pi %s" % e + except Exception as e: + logger.error("Exception in actions.py in is_pi {}".format(e)) return False # SFA get record @@ -224,7 +278,7 @@ def sfa_add_authority(request, authority_params): # REGISTRY ONLY TO BE REMOVED WITH MANIFOLD-V2 query = Query.create('myslice:authority').set(authority_params).select('authority_hrn') results = execute_query(request, query) - print "sfa_add_auth results=",results + logger.info("sfa_add_auth results={}".format(results)) if not results: raise Exception, "Could not create %s. Already exists ?" % authority_params['hrn'] return results @@ -305,13 +359,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 +381,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 = {} @@ -399,7 +484,7 @@ def make_request_authority(authority): return request def make_requests(pending_users, pending_slices, pending_authorities, pending_projects, pending_joins): - print "$$$$$$$$$$$$$$$ make_request" + logger.info("$$$$$$$$$$$$$$$ make_request") requests = [] for user in pending_users: requests.append(make_request_user(user)) @@ -414,7 +499,7 @@ def make_requests(pending_users, pending_slices, pending_authorities, pending_pr return requests def get_request_by_id(ids): - print "$$$$$$$$$$$$$$$$ get_request_by_id" + logger.info("$$$$$$$$$$$$$$$$ get_request_by_id") sorted_ids = { 'user': [], 'slice': [], 'authority': [], 'project': [], 'join': [] } for type__id in ids: type, id = type__id.split('__') @@ -436,7 +521,7 @@ def get_request_by_id(ids): return make_requests(pending_users, pending_slices, pending_authorities, pending_projects, pending_joins) def get_requests(authority_hrns=None): - print "$$$$$$$$$$$$$ get_request_by_authority auth_hrns = ", authority_hrns + logger.info("$$$$$$$$$$$$$ get_request_by_authority auth_hrns = {}".format(authority_hrns)) if not authority_hrns: ## get those pending users who have confirmed their emails pending_users = PendingUser.objects.filter(status__iexact = 'True') @@ -543,7 +628,7 @@ def portal_validate_request(wsgi_request, request_ids): #'pi' : None, #'enabled' : True } - print "ADD Authority" + logger.info("ADD Authority") sfa_add_authority(wsgi_request, sfa_authority_params) request_status['SFA authority'] = {'status': True } PendingAuthority.objects.get(id=request['id']).delete() @@ -598,14 +683,14 @@ def validate_action(request, **kwargs): ids = filter(None, kwargs['id'].split('/')) status = portal_validate_request(request, ids) json_answer = json.dumps(status) - return HttpResponse (json_answer, mimetype="application/json") + return HttpResponse (json_answer, content_type="application/json") def reject_action(request, **kwargs): ids = filter(None, kwargs['id'].split('/')) status = portal_reject_request(request, ids) json_answer = json.dumps(status) - return HttpResponse (json_answer, mimetype="application/json") + return HttpResponse (json_answer, content_type="application/json") def portal_reject_request(wsgi_request, request_ids): @@ -656,33 +741,13 @@ def portal_reject_request(wsgi_request, request_ids): msg = EmailMultiAlternatives(subject, text_content, sender, [user_email]) msg.attach_alternative(html_content, "text/html") 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() + except Exception as e: + logger.error("Failed to send email, please check the mail templates and the SMTP configuration of your server") + # 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)} @@ -717,8 +782,8 @@ def portal_reject_request(wsgi_request, request_ids): msg = EmailMultiAlternatives(subject, text_content, sender, [user_email]) msg.attach_alternative(html_content, "text/html") msg.send() - except Exception, e: - print "Failed to send email, please check the mail templates and the SMTP configuration of your server" + except Exception as e: + logger.error("Failed to send email, please check the mail templates and the SMTP configuration of your server") PendingSlice.objects.get(id=request['id']).delete() @@ -756,8 +821,8 @@ def portal_reject_request(wsgi_request, request_ids): msg = EmailMultiAlternatives(subject, text_content, sender, [user_email]) msg.attach_alternative(html_content, "text/html") msg.send() - except Exception, e: - print "Failed to send email, please check the mail templates and the SMTP configuration of your server" + except Exception as e: + logger.error("Failed to send email, please check the mail templates and the SMTP configuration of your server") PendingAuthority.objects.get(id=request['id']).delete() @@ -855,8 +920,8 @@ def create_slice(wsgi_request, request): msg = EmailMultiAlternatives(subject, text_content, sender, [user_email]) msg.attach_alternative(html_content, "text/html") msg.send() - except Exception, e: - print "Failed to send email, please check the mail templates and the SMTP configuration of your server" + except Exception as e: + logger.error("Failed to send email, please check the mail templates and the SMTP configuration of your server") return results @@ -893,8 +958,8 @@ def create_pending_slice(wsgi_request, request, email): msg = EmailMultiAlternatives(subject, text_content, sender, recipients) msg.attach_alternative(html_content, "text/html") msg.send() - except Exception, e: - print "Failed to send email, please check the mail templates and the SMTP configuration of your server" + except Exception as e: + logger.error("Failed to send email, please check the mail templates and the SMTP configuration of your server") def create_pending_project(wsgi_request, request): @@ -1045,13 +1110,13 @@ def sfa_create_user(wsgi_request, request, namespace = None, as_admin = False): sender = sender.replace('\n', '') - subject = 'User validated' + subject = 'Account validated' msg = EmailMultiAlternatives(subject, text_content, sender, [request['email']]) msg.attach_alternative(html_content, "text/html") msg.send() - except Exception, e: - print "Failed to send email, please check the mail templates and the SMTP configuration of your server" + except Exception as e: + logger.error("Failed to send email, please check the mail templates and the SMTP configuration of your server") return results @@ -1089,7 +1154,7 @@ def iotlab_create_user (wsgi_request, request, namespace = None, as_admin=False) iotlab_user_params1 = json.dumps(iotlab_user_params) r=requests.post(url=URL_REST, data=iotlab_user_params1, headers=headers, auth=auth) - print 'Create iotlab user : ', r.status_code, r.text + logger.info('Create iotlab user : {} {}'.format(r.status_code, r.text)) return r.text def create_user(wsgi_request, request, namespace = None, as_admin = False): @@ -1185,8 +1250,8 @@ def create_pending_user(wsgi_request, request, user_detail): 'config' : json.dumps(account_config), } manifold_add_account(wsgi_request, account_params) - except Exception, e: - print "Failed creating manifold account on platform %s for user: %s" % ('myslice', request['email']) + except Exception as e: + logger.error("Failed creating manifold account on platform {} for user: {}".format('myslice', request['email'])) try: # Send an email: the recipients are the PI of the authority @@ -1211,6 +1276,6 @@ def create_pending_user(wsgi_request, request, user_detail): msg.attach_alternative(html_content, "text/html") msg.send() except Exception, e: - print "Failed to send email, please check the mail templates and the SMTP configuration of your server" + logger.error("Failed to send email, please check the mail templates and the SMTP configuration of your server") import traceback - traceback.print_exc() + logger.error(traceback.format_exc())