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 unfold.loginrequired import FreeAccessView
12 from ui.topmenu import topmenu_items
14 from manifold.manifoldapi import execute_admin_query
15 from manifold.core.query import Query
17 from portal.models import PendingUser
18 from portal.actions import authority_get_pi_emails
20 # since we inherit from FreeAccessView we cannot redefine 'dispatch'
21 # so let's override 'get' and 'post' instead
23 class RegistrationView (FreeAccessView):
25 def post (self, request):
26 return self.get_or_post (request, 'POST')
28 def get (self, request):
29 return self.get_or_post (request, 'GET')
31 def get_or_post (self, request, method):
34 #authorities_query = Query.get('authority').\
35 # select('name', 'authority_hrn')
37 #onelab_enabled_query = Query.get('local:platform').filter_by('platform', '==', 'ple').filter_by('disabled', '==', 'False')
38 #onelab_enabled = not not execute_admin_query(request, onelab_enabled_query)
41 print "ONELAB ENABLED"
42 authorities_query = Query.get('ple:authority').select('name', 'authority_hrn').filter_by('authority_hrn', 'included', ['ple.inria', 'ple.upmc', 'ple.ibbtple', 'ple.nitos'])
44 print "FIREXP ENABLED"
46 authorities = execute_admin_query(request, authorities_query)
47 # xxx tocheck - if authorities is empty, it's no use anyway
48 # (users won't be able to validate the form anyway)
51 page.add_js_files ( [ "js/jquery.validate.js", "js/my_account.register.js" ] )
52 page.add_css_files ( [ "css/onelab.css", "css/registration.css" ] )
54 print 'registration view, method',method
57 # We shall use a form here
59 #get_email = PendingUser.objects.get(email)
60 reg_fname = request.POST.get('firstname', '')
61 reg_lname = request.POST.get('lastname', '')
62 #reg_aff = request.POST.get('affiliation','')
63 reg_auth = request.POST.get('authority_hrn', '')
64 reg_login = request.POST.get('login', '')
65 reg_email = request.POST.get('email','').lower()
67 #POST value validation
68 if (re.search(r'^[\w+\s.@+-]+$', reg_fname)==None):
69 errors.append('First Name may contain only letters, numbers, spaces and @/./+/-/_ characters.')
70 if (re.search(r'^[\w+\s.@+-]+$', reg_lname) == None):
71 errors.append('Last Name may contain only letters, numbers, spaces and @/./+/-/_ characters.')
72 # XXX validate authority hrn !!
73 if PendingUser.objects.filter(email__iexact=reg_email):
74 errors.append('Email already registered.Please provide a new email address.')
76 # XXX TODO: Factorize with portal/accountview.py
77 if 'generate' in request.POST['question']:
78 from Crypto.PublicKey import RSA
79 private = RSA.generate(1024)
80 private_key = json.dumps(private.exportKey())
81 public = private.publickey()
82 public_key = json.dumps(public.exportKey(format='OpenSSH'))
84 # # Generate public and private keys using SFA Library
85 # from sfa.trust.certificate import Keypair
86 # k = Keypair(create=True)
87 # public_key = k.get_pubkey_string()
88 # private_key = k.as_pem()
89 # private_key = ''.join(private_key.split())
90 # public_key = "ssh-rsa " + public_key
92 keypair = '{"user_public_key":'+ public_key + ', "user_private_key":'+ private_key + '}'
93 #keypair = re.sub("\r", "", keypair)
94 #keypair = re.sub("\n", "\\n", keypair)
95 #keypair = keypair.rstrip('\r\n')
96 #keypair = ''.join(keypair.split())
98 up_file = request.FILES['user_public_key']
99 file_content = up_file.read()
100 file_name = up_file.name
101 file_extension = os.path.splitext(file_name)[1]
102 allowed_extension = ['.pub','.txt']
103 if file_extension in allowed_extension and re.search(r'ssh-rsa',file_content):
104 keypair = '{"user_public_key":"'+ file_content +'"}'
105 keypair = re.sub("\r", "", keypair)
106 keypair = re.sub("\n", "\\n",keypair)
107 keypair = ''.join(keypair.split())
109 public_key = file_content
111 errors.append('Please upload a valid RSA public key [.txt or .pub].')
113 #b = PendingUser(first_name=reg_fname, last_name=reg_lname, affiliation=reg_aff,
114 # email=reg_email, password=request.POST['password'], keypair=keypair)
118 first_name = reg_fname,
119 last_name = reg_lname,
120 #affiliation = reg_aff,
121 authority_hrn = reg_auth,
124 password = request.POST['password'],
131 'first_name' : reg_fname,
132 'last_name' : reg_lname,
133 'authority_hrn' : reg_auth,
135 'keypair' : 'Public Key :' + public_key,
136 'cc_myself' : True # form.cleaned_data['cc_myself']
139 recipients = authority_get_pi_emails(request,reg_auth)
142 recipients.append(ctx['email'])
144 msg = render_to_string('user_request_email.txt', ctx)
145 send_mail("Onelab New User request for %s submitted"%reg_email, msg, reg_email, recipients)
147 return render(request, 'user_register_complete.html')
150 'topmenu_items': topmenu_items('Register', request),
152 'firstname': request.POST.get('firstname', ''),
153 'lastname': request.POST.get('lastname', ''),
154 #'affiliation': request.POST.get('affiliation', ''),
155 'authority_hrn': request.POST.get('authority_hrn', ''),
156 'email': request.POST.get('email', ''),
157 'password': request.POST.get('password', ''),
158 'authorities': authorities,
160 template_env.update(page.prelude_env ())
161 return render(request, 'registration_view.html',template_env)