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