3 from random import randint
5 from hashlib import md5
6 from django.core.mail import EmailMultiAlternatives
7 from django.contrib.auth.models import User
8 from django.views.generic import View
9 from django.template.loader import render_to_string
10 from django.shortcuts import render
11 from django.contrib.auth import get_user_model
12 from django.contrib.sites.models import Site
14 from unfold.page import Page
15 from unfold.loginrequired import FreeAccessView
16 from ui.topmenu import topmenu_items_live
18 from manifoldapi.manifoldapi import execute_admin_query
19 from manifold.core.query import Query
21 from portal.models import PendingUser,PendingAuthority
22 from portal.actions import authority_get_pi_emails, manifold_add_user,manifold_add_account, create_pending_user
24 from myslice.theme import ThemeView
26 # since we inherit from FreeAccessView we cannot redefine 'dispatch'
27 # so let's override 'get' and 'post' instead
29 class JoinView (FreeAccessView, ThemeView):
31 def post (self, request):
32 return self.get_or_post (request, 'POST')
34 def get (self, request):
35 return self.get_or_post (request, 'GET')
37 def get_or_post (self, request, method):
39 # List authorities already in the Registry in order to avoid duplicates
40 # Using cache manifold-tables to get the list of authorities faster
41 authorities_query = Query.get('authority').select('name', 'authority_hrn')
42 authorities = execute_admin_query(request, authorities_query)
43 if authorities is not None:
44 authorities = sorted(authorities)
45 root_authorities = sorted([a for a in authorities if '.' not in a['authority_hrn']])
48 page.add_js_files ( [ "js/jquery.validate.js", "js/join.js", "js/jquery.qtip.min.js" ] )
49 page.add_css_files ( [ "css/onelab.css", "css/registration.css", "css/jquery.qtip.min.css" ] )
50 page.add_css_files ( [ "https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" ] )
53 # xxx tocheck - if authorities is empty, it's no use anyway
54 # (users won't be able to validate the form anyway)
56 # List local users in Manifold DB in order ot avoid duplicates
57 user_query = Query().get('local:user').select('user_id','email')
58 list_users = execute_admin_query(self.request, user_query)
60 reg_root_authority_hrn = request.POST.get('root_authority_hrn', '').lower()
62 reg_site_name = request.POST.get('site_name', '')
63 reg_site_authority = request.POST.get('site_authority', '').lower()
64 reg_site_abbreviated_name = request.POST.get('site_abbreviated_name', '').lower()
65 reg_site_url = request.POST.get('site_url', '')
66 reg_site_latitude = request.POST.get('site_latitude', '')
67 reg_site_longitude = request.POST.get('site_longitude', '')
69 reg_fname = request.POST.get('pi_first_name', '')
70 reg_lname = request.POST.get('pi_last_name', '')
71 reg_auth = 'onelab.' + reg_site_abbreviated_name
72 reg_email = request.POST.get('pi_email','').lower()
73 reg_phone = request.POST.get('pi_phone','')
75 split_email = reg_email.split("@")[0]
76 split_email = split_email.replace(".", "_")
77 user_hrn = reg_auth + '.' + split_email
79 UserModel = get_user_model()
81 reg_address_line1 = request.POST.get('address_line1', '')
82 reg_address_line2 = request.POST.get('address_line2', '')
83 reg_address_line3 = request.POST.get('address_line3', '')
84 reg_address_city = request.POST.get('address_city', '')
85 reg_address_postalcode = request.POST.get('address_postalcode', '')
86 reg_address_state = request.POST.get('address_state', '')
87 reg_address_country = request.POST.get('address_country', '')
89 #POST value validation
90 if (re.search(r'^[\w+\s.@+-]+$', reg_fname)==None):
91 errors.append('First Name may contain only letters, numbers, spaces and @/./+/-/_ characters.')
92 if (re.search(r'^[\w+\s.@+-]+$', reg_lname) == None):
93 errors.append('Last Name may contain only letters, numbers, spaces and @/./+/-/_ characters.')
94 #if (re.search(r'^\w+$', reg_site_authority) == None):
95 # errors.append('Site Authority may contain only letters or numbers.')
96 # checking in django_db !!
97 if PendingUser.objects.filter(email__iexact=reg_email):
98 errors.append('Email is pending for validation. Please provide a new email address.')
99 if PendingAuthority.objects.filter(site_abbreviated_name__iexact=reg_site_abbreviated_name):
100 errors.append('This site is pending for validation.')
101 #if PendingAuthority.objects.filter(site_name__iexact=reg_site_name):
102 # errors.append('This site is pending for validation.')
104 if UserModel._default_manager.filter(email__iexact=reg_email):
105 errors.append('This email is not usable. Please contact the administrator or try with another email.')
106 for user_detail in list_users:
107 if user_detail['email']==reg_email:
108 errors.append('Email already registered in Manifold. Please provide a new email address.')
110 # XXX TODO: Factorize with portal/accountview.py
111 # if 'generate' in request.POST['question']:
112 from Crypto.PublicKey import RSA
113 private = RSA.generate(1024)
114 private_key = json.dumps(private.exportKey())
115 public = private.publickey()
116 public_key = json.dumps(public.exportKey(format='OpenSSH'))
119 account_config = '{"user_public_key":'+ public_key + ', "user_private_key":'+ private_key + ', "user_hrn":"'+ user_hrn + '"}'
120 auth_type = 'managed'
121 public_key = public_key.replace('"', '');
122 private_key = private_key.replace('"', '');
125 reg_password = request.POST['pi_password']
126 a = PendingAuthority(
127 site_name = reg_site_name,
128 site_authority = reg_auth,
129 site_abbreviated_name = reg_site_abbreviated_name,
130 site_url = reg_site_url,
131 site_latitude = reg_site_latitude,
132 site_longitude = reg_site_longitude,
133 address_line1 = reg_address_line1,
134 address_line2 = reg_address_line2,
135 address_line3 = reg_address_line3,
136 address_city = reg_address_city,
137 address_postalcode = reg_address_postalcode,
138 address_state = reg_address_state,
139 address_country = reg_address_country,
140 authority_hrn = reg_root_authority_hrn,
144 reg_password = request.POST['pi_password']
145 salt = randint(1,100000)
147 current_site = Site.objects.get_current()
148 current_site = current_site.domain
150 email_hash = md5(str(salt)+reg_email).hexdigest()
152 'first_name' : reg_fname,
153 'last_name' : reg_lname,
154 'organization' : reg_site_name,
155 'authority_hrn' : reg_auth,
157 'password' : reg_password,
158 'public_key' : public_key,
159 'private_key' : private_key,
160 'current_site' : current_site,
161 'email_hash' : email_hash,
162 'user_hrn' : user_hrn,
164 'auth_type' : 'managed',
165 'validation_link': 'http://' + current_site + '/portal/email_activation/'+ email_hash
169 create_pending_user(request, user_request, user_detail)
170 # saves the user to django auth_user table [needed for password reset]
171 #user = User.objects.create_user(reg_email, reg_email, reg_password)
173 #creating user to manifold local:user
174 #user_config = '{"first_name":"'+ reg_fname + '", "last_name":"'+ reg_lname + '", "authority_hrn":"'+ reg_auth + '"}'
175 #user_params = {'email': reg_email, 'password': reg_password, 'config': user_config, 'status': 1}
176 #manifold_add_user(request,user_params)
177 #creating local:account in manifold
178 #user_id = user_detail['user_id']+1 # the user_id for the newly created user in local:user
179 #account_params = {'platform_id': 5, 'user_id': user_id, 'auth_type': auth_type, 'config': account_config}
180 #manifold_add_account(request,account_params)
185 'site_name' : reg_site_name,
186 'authority_hrn' : reg_root_authority_hrn + '.' + reg_site_authority,
187 'site_abbreviated_name' : reg_site_abbreviated_name,
188 'site_url' : reg_site_url,
189 'site_latitude' : reg_site_latitude,
190 'site_longitude' : reg_site_longitude,
191 'address_line1' : reg_address_line1,
192 'address_line2' : reg_address_line2,
193 'address_line3' : reg_address_line3,
194 'address_city' : reg_address_city,
195 'address_postalcode' : reg_address_postalcode,
196 'address_state' : reg_address_state,
197 'address_country' : reg_address_country,
198 'first_name' : reg_fname,
199 'last_name' : reg_lname,
200 'authority_hrn' : reg_auth,
202 'user_hrn' : user_hrn,
203 'public_key' : public_key,
205 recipients = authority_get_pi_emails(request,reg_auth)
207 # We don't need to send this email to user.
208 # it's for the PI only
209 #if ctx['cc_myself']:
210 # recipients.append(ctx['email'])
211 theme.template_name = 'authority_request_email.html'
212 html_content = render_to_string(theme.template, ctx)
214 theme.template_name = 'authority_request_email.txt'
215 text_content = render_to_string(theme.template, ctx)
217 theme.template_name = 'authority_request_email_subject.txt'
218 subject = render_to_string(theme.template, ctx)
219 subject = subject.replace('\n', '')
221 theme.template_name = 'email_default_sender.txt'
222 sender = render_to_string(theme.template, ctx)
223 sender = sender.replace('\n', '')
225 msg = EmailMultiAlternatives(subject, text_content, sender, recipients)
226 msg.attach_alternative(html_content, "text/html")
230 print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
232 self.template_name = 'join_complete.html'
233 return render(request, self.template, {'theme': self.theme})
234 #return render(request, 'user_register_complete.html')
237 'topmenu_items': topmenu_items_live('join', page),
239 'pi_first_name': request.POST.get('pi_first_name', ''),
240 'pi_last_name': request.POST.get('pi_last_name', ''),
241 'pi_email': request.POST.get('pi_email', ''),
242 'pi_phone': request.POST.get('pi_phone', ''),
243 'pi_password': request.POST.get('pi_password', ''),
244 'site_name': request.POST.get('site_name', ''),
245 'site_authority': request.POST.get('site_authority', '').lower(),
246 'site_abbreviated_name': request.POST.get('site_abbreviated_name', ''),
247 'site_url': request.POST.get('site_url', ''),
248 'site_latitude': request.POST.get('site_latitude', ''),
249 'site_longitude': request.POST.get('site_longitude', ''),
250 'address_line1': request.POST.get('address_line1', ''),
251 'address_line2': request.POST.get('address_line2', ''),
252 'address_line3': request.POST.get('address_line3', ''),
253 'address_city': request.POST.get('address_city', ''),
254 'address_postalcode': request.POST.get('address_postalcode', ''),
255 'address_state': request.POST.get('address_state', ''),
256 'address_country': request.POST.get('address_country', ''),
257 'root_authority_hrn': request.POST.get('root_authority_hrn', '').lower(),
258 'root_authorities': root_authorities,
259 'authorities': authorities,
262 template_env.update(page.prelude_env ())
263 return render(request, 'join_view.html',template_env)