3 from django.core.mail import send_mail
5 from django.views.generic import View
6 from django.template.loader import render_to_string
7 from django.shortcuts import render
9 from myslice.viewutils import topmenu_items
11 from manifold.manifoldapi import execute_query
12 from manifold.core.query import Query
14 from portal.models import PendingUser
15 from portal.actions import authority_get_pi_emails
17 # This is a rough porting from views.py
18 # the former function-based view is now made a class
19 # we redefine dispatch as it is simple
20 # and coincidentally works since we do not need LoginRequiredAutoLogoutView
21 # a second stab should redefine post and get instead
22 # also this was not thoroughly tested either, might miss some imports
25 class RegistrationView (View):
27 def dispatch (self, request):
31 authorities_query = Query.get('authority').filter_by('authority_hrn', 'included', ['ple.inria', 'ple.upmc']).select('name', 'authority_hrn')
32 #authorities_query = Query.get('authority').select('authority_hrn')
33 authorities = execute_query(request, authorities_query)
35 if request.method == 'POST':
36 # We shall use a form here
38 #get_email = PendingUser.objects.get(email)
39 reg_fname = request.POST.get('firstname', '')
40 reg_lname = request.POST.get('lastname', '')
41 #reg_aff = request.POST.get('affiliation','')
42 reg_auth = request.POST.get('authority_hrn', '')
43 reg_email = request.POST.get('email','').lower()
45 #POST value validation
46 if (re.search(r'^[\w+\s.@+-]+$', reg_fname)==None):
47 errors.append('First Name may contain only letters, numbers, spaces and @/./+/-/_ characters.')
48 #return HttpResponse("Only Letters, Numbers, - and _ allowd in First Name")
49 #return render(request, 'registration_view.html')
50 if (re.search(r'^[\w+\s.@+-]+$', reg_lname) == None):
51 errors.append('Last Name may contain only letters, numbers, spaces and @/./+/-/_ characters.')
52 #return HttpResponse("Only Letters, Numbers, - and _ is allowed in Last name")
53 #return render(request, 'registration_view.html')
54 # if (re.search(r'^[\w+\s.@+-]+$', reg_aff) == None):
55 # errors.append('Affiliation may contain only letters, numbers, spaces and @/./+/-/_ characters.')
56 #return HttpResponse("Only Letters, Numbers and _ is allowed in Affiliation")
57 #return render(request, 'registration_view.html')
58 # XXX validate authority hrn !!
59 if PendingUser.objects.filter(email__iexact=reg_email):
60 errors.append('Email already registered.Please provide a new email address.')
61 #return HttpResponse("Email Already exists")
62 #return render(request, 'registration_view.html')
63 if 'generate' in request.POST['question']:
64 # Generate public and private keys using SFA Library
65 from sfa.trust.certificate import Keypair
66 k = Keypair(create=True)
67 public_key = k.get_pubkey_string()
68 private_key = k.as_pem()
69 private_key = ''.join(private_key.split())
70 public_key = "ssh-rsa " + public_key
72 keypair = '{"user_public_key":"'+ public_key + '", "user_private_key":"'+ private_key + '"}'
73 # keypair = re.sub("\r", "", keypair)
74 # keypair = re.sub("\n", "\\n", keypair)
75 # #keypair = keypair.rstrip('\r\n')
76 # keypair = ''.join(keypair.split())
78 up_file = request.FILES['user_public_key']
79 file_content = up_file.read()
80 file_name = up_file.name
81 file_extension = os.path.splitext(file_name)[1]
82 allowed_extension = ['.pub','.txt']
83 if file_extension in allowed_extension and re.search(r'ssh-rsa',file_content):
84 keypair = '{"user_public_key":"'+ file_content +'"}'
85 keypair = re.sub("\r", "", keypair)
86 keypair = re.sub("\n", "\\n",keypair)
87 keypair = ''.join(keypair.split())
89 errors.append('Please upload a valid RSA public key [.txt or .pub].')
91 #b = PendingUser(first_name=reg_fname, last_name=reg_lname, affiliation=reg_aff,
92 # email=reg_email, password=request.POST['password'], keypair=keypair)
99 authority_hrn=reg_auth,
101 password=request.POST['password'],
108 'first_name' : reg_fname,
109 'last_name' : reg_lname,
110 'authority_hrn': reg_auth,
113 'cc_myself' : True # form.cleaned_data['cc_myself']
116 recipients = authority_get_pi_emails(request,reg_auth)
118 recipients.append(ctx['email'])
120 msg = render_to_string('user_request_email.txt', ctx)
121 send_mail("Onelab New User request for %s submitted"%reg_email, msg, reg_email, recipients)
123 return render(request, 'user_register_complete.html')
125 return render(request, 'registration_view.html',{
126 'topmenu_items': topmenu_items('Register', request),
128 'firstname': request.POST.get('firstname', ''),
129 'lastname': request.POST.get('lastname', ''),
130 #'affiliation': request.POST.get('affiliation', ''),
131 'authority_hrn': request.POST.get('authority_hrn', ''),
132 'email': request.POST.get('email', ''),
133 'password': request.POST.get('password', ''),
134 'authorities': authorities