3 from random import randint
5 from django.core.mail import send_mail
6 from django.contrib.auth.models import User
7 from django.views.generic import View
8 from django.template.loader import render_to_string
9 from django.shortcuts import render
10 from django.contrib.auth import get_user_model
12 from unfold.page import Page
13 from unfold.loginrequired import FreeAccessView
14 from ui.topmenu import topmenu_items_live
16 from manifold.manifoldapi import execute_admin_query
17 from manifold.core.query import Query
19 from portal.models import PendingUser,PendingAuthority
20 from portal.actions import authority_get_pi_emails, manifold_add_user,manifold_add_account
22 # since we inherit from FreeAccessView we cannot redefine 'dispatch'
23 # so let's override 'get' and 'post' instead
25 class JoinView (FreeAccessView):
27 def post (self, request):
28 return self.get_or_post (request, 'POST')
30 def get (self, request):
31 return self.get_or_post (request, 'GET')
33 def get_or_post (self, request, method):
35 # List authorities already in the Registry in order to avoid duplicates
36 # Using cache manifold-tables to get the list of authorities faster
37 authorities_query = Query.get('authority').select('name', 'authority_hrn')
38 authorities = execute_admin_query(request, authorities_query)
39 if authorities is not None:
40 authorities = sorted(authorities)
41 root_authorities = sorted([a for a in authorities if '.' not in a['authority_hrn']])
44 page.add_js_files ( [ "js/jquery.validate.js", "js/join.js" ] )
45 page.add_css_files ( [ "css/onelab.css", "css/registration.css" ] )
46 page.add_css_files ( [ "http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" ] )
49 # xxx tocheck - if authorities is empty, it's no use anyway
50 # (users won't be able to validate the form anyway)
52 # List local users in Manifold DB in order ot avoid duplicates
53 user_query = Query().get('local:user').select('user_id','email')
54 list_users = execute_admin_query(self.request, user_query)
56 reg_root_authority_hrn = request.POST.get('root_authority_hrn', '').lower()
58 reg_site_name = request.POST.get('site_name', '')
59 reg_site_authority = request.POST.get('site_authority', '').lower()
60 reg_site_abbreviated_name = request.POST.get('site_abbreviated_name', '')
61 reg_site_url = request.POST.get('site_url', '')
62 reg_site_latitude = request.POST.get('site_latitude', '')
63 reg_site_longitude = request.POST.get('site_longitude', '')
65 reg_fname = request.POST.get('pi_first_name', '')
66 reg_lname = request.POST.get('pi_last_name', '')
67 reg_auth = reg_root_authority_hrn + "." + reg_site_authority
68 reg_email = request.POST.get('pi_email','').lower()
69 reg_phone = request.POST.get('pi_phone','')
71 split_email = reg_email.split("@")[0]
72 split_email = split_email.replace(".", "_")
73 user_hrn = reg_auth + '.' + split_email+ str(randint(1,1000000))
75 UserModel = get_user_model()
77 reg_address_line1 = request.POST.get('address_line1', '')
78 reg_address_line2 = request.POST.get('address_line2', '')
79 reg_address_line3 = request.POST.get('address_line3', '')
80 reg_address_city = request.POST.get('address_city', '')
81 reg_address_postalcode = request.POST.get('address_postalcode', '')
82 reg_address_state = request.POST.get('address_state', '')
83 reg_address_country = request.POST.get('address_country', '')
85 #POST value validation
86 if (re.search(r'^[\w+\s.@+-]+$', reg_fname)==None):
87 errors.append('First Name may contain only letters, numbers, spaces and @/./+/-/_ characters.')
88 if (re.search(r'^[\w+\s.@+-]+$', reg_lname) == None):
89 errors.append('Last Name may contain only letters, numbers, spaces and @/./+/-/_ characters.')
90 if (re.search(r'^\w+$', reg_site_authority) == None):
91 errors.append('Site Authority may contain only letters or numbers.')
92 # checking in django_db !!
93 if PendingUser.objects.filter(email__iexact=reg_email):
94 errors.append('Email is pending for validation. Please provide a new email address.')
95 if PendingAuthority.objects.filter(site_authority__iexact=reg_auth):
96 errors.append('This site is pending for validation.')
97 if PendingAuthority.objects.filter(site_name__iexact=reg_site_name):
98 errors.append('This site is pending for validation.')
100 if UserModel._default_manager.filter(email__iexact=reg_email):
101 errors.append('This email is not usable. Please contact the administrator or try with another email.')
102 for user_detail in list_users:
103 if user_detail['email']==reg_email:
104 errors.append('Email already registered in Manifold. Please provide a new email address.')
106 # XXX TODO: Factorize with portal/accountview.py
107 # if 'generate' in request.POST['question']:
108 from Crypto.PublicKey import RSA
109 private = RSA.generate(1024)
110 private_key = json.dumps(private.exportKey())
111 public = private.publickey()
112 public_key = json.dumps(public.exportKey(format='OpenSSH'))
115 account_config = '{"user_public_key":'+ public_key + ', "user_private_key":'+ private_key + ', "user_hrn":"'+ user_hrn + '"}'
116 auth_type = 'managed'
117 public_key = public_key.replace('"', '');
120 reg_password = request.POST['pi_password']
121 a = PendingAuthority(
122 site_name = reg_site_name,
123 site_authority = reg_root_authority_hrn + '.' + reg_site_authority,
124 site_abbreviated_name = reg_site_abbreviated_name,
125 site_url = reg_site_url,
126 site_latitude = reg_site_latitude,
127 site_longitude = reg_site_longitude,
128 address_line1 = reg_address_line1,
129 address_line2 = reg_address_line2,
130 address_line3 = reg_address_line3,
131 address_city = reg_address_city,
132 address_postalcode = reg_address_postalcode,
133 address_state = reg_address_state,
134 address_country = reg_address_country,
135 authority_hrn = reg_root_authority_hrn,
139 reg_password = request.POST['pi_password']
141 first_name = reg_fname,
142 last_name = reg_lname,
143 authority_hrn = reg_auth,
145 password = reg_password,
146 keypair = account_config,
151 # saves the user to django auth_user table [needed for password reset]
152 user = User.objects.create_user(reg_email, reg_email, reg_password)
154 #creating user to manifold local:user
155 user_config = '{"firstname":"'+ reg_fname + '", "lastname":"'+ reg_lname + '", "authority":"'+ reg_auth + '"}'
156 user_params = {'email': reg_email, 'password': reg_password, 'config': user_config, 'status': 1}
157 manifold_add_user(request,user_params)
158 #creating local:account in manifold
159 user_id = user_detail['user_id']+1 # the user_id for the newly created user in local:user
160 account_params = {'platform_id': 5, 'user_id': user_id, 'auth_type': auth_type, 'config': account_config}
161 manifold_add_account(request,account_params)
165 'first_name' : reg_fname,
166 'last_name' : reg_lname,
167 'authority_hrn' : reg_auth,
169 'user_hrn' : user_hrn,
170 'public_key' : public_key,
172 recipients = authority_get_pi_emails(request,reg_auth)
174 # We don't need to send this email to user.
175 # it's for the PI only
176 #if ctx['cc_myself']:
177 # recipients.append(ctx['email'])
179 msg = render_to_string('user_request_email.txt', ctx)
180 send_mail("Onelab New Authority request for %s submitted"%reg_email, msg, 'support@myslice.info', recipients)
181 return render(request, 'user_register_complete.html')
184 'topmenu_items': topmenu_items_live('join', page),
186 'pi_first_name': request.POST.get('pi_first_name', ''),
187 'pi_last_name': request.POST.get('pi_last_name', ''),
188 'pi_email': request.POST.get('pi_email', ''),
189 'pi_phone': request.POST.get('pi_phone', ''),
190 'pi_password': request.POST.get('pi_password', ''),
191 'site_name': request.POST.get('site_name', ''),
192 'site_authority': request.POST.get('site_authority', '').lower(),
193 'site_abbreviated_name': request.POST.get('site_abbreviated_name', ''),
194 'site_url': request.POST.get('site_url', ''),
195 'site_latitude': request.POST.get('site_latitude', ''),
196 'site_longitude': request.POST.get('site_longitude', ''),
197 'address_line1': request.POST.get('address_line1', ''),
198 'address_line2': request.POST.get('address_line2', ''),
199 'address_line3': request.POST.get('address_line3', ''),
200 'address_city': request.POST.get('address_city', ''),
201 'address_postalcode': request.POST.get('address_postalcode', ''),
202 'address_state': request.POST.get('address_state', ''),
203 'address_country': request.POST.get('address_country', ''),
204 'root_authority_hrn': request.POST.get('root_authority_hrn', '').lower(),
205 'root_authorities': root_authorities,
206 'authorities': authorities,
208 template_env.update(page.prelude_env ())
209 return render(request, 'join_view.html',template_env)