4 from django.core.mail import send_mail
6 from django.views.generic import View
7 from django.template.loader import render_to_string
8 from django.shortcuts import render
10 from unfold.page import Page
11 from ui.topmenu import topmenu_items
13 from manifold.manifoldapi import execute_admin_query
14 from manifold.core.query import Query
16 from portal.models import PendingUser
17 from portal.actions import authority_get_pi_emails
19 # This is a rough porting from views.py
20 # the former function-based view is now made a class
21 # we redefine dispatch as it is simple
22 # and coincidentally works since we do not need LoginRequiredAutoLogoutView
23 # a second stab should redefine post and get instead
24 # also this was not thoroughly tested either, might miss some imports
27 class RegistrationView (View):
29 def dispatch (self, request):
33 authorities_query = Query.get('authority').\
34 select('name', 'authority_hrn')
36 onelab_enabled_query = Query.get('local:platform').filter_by('platform', '==', 'ple-onelab').filter_by('disabled', '==', 'False')
37 #onelab_enabled = not not execute_admin_query(request, onelab_enabled_query)
40 print "ONELAB ENABLED"
41 authorities_query = authorities_query.filter_by('authority_hrn', 'included', ['ple.inria', 'ple.upmc', 'ple.ibbtple'])
43 print "FIREXP ENABLED"
45 authorities = execute_admin_query(request, authorities_query)
46 # xxx tocheck - if authorities is empty, it's no use anyway
47 # (users won't be able to validate the form anyway)
50 page.add_js_files ( [ "js/jquery.validate.js", "js/my_account.register.js" ] )
51 page.add_css_files ( [ "css/onelab.css", "css/registration.css" ] )
53 print 'registration view, method',request.method
55 if request.method == 'POST':
56 # We shall use a form here
58 #get_email = PendingUser.objects.get(email)
59 reg_fname = request.POST.get('firstname', '')
60 reg_lname = request.POST.get('lastname', '')
61 #reg_aff = request.POST.get('affiliation','')
62 reg_auth = request.POST.get('authority_hrn', '')
63 reg_email = request.POST.get('email','').lower()
65 #POST value validation
66 if (re.search(r'^[\w+\s.@+-]+$', reg_fname)==None):
67 errors.append('First Name may contain only letters, numbers, spaces and @/./+/-/_ characters.')
68 if (re.search(r'^[\w+\s.@+-]+$', reg_lname) == None):
69 errors.append('Last Name may contain only letters, numbers, spaces and @/./+/-/_ characters.')
70 # XXX validate authority hrn !!
71 if PendingUser.objects.filter(email__iexact=reg_email):
72 errors.append('Email already registered.Please provide a new email address.')
74 # XXX TODO: Factorize with portal/accountview.py
75 if 'generate' in request.POST['question']:
76 from Crypto.PublicKey import RSA
77 private = RSA.generate(1024)
78 private_key = json.dumps(private.exportKey())
79 public = private.publickey()
80 public_key = json.dumps(public.exportKey(format='OpenSSH'))
82 # # Generate public and private keys using SFA Library
83 # from sfa.trust.certificate import Keypair
84 # k = Keypair(create=True)
85 # public_key = k.get_pubkey_string()
86 # private_key = k.as_pem()
87 # private_key = ''.join(private_key.split())
88 # public_key = "ssh-rsa " + public_key
90 keypair = '{"user_public_key":'+ public_key + ', "user_private_key":'+ private_key + '}'
91 #keypair = re.sub("\r", "", keypair)
92 #keypair = re.sub("\n", "\\n", keypair)
93 #keypair = keypair.rstrip('\r\n')
94 #keypair = ''.join(keypair.split())
96 up_file = request.FILES['user_public_key']
97 file_content = up_file.read()
98 file_name = up_file.name
99 file_extension = os.path.splitext(file_name)[1]
100 allowed_extension = ['.pub','.txt']
101 if file_extension in allowed_extension and re.search(r'ssh-rsa',file_content):
102 keypair = '{"user_public_key":"'+ file_content +'"}'
103 keypair = re.sub("\r", "", keypair)
104 keypair = re.sub("\n", "\\n",keypair)
105 keypair = ''.join(keypair.split())
107 public_key = file_content
109 errors.append('Please upload a valid RSA public key [.txt or .pub].')
111 #b = PendingUser(first_name=reg_fname, last_name=reg_lname, affiliation=reg_aff,
112 # email=reg_email, password=request.POST['password'], keypair=keypair)
116 first_name=reg_fname,
118 #affiliation=reg_aff,
119 authority_hrn=reg_auth,
121 password=request.POST['password'],
128 'first_name' : reg_fname,
129 'last_name' : reg_lname,
130 'authority_hrn' : reg_auth,
132 'keypair' : 'Public Key :' + public_key,
133 'cc_myself' : True # form.cleaned_data['cc_myself']
136 #recipients = authority_get_pi_emails(request,reg_auth)
137 recipients = ['devel@myslice.info']
139 recipients.append(ctx['email'])
141 msg = render_to_string('user_request_email.txt', ctx)
144 send_mail("Onelab New User request for %s submitted"%reg_email, msg, reg_email, recipients)
146 return render(request, 'user_register_complete.html')
149 'topmenu_items': topmenu_items('Register', request),
151 'firstname': request.POST.get('firstname', ''),
152 'lastname': request.POST.get('lastname', ''),
153 #'affiliation': request.POST.get('affiliation', ''),
154 'authority_hrn': request.POST.get('authority_hrn', ''),
155 'email': request.POST.get('email', ''),
156 'password': request.POST.get('password', ''),
157 'authorities': authorities,
159 template_env.update(page.prelude_env ())
160 return render(request, 'registration_view.html',template_env)