-
Notifications
You must be signed in to change notification settings - Fork 422
/
views.py
594 lines (479 loc) · 20.8 KB
/
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# -*- coding: utf-8 -*-
import json
import deform
from hem.db import get_session
from horus.lib import FlashMessage
from horus.resources import UserFactory
from pyramid import httpexceptions
from pyramid.view import view_config, view_defaults
from pyramid.security import forget
from pyramid_mailer import get_mailer
from pyramid_mailer.message import Message
from h.resources import Application
from h.notification.models import Subscriptions
from h.models import _
from h.accounts.models import User
from h.accounts.models import Activation
from h.accounts.events import ActivationEvent
from h.accounts.events import PasswordResetEvent
from h.accounts.events import LogoutEvent
from h.accounts.events import LoginEvent
from h.accounts.events import RegistrationEvent
from h.accounts import schemas
from h import session
def ajax_form(request, result):
if isinstance(result, httpexceptions.HTTPRedirection):
request.response.headers.extend(result.headers)
result = {'status': 'okay'}
elif isinstance(result, httpexceptions.HTTPError):
request.response.status_code = result.code
result = {'status': 'failure', 'reason': str(result)}
elif 'errors' in result:
request.response.status_code = result.pop('code', 400)
result['status'] = 'failure'
result['flash'] = session.pop_flash(request)
return result
def validate_form(form, data):
"""Validate POST payload data for a form."""
try:
appstruct = form.validate(data)
except deform.ValidationFailure as err:
return {'errors': err.error.asdict()}, None
else:
return None, appstruct
def view_auth_defaults(fn, *args, **kwargs):
kwargs.setdefault('accept', 'text/html')
kwargs.setdefault('layout', 'auth')
kwargs.setdefault('renderer', 'h:templates/auth.html')
return view_defaults(*args, **kwargs)(fn)
@view_config(accept='application/json', renderer='json',
context='pyramid.exceptions.BadCSRFToken')
def bad_csrf_token(context, request):
request.response.status_code = 403
reason = _('Session is invalid. Please try again.')
return {
'status': 'failure',
'reason': reason,
'model': session.model(request),
}
class AsyncFormViewMapper(object):
def __init__(self, **kw):
self.attr = kw['attr']
def __call__(self, view):
def wrapper(context, request):
if request.method == 'POST':
data = request.json_body
data.update(request.params)
request.content_type = 'application/x-www-form-urlencoded'
request.POST.clear()
request.POST.update(data)
inst = view(request)
meth = getattr(inst, self.attr)
result = meth()
result = ajax_form(request, result)
model = result.setdefault('model', {})
model.update(session.model(request))
result.pop('form', None)
return result
return wrapper
@view_auth_defaults
@view_config(attr='login', route_name='login')
@view_config(attr='logout', route_name='logout')
class AuthController(object):
def __init__(self, request):
self.request = request
self.schema = schemas.LoginSchema().bind(request=self.request)
self.form = deform.Form(self.schema)
self.login_redirect = self.request.route_url('stream')
self.logout_redirect = self.request.route_url('index')
def login(self):
if self.request.authenticated_userid is not None:
return httpexceptions.HTTPFound(location=self.login_redirect)
err, appstruct = validate_form(self.form, self.request.POST.items())
if err is not None:
return err
user = appstruct['user']
self.request.registry.notify(LoginEvent(self.request, user))
return {}
def logout(self):
self.request.registry.notify(LogoutEvent(self.request))
self.request.session.invalidate()
FlashMessage(self.request, _('You have logged out.'), kind='success')
headers = forget(self.request)
return httpexceptions.HTTPFound(location=self.logout_redirect,
headers=headers)
@view_defaults(accept='application/json', context=Application, renderer='json')
@view_config(attr='login', request_param='__formid__=login')
@view_config(attr='logout', request_param='__formid__=logout')
class AsyncAuthController(AuthController):
__view_mapper__ = AsyncFormViewMapper
@view_auth_defaults
@view_config(attr='forgot_password',
route_name='forgot_password',
request_method='POST')
@view_config(attr='forgot_password_form',
route_name='forgot_password',
request_method='GET')
@view_config(attr='reset_password',
route_name='reset_password',
request_method='POST')
@view_config(attr='reset_password_form',
route_name='reset_password',
request_method='GET')
class ForgotPasswordController(object):
"""Controller for handling password reset forms."""
def __init__(self, request):
self.request = request
self.forgot_password_redirect = self.request.route_url('index')
self.reset_password_redirect = self.request.route_url('index')
def forgot_password(self):
"""
Handle submission of the forgot password form.
Validates that the email is one we know about, and then generates a new
activation for the associated user, and dispatches a "reset your
password" email which contains a token and/or link to the reset
password form.
"""
schema = schemas.ForgotPasswordSchema().bind(request=self.request)
form = deform.Form(schema)
# Nothing to do here for logged-in users
if self.request.authenticated_userid is not None:
return httpexceptions.HTTPFound(
location=self.forgot_password_redirect)
err, appstruct = validate_form(form, self.request.POST.items())
if err is not None:
return err
# If the validation passes, we assume the user exists.
#
# TODO: fix this latent race condition by returning a user object in
# the appstruct.
user = User.get_by_email(self.request, appstruct['email'])
# Create a new activation for this user. Any previous activation will
# get overwritten.
activation = Activation()
db = get_session(self.request)
db.add(activation)
user.activation = activation
# Send the reset password email
code = user.activation.code
link = reset_password_link(self.request, code)
message = reset_password_email(user, code, link)
mailer = get_mailer(self.request)
mailer.send(message)
FlashMessage(self.request,
_("Please check your email to finish resetting your "
"password."),
kind="success")
return httpexceptions.HTTPFound(location=self.reset_password_redirect)
# FIXME: generate a form here and progressively enhance it rather than
# relying entirely on Angular.
def forgot_password_form(self):
"""Render the forgot password form."""
if self.request.authenticated_userid is not None:
return httpexceptions.HTTPFound(
location=self.forgot_password_redirect)
return {}
def reset_password(self):
"""
Handle submission of the reset password form.
This function checks that the activation code (i.e. reset token)
provided by the form is valid, retrieves the user associated with the
activation code, and resets their password.
"""
schema = schemas.ResetPasswordSchema().bind(request=self.request)
form = deform.Form(schema)
code = self.request.matchdict.get('code')
if code is None:
return httpexceptions.HTTPNotFound()
activation = Activation.get_by_code(self.request, code)
if activation is None:
return httpexceptions.HTTPNotFound()
user = User.get_by_activation(self.request, activation)
if user is None:
return httpexceptions.HTTPNotFound()
if self.request.method != 'POST':
return httpexceptions.HTTPMethodNotAllowed()
err, appstruct = validate_form(form, self.request.POST.items())
if err is not None:
return err
user.password = appstruct['password']
db = get_session(self.request)
db.delete(activation)
FlashMessage(self.request,
_('Your password has been reset!'),
kind='success')
self.request.registry.notify(PasswordResetEvent(self.request, user))
return httpexceptions.HTTPFound(location=self.reset_password_redirect)
# FIXME: generate a form here and progressively enhance it rather than
# relying entirely on Angular.
def reset_password_form(self):
"""Render the reset password form."""
return {}
@view_defaults(accept='application/json', context=Application, renderer='json')
@view_config(
attr='forgot_password',
request_param='__formid__=forgot_password'
)
@view_config(
attr='reset_password',
request_param='__formid__=reset_password'
)
class AsyncForgotPasswordController(ForgotPasswordController):
__view_mapper__ = AsyncFormViewMapper
def reset_password(self):
request = self.request
request.matchdict = request.POST
return super(AsyncForgotPasswordController, self).reset_password()
@view_auth_defaults
@view_config(attr='register', route_name='register', request_method='POST')
@view_config(attr='register_form', route_name='register', request_method='GET')
@view_config(attr='activate', route_name='activate', request_method='GET')
class RegisterController(object):
def __init__(self, request):
self.request = request
self.schema = schemas.RegisterSchema().bind(request=self.request)
self.form = deform.Form(self.schema)
def register(self):
"""
Handle submission of the new user registration form.
Validates the form data, creates a new activation for the user, sends
the activation mail, and then redirects the user to the index.
"""
err, appstruct = validate_form(self.form, self.request.POST.items())
if err is not None:
return err
db = get_session(self.request)
# Create the new user from selected form fields
props = {k: appstruct[k] for k in ['username', 'email', 'password']}
user = User(**props)
db.add(user)
# Create a new activation for the user
activation = Activation()
db.add(activation)
user.activation = activation
# Flush the session to ensure that the user can be created and the
# activation is successfully wired up
db.flush()
# Send the activation email
message = activation_email(self.request, user)
mailer = get_mailer(self.request)
mailer.send(message)
FlashMessage(self.request,
_("Thank you for registering! Please check your e-mail "
"now. You can continue by clicking the activation link "
"we have sent you."),
kind='success')
self.request.registry.notify(RegistrationEvent(self.request, user))
return httpexceptions.HTTPFound(
location=self.request.route_url('index'))
# FIXME: generate a form here and progressively enhance it rather than
# relying entirely on Angular.
def register_form(self):
"""Render the registration form."""
# Logged in users shouldn't be able to register...
if self.request.authenticated_userid is not None:
return httpexceptions.HTTPFound(self.request.route_url('stream'))
return {}
def activate(self):
"""
Handle a request for a user activation link.
Checks if the activation code passed is valid, and (as a safety check)
that it is an activation for the passed user id. If all is well,
activate the user and redirect them to the stream.
"""
code = self.request.matchdict.get('code')
id_ = self.request.matchdict.get('id')
if code is None or id_ is None:
return httpexceptions.HTTPNotFound()
try:
id_ = int(id_)
except ValueError:
return httpexceptions.HTTPNotFound()
activation = Activation.get_by_code(self.request, code)
if activation is None:
return httpexceptions.HTTPNotFound()
user = User.get_by_activation(self.request, activation)
if user is None or user.id != id_:
return httpexceptions.HTTPNotFound()
# Activate the user (by deleting the activation)
db = get_session(self.request)
db.delete(activation)
FlashMessage(self.request,
_("Your e-mail address has been verified. Thank you!"),
kind='success')
self.request.registry.notify(ActivationEvent(self.request, user))
return httpexceptions.HTTPFound(
location=self.request.route_url('index'))
@view_defaults(accept='application/json', context=Application, renderer='json')
@view_config(attr='register', request_param='__formid__=register')
@view_config(attr='activate', request_param='__formid__=activate')
class AsyncRegisterController(RegisterController):
__view_mapper__ = AsyncFormViewMapper
@view_auth_defaults
@view_config(attr='edit_profile', route_name='edit_profile')
@view_config(attr='disable_user', route_name='disable_user')
@view_config(attr='profile', route_name='profile')
class ProfileController(object):
def __init__(self, request):
self.request = request
self.schema = schemas.ProfileSchema().bind(request=self.request)
self.form = deform.Form(self.schema)
def edit_profile(self):
"""Handle POST payload from profile update form."""
if self.request.method != 'POST':
return httpexceptions.HTTPMethodNotAllowed()
# Nothing to do here for non logged-in users
if self.request.authenticated_userid is None:
return httpexceptions.HTTPUnauthorized()
err, appstruct = validate_form(self.form, self.request.POST.items())
if err is not None:
return err
user = User.get_by_id(self.request, self.request.authenticated_userid)
response = {'model': {'email': user.email}}
# We allow updating subscriptions without validating a password
subscriptions = appstruct.get('subscriptions')
if subscriptions:
data = json.loads(subscriptions)
err = _update_subscription_data(self.request, data)
if err is not None:
return err
return response
# Any updates to fields below this point require password validation.
#
# `pwd` is the current password
# `password` (used below) is optional, and is the new password
#
if not User.validate_user(user, appstruct.get('pwd')):
return {'errors': {'pwd': _('Invalid password')}, 'code': 401}
email = appstruct.get('email')
if email:
email_user = User.get_by_email(self.request, email)
if email_user:
if email_user.id != user.id:
return {
'errors': {'pwd': _('That email is already used')},
}
response['model']['email'] = user.email = email
password = appstruct.get('password')
if password:
user.password = password
return response
def disable_user(self):
"""Disable the user by setting a random password."""
err, appstruct = validate_form(self.form, self.request.POST.items())
if err is not None:
return err
username = appstruct['username']
pwd = appstruct['pwd']
# Password check
user = User.get_user(self.request, username, pwd)
if user:
# TODO: maybe have an explicit disabled flag in the status
user.password = User.generate_random_password()
FlashMessage(self.request, _('Account disabled.'), kind='success')
return {}
else:
return dict(errors={'pwd': _('Invalid password')}, code=401)
def profile(self):
"""
Return a serialisation of the user's profile.
For use by the frontend. Includes current email and subscriptions data.
"""
request = self.request
userid = request.authenticated_userid
model = {}
if userid:
model["email"] = User.get_by_id(request, userid).email
if request.registry.feature('notification'):
model['subscriptions'] = Subscriptions.get_subscriptions_for_uri(
request,
userid
)
return {'model': model}
def unsubscribe(self):
request = self.request
subscription_id = request.GET['subscription_id']
subscription = Subscriptions.get_by_id(request, subscription_id)
if subscription:
subscription.active = False
return {}
return {}
@view_defaults(accept='application/json', context=Application, renderer='json')
@view_config(attr='edit_profile', request_param='__formid__=edit_profile')
@view_config(attr='disable_user', request_param='__formid__=disable_user')
@view_config(attr='profile', request_param='__formid__=profile')
@view_config(attr='unsubscribe', request_param='__formid__=unsubscribe')
class AsyncProfileController(ProfileController):
__view_mapper__ = AsyncFormViewMapper
def activation_email(request, user):
"""
Generate an 'activate your account' email for the specified user.
:rtype: pyramid_mailer.message.Message
"""
link = request.route_url('activate', id=user.id, code=user.activation.code)
emailtext = ("Please validate your email and activate your account by "
"visiting: {link}")
body = emailtext.format(link=link)
msg = Message(subject=_("Please activate your account"),
recipients=[user.email],
body=body)
return msg
def reset_password_email(user, reset_code, reset_link):
"""
Generate a 'reset your password' email for the specified user.
:rtype: pyramid_mailer.message.Message
"""
emailtext = ("Hello, {username}!\n\n"
"Someone requested resetting your password. If it was "
"you, reset your password by using this reset code:\n\n"
"{code}\n\n"
"Alternatively, you can reset your password by "
"clicking on this link:\n\n"
"{link}\n\n"
"If you don't want to change your password, please "
"ignore this email message.\n\n"
"Regards,\n"
"The Hypothesis Team\n")
body = emailtext.format(code=reset_code,
link=reset_link,
username=user.username)
msg = Message(subject=_("Reset your password"),
recipients=[user.email],
body=body)
return msg
def reset_password_link(request, reset_code):
"""Transform an activation code into a password reset link."""
return request.route_url('reset_password', code=reset_code)
def _update_subscription_data(request, subscription):
"""
Update the subscriptions in the database from form data.
Using data from the passed subscription struct, find a subscription in the
database, and update it (if it belongs to the current logged-in user).
"""
sub = Subscriptions.get_by_id(request, subscription['id'])
if sub is None:
return {
'errors': {'subscriptions': _('Subscription not found')},
}
# If we're trying to update a subscription for anyone other than
# the currently logged-in user, bail fast.
#
# The error message is deliberately identical to the one above, so
# as not to leak any information about who which subscription ids
# belong to.
if sub.uri != request.authenticated_userid:
return {
'errors': {'subscriptions': _('Subscription not found')},
}
sub.active = subscription.get('active', True)
FlashMessage(request, _('Changes saved!'), kind='success')
def includeme(config):
config.add_route('disable_user', '/disable/{userid}',
factory=UserFactory,
traverse="/{userid}")
config.add_route('register', '/register')
config.add_route('activate', '/activate/{id}/{code}')
config.add_route('forgot_password', '/forgot_password')
config.add_route('reset_password', '/reset_password/{code}')
config.include('horus')
config.scan(__name__)