forked from hforge/ikaaro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users_views.py
557 lines (416 loc) · 18.1 KB
/
users_views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# -*- coding: UTF-8 -*-
# Copyright (C) 2005-2008 Juan David Ibáñez Palomar <[email protected]>
# Copyright (C) 2006-2007 Hervé Cauwelier <[email protected]>
# Copyright (C) 2007 Henry Obein <[email protected]>
# Copyright (C) 2007-2008 Sylvain Taverne <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from itools
from itools.core import freeze, merge_dicts, proto_lazy_property
from itools.database import PhraseQuery, StartQuery, TextQuery
from itools.database import AndQuery, OrQuery
from itools.datatypes import String, Unicode
from itools.gettext import MSG
from itools.i18n import get_language_name
from itools.web import BaseView, FormError, STLView, INFO, ERROR
# Import from pytz
from pytz import common_timezones
# Import from ikaaro
from autoadd import AutoAdd
from autoedit import AutoEdit
from autoform import AutoForm, HiddenWidget, ReadOnlyWidget, TextWidget
from autoform import PasswordWidget, ChoosePassword_Widget
from buttons import Button, BrowseButton
from datatypes import ChoosePassword_Datatype
from emails import send_email
from fields import Password_Field, ChoosePassword_Field
import messages
from views import BrowseForm
class User_ConfirmRegistration(AutoForm):
access = True
title = MSG(u'Choose your password')
description = MSG(u'To activate your account, please type a password.')
schema = freeze({
'key': String(mandatory=True),
'username': String,
'newpass': ChoosePassword_Datatype(mandatory=True),
'newpass2': String(mandatory=True)})
widgets = freeze([
HiddenWidget('key'),
ReadOnlyWidget('username', title=MSG(u'Username')),
ChoosePassword_Widget('newpass', userid='username'),
PasswordWidget('newpass2', title=MSG(u'Repeat password'))])
def get_value(self, resource, context, name, datatype):
if name == 'key':
return resource.get_property('user_state').get_parameter('key')
if name == 'username':
return resource.get_login_name()
proxy = super(User_ConfirmRegistration, self)
return proxy.get_value(resource, context, name, datatype)
def get_namespace(self, resource, context):
# Check register key
username = context.get_form_value('username', default='')
key = resource.get_property('user_state').get_parameter('key')
if key is None:
goto = '/;login?username=%s' % username
return context.come_back(messages.MSG_REGISTERED, goto=goto)
elif context.get_form_value('key') != key:
goto ='/;login?username=%s' % username
return context.come_back(messages.MSG_BAD_KEY, goto=goto)
proxy = super(User_ConfirmRegistration, self)
return proxy.get_namespace(resource, context)
def _get_form(self, resource, context):
proxy = super(User_ConfirmRegistration, self)
form = proxy._get_form(resource, context)
if form['username'] == form['newpass']:
raise FormError, messages.MSG_PASSWORD_EQUAL_TO_USERNAME
return form
def action(self, resource, context, form):
# Check register key
key = resource.get_property('user_state').get_parameter('key')
if not key:
context.message = MSG(u'User is not pending')
return
if form['key'] != key:
context.message = messages.MSG_BAD_KEY
return
# Check passwords
password = form['newpass']
password2 = form['newpass2']
if password != password2:
context.message = messages.MSG_PASSWORD_MISMATCH
return
# Set user
resource.set_value('password', password)
resource.del_property('user_state')
# Set cookie
resource._login(password, context)
# Send email
to_addr = resource.get_value('email')
send_email('register-send-confirmation', context, to_addr,
user=resource)
# Ok
message = INFO(u'Operation successful! Welcome.')
return context.come_back(message, goto='./')
class User_ChangePasswordForgotten(User_ConfirmRegistration):
description = MSG(u'Please choose a new password for your account')
class User_ResendConfirmation(BaseView):
access = 'is_admin'
def GET(self, resource, context):
# Already confirmed
user_state = resource.get_value('user_state')
if user_state != 'pending':
msg = MSG(u'User has already confirmed his registration!')
return context.come_back(msg)
# Resend confirmation
resource.update_pending_key()
email = resource.get_value('email')
send_email('user-ask-for-confirmation', context, email, user=resource)
# Ok
msg = MSG(u'Confirmation sent!')
return context.come_back(msg)
class User_Profile(STLView):
access = 'is_allowed_to_view'
title = MSG(u'Profile')
description = MSG(u"User's profile page.")
icon = 'action_home.png'
template = '/ui/user/profile.xml'
items = ['edit_account', 'edit_preferences', 'edit_password']
def get_items(self, resource, context):
items = []
for name in self.items:
# Get the view & check access rights
view = resource.get_view(name)
if view and context.is_access_allowed(resource, view):
items.append({
'url': ';%s' % name,
'title': view.title,
'description': getattr(view, 'description', None),
'icon': resource.get_method_icon(view, size='48x48')})
return items
def get_namespace(self, resource, context):
avatar = resource.get_value('avatar')
state = resource.get_value('user_state')
return {
'firstname': resource.get_value('firstname'),
'lastname': resource.get_value('lastname'),
'avatar': avatar is not None,
'items': self.get_items(resource, context),
'user_is_active': (state == 'active')}
class User_EditAccount(AutoEdit):
access = 'is_allowed_to_edit'
title = MSG(u'Edit Account')
description = MSG(u'Edit your name and email address.')
icon = 'card.png'
# TODO The email address must be verified when changed. We should allow
# users to have several email addresses.
fields = ['firstname', 'lastname', 'avatar', 'email']
def set_value(self, resource, context, name, form):
field = resource.get_field(name)
if getattr(field, 'unique', False):
old_value = resource.get_value(name)
new_value = form[name]
if old_value != new_value:
query = PhraseQuery(name, new_value)
results = context.database.search(query)
if len(results):
error = (
u'There is another user with the "{value}" {name},'
u' please choose another one.')
context.message = ERROR(error, name=name, value=new_value)
return True
proxy = super(User_EditAccount, self)
return proxy.set_value(resource, context, name, form)
class User_EditPreferences(STLView):
access = 'is_allowed_to_edit'
title = MSG(u'Edit Preferences')
description = MSG(u'Set your preferred language and timezone.')
icon = 'preferences.png'
template = '/ui/user/edit_preferences.xml'
schema = {
'user_language': String,
'user_timezone': String}
def get_namespace(self, resource, context):
root = context.root
# Languages
user_language = resource.get_value('user_language')
languages = [
{'code': code,
'name': get_language_name(code),
'is_selected': code == user_language}
for code in root.get_available_languages() ]
# Timezone
user_timezone = resource.get_value('user_timezone')
timezones = [
{'name': name,
'is_selected': name == user_timezone}
for name in common_timezones ]
return {'languages': languages,
'timezones': timezones}
def action(self, resource, context, form):
value = form['user_language']
if value == '':
resource.del_property('user_language')
else:
resource.set_property('user_language', value)
value = form['user_timezone']
if value == '':
resource.del_property('user_timezone')
else:
resource.set_property('user_timezone', value)
# Ok
context.message = messages.MSG_CHANGES_SAVED
class User_EditPassword(AutoForm):
access = 'is_allowed_to_edit'
title = MSG(u'Edit Password')
description = MSG(u'Change your password.')
icon = 'lock.png'
schema = freeze({
'username': String,
'newpass': ChoosePassword_Datatype(mandatory=True),
'newpass2': String(mandatory=True)})
widgets = [
HiddenWidget('username'),
ChoosePassword_Widget('newpass', userid='username',
title=MSG(u'New password')),
PasswordWidget('newpass2', title=MSG(u'Confirm'))]
def get_schema(self, resource, context):
if resource.name != context.user.name:
return self.schema
return merge_dicts(self.schema, password=String(mandatory=True))
def get_widgets(self, resource, context):
if resource.name != context.user.name:
return self.widgets
title = MSG(u'Type your current password')
return self.widgets + [PasswordWidget('password', title=title)]
def get_value(self, resource, context, name, datatype):
if name == 'username':
return resource.get_login_name()
proxy = super(User_EditPassword, self)
return proxy.get_value(resource, context, name, datatype)
def _get_form(self, resource, context):
form = super(User_EditPassword, self)._get_form(resource, context)
# Strip password
newpass = form['newpass'].strip()
form['newpass'] = newpass
# Check username is different from password
if form['username'] == form['newpass']:
raise FormError, messages.MSG_PASSWORD_EQUAL_TO_USERNAME
# Check the new password matches
if newpass != form['newpass2']:
raise FormError, ERROR(u"Passwords mismatch, please try again.")
# Check old password
if resource.name == context.user.name:
password = form['password']
if not resource.authenticate(password):
message = ERROR(
u"You mistyped your actual password, your account is"
u" not changed.")
raise FormError, message
# Ok
return form
def action(self, resource, context, form):
# Clear confirmation key and set password
resource.set_value('user_state', None)
resource.set_value('password', form['newpass'].strip())
# Relogin
if resource.name == context.user.name:
context.login(resource)
# Ok
context.message = messages.MSG_CHANGES_SAVED
###########################################################################
# Container
###########################################################################
class BrowseUsers(BrowseForm):
access = 'is_admin'
title = MSG(u'Browse Members')
icon = 'userfolder.png'
description = MSG(u'See the users.')
schema = {'ids': String(multiple=True, mandatory=True)}
def get_query_schema(self):
schema = super(BrowseUsers, self).get_query_schema()
return merge_dicts(schema, sort_by=String(default='email'))
search_schema = {'search_term': Unicode}
search_widgets = [TextWidget('search_term', title=MSG(u'Search'))]
def get_items(self, resource, context):
# Build the Query
search_query = PhraseQuery('format', 'user')
search_term = context.query['search_term'].strip()
if search_term:
search_query = AndQuery(search_query)
or_query = OrQuery(
TextQuery('lastname', search_term),
TextQuery('firstname', search_term),
StartQuery('email', search_term),
StartQuery('email_domain', search_term))
search_query.append(or_query)
# Ok
return context.search(search_query)
def sort_and_batch(self, resource, context, results):
start = context.query['batch_start']
size = context.query['batch_size']
sort_by = context.query['sort_by']
reverse = context.query['reverse']
# Slow
if sort_by == 'account_state':
f = lambda x: self.get_item_value(resource, context, x,
sort_by)[0].gettext()
items = results.get_resources()
items = list(items)
items.sort(key=lambda x: f(x), reverse=reverse)
items = items[start:start+size]
database = resource.database
return [ database.get_resource(x.abspath) for x in items ]
# Fast
items = results.get_resources(sort_by, reverse, start, size)
return list(items)
table_columns = [
('checkbox', None),
('name', MSG(u'User ID')),
('email', MSG(u'Login')),
('firstname', MSG(u'First Name')),
('lastname', MSG(u'Last Name')),
('account_state', MSG(u'State'))]
def get_item_value(self, resource, context, item, column):
if column == 'checkbox':
return item.name, False
elif column == 'name':
return item.name, str(item.abspath)
elif column == 'account_state':
if item.get_value('user_state') == 'pending':
href = '/users/%s/;resend_confirmation' % item.name
return MSG(u'Resend Confirmation'), href
return item.get_value_title('user_state'), None
return item.get_value_title(column)
class Users_Browse(BrowseUsers):
table_actions = [
BrowseButton(access='is_admin', name='switch_state',
title=MSG(u'Switch state'))]
def action_switch_state(self, resource, context, form):
# Verify if after this operation, all is ok
usernames = form['ids']
if context.user.name in usernames:
context.message = ERROR(u'You cannot change your state yourself.')
return
database = resource.database
for username in usernames:
user = database.get_resource('/users/%s' % username)
email = user.get_value('email')
user_state = user.get_value('user_state')
if user_state == 'active':
user.set_value('user_state', 'inactive')
send_email('switch-state-deactivate', context, email)
elif user_state == 'inactive':
user.set_value('user_state', 'active')
send_email('switch-state-activate', context, email)
else: # pending
continue
# Ok
context.message = messages.MSG_CHANGES_SAVED
class Users_AddUser(AutoAdd):
access = 'is_admin'
title = MSG(u'Add New Member')
icon = 'card.png'
description = MSG(u'Grant access to a new user.')
fields = ['email', 'password', 'password2', 'groups']
password = ChoosePassword_Field(title=MSG(u'Password'), userid='email')
password.tip = MSG(u'If no password is given an email will be sent to the'
u' user, asking him to choose his password.')
password2 = Password_Field(title=MSG(u'Repeat password'), datatype=String)
@proto_lazy_property
def _resource_class(self):
return self.context.database.get_resource_class('user')
def _get_form(self, resource, context):
form = super(Users_AddUser, self)._get_form(resource, context)
# Check the password is not equal to the username
password = form['password'].strip()
if form['email'] == password:
raise FormError, messages.MSG_PASSWORD_EQUAL_TO_USERNAME
# Check whether the user already exists
email = form['email'].strip()
results = context.search(email=email)
if len(results):
raise FormError, ERROR(u'The user is already here.')
# Check the password is right
if password != form['password2']:
raise FormError, messages.MSG_PASSWORD_MISMATCH
if password == '':
form['password'] = None
return form
actions = [
Button(access='is_admin', css='button-ok',
title=MSG(u'Add and view')),
Button(access='is_admin', css='button-ok', name='add_and_return',
title=MSG(u'Add and return'))]
def get_container(self, resource, context, form):
return resource.get_resource('/users')
automatic_resource_name = True
def make_new_resource(self, resource, context, form):
proxy = super(Users_AddUser, self)
child = proxy.make_new_resource(resource, context, form)
# Send email to the new user
if child:
if form['password']:
email_id = 'add-user-send-notification'
else:
child.update_pending_key()
email_id = 'user-ask-for-confirmation'
send_email(email_id, context, form['email'], user=child)
# Ok
return child
def action_add_and_return(self, resource, context, form):
child = self.make_new_resource(resource, context, form)
if child is None:
return
context.message = INFO(u'User added.')