-
Notifications
You must be signed in to change notification settings - Fork 2k
/
user.py
615 lines (516 loc) · 23.1 KB
/
user.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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
import logging
from urllib import quote
from urlparse import urlparse
from pylons import session, c, g, request, config
from pylons.i18n import _
import ckan.lib.i18n as i18n
import ckan.lib.base as base
import ckan.model as model
import ckan.lib.helpers as h
import ckan.new_authz as new_authz
import ckan.logic as logic
import ckan.logic.schema as schema
import ckan.lib.captcha as captcha
import ckan.lib.mailer as mailer
import ckan.lib.navl.dictization_functions as dictization_functions
log = logging.getLogger(__name__)
abort = base.abort
render = base.render
validate = base.validate
check_access = logic.check_access
get_action = logic.get_action
NotFound = logic.NotFound
NotAuthorized = logic.NotAuthorized
ValidationError = logic.ValidationError
DataError = dictization_functions.DataError
unflatten = dictization_functions.unflatten
class UserController(base.BaseController):
def __before__(self, action, **env):
base.BaseController.__before__(self, action, **env)
try:
context = {'model': model, 'user': c.user or c.author}
check_access('site_read', context)
except NotAuthorized:
if c.action not in ('login', 'request_reset', 'perform_reset',):
abort(401, _('Not authorized to see this page'))
## hooks for subclasses
new_user_form = 'user/new_user_form.html'
edit_user_form = 'user/edit_user_form.html'
def _new_form_to_db_schema(self):
return schema.user_new_form_schema()
def _db_to_new_form_schema(self):
'''This is an interface to manipulate data from the database
into a format suitable for the form (optional)'''
def _edit_form_to_db_schema(self):
return schema.user_edit_form_schema()
def _db_to_edit_form_schema(self):
'''This is an interface to manipulate data from the database
into a format suitable for the form (optional)'''
def _setup_template_variables(self, context, data_dict):
c.is_sysadmin = new_authz.is_sysadmin(c.user)
try:
user_dict = get_action('user_show')(context, data_dict)
except NotFound:
abort(404, _('User not found'))
except NotAuthorized:
abort(401, _('Not authorized to see this page'))
c.user_dict = user_dict
c.is_myself = user_dict['name'] == c.user
c.about_formatted = h.render_markdown(user_dict['about'])
## end hooks
def _get_repoze_handler(self, handler_name):
'''Returns the URL that repoze.who will respond to and perform a
login or logout.'''
return getattr(request.environ['repoze.who.plugins']['friendlyform'],
handler_name)
def index(self):
LIMIT = 20
page = int(request.params.get('page', 1))
c.q = request.params.get('q', '')
c.order_by = request.params.get('order_by', 'name')
context = {'return_query': True}
data_dict = {'q': c.q,
'order_by': c.order_by}
try:
check_access('user_list', context, data_dict)
except NotAuthorized:
abort(401, _('Not authorized to see this page'))
users_list = get_action('user_list')(context, data_dict)
c.page = h.Page(
collection=users_list,
page=page,
url=h.pager_url,
item_count=users_list.count(),
items_per_page=LIMIT
)
return render('user/list.html')
def read(self, id=None):
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'for_view': True}
data_dict = {'id': id,
'user_obj': c.userobj}
context['with_related'] = True
self._setup_template_variables(context, data_dict)
# The legacy templates have the user's activity stream on the user
# profile page, new templates do not.
if h.asbool(config.get('ckan.legacy_templates', False)):
c.user_activity_stream = get_action('user_activity_list_html')(
context, {'id': c.user_dict['id']})
return render('user/read.html')
def me(self, locale=None):
if not c.user:
h.redirect_to(locale=locale, controller='user', action='login',
id=None)
user_ref = c.userobj.get_reference_preferred_for_uri()
h.redirect_to(locale=locale, controller='user', action='dashboard',
id=user_ref)
def register(self, data=None, errors=None, error_summary=None):
return self.new(data, errors, error_summary)
def new(self, data=None, errors=None, error_summary=None):
'''GET to display a form for registering a new user.
or POST the form data to actually do the user registration.
'''
context = {'model': model, 'session': model.Session,
'user': c.user or c.author,
'schema': self._new_form_to_db_schema(),
'save': 'save' in request.params}
try:
check_access('user_create', context)
except NotAuthorized:
abort(401, _('Unauthorized to create a user'))
if context['save'] and not data:
return self._save_new(context)
if c.user and not data:
# #1799 Don't offer the registration form if already logged in
return render('user/logout_first.html')
data = data or {}
errors = errors or {}
error_summary = error_summary or {}
vars = {'data': data, 'errors': errors, 'error_summary': error_summary}
c.is_sysadmin = new_authz.is_sysadmin(c.user)
c.form = render(self.new_user_form, extra_vars=vars)
return render('user/new.html')
def _save_new(self, context):
try:
data_dict = logic.clean_dict(unflatten(
logic.tuplize_dict(logic.parse_params(request.params))))
context['message'] = data_dict.get('log_message', '')
captcha.check_recaptcha(request)
user = get_action('user_create')(context, data_dict)
except NotAuthorized:
abort(401, _('Unauthorized to create user %s') % '')
except NotFound, e:
abort(404, _('User not found'))
except DataError:
abort(400, _(u'Integrity Error'))
except captcha.CaptchaError:
error_msg = _(u'Bad Captcha. Please try again.')
h.flash_error(error_msg)
return self.new(data_dict)
except ValidationError, e:
errors = e.error_dict
error_summary = e.error_summary
return self.new(data_dict, errors, error_summary)
if not c.user:
# Redirect to a URL picked up by repoze.who which performs the
# login
login_url = self._get_repoze_handler('login_handler_path')
h.redirect_to('%s?login=%s&password=%s' % (
login_url,
str(data_dict['name']),
quote(data_dict['password1'].encode('utf-8'))))
else:
# #1799 User has managed to register whilst logged in - warn user
# they are not re-logged in as new user.
h.flash_success(_('User "%s" is now registered but you are still '
'logged in as "%s" from before') %
(data_dict['name'], c.user))
return render('user/logout_first.html')
def edit(self, id=None, data=None, errors=None, error_summary=None):
context = {'save': 'save' in request.params,
'schema': self._edit_form_to_db_schema(),
}
if id is None:
if c.userobj:
id = c.userobj.id
else:
abort(400, _('No user specified'))
data_dict = {'id': id}
if (context['save']) and not data:
return self._save_edit(id, context)
try:
old_data = get_action('user_show')(context, data_dict)
schema = self._db_to_edit_form_schema()
if schema:
old_data, errors = validate(old_data, schema)
c.display_name = old_data.get('display_name')
c.user_name = old_data.get('name')
data = data or old_data
except NotAuthorized:
abort(401, _('Unauthorized to edit user %s') % '')
except NotFound, e:
abort(404, _('User not found'))
user_obj = context.get('user_obj')
if not (new_authz.is_sysadmin(c.user)
or c.user == user_obj.name):
abort(401, _('User %s not authorized to edit %s') %
(str(c.user), id))
errors = errors or {}
vars = {'data': data, 'errors': errors, 'error_summary': error_summary}
self._setup_template_variables({'model': model,
'session': model.Session,
'user': c.user or c.author},
data_dict)
c.is_myself = True
c.show_email_notifications = h.asbool(
config.get('ckan.activity_streams_email_notifications'))
c.form = render(self.edit_user_form, extra_vars=vars)
return render('user/edit.html')
def _save_edit(self, id, context):
try:
data_dict = logic.clean_dict(unflatten(
logic.tuplize_dict(logic.parse_params(request.params))))
context['message'] = data_dict.get('log_message', '')
data_dict['id'] = id
# MOAN: Do I really have to do this here?
if 'activity_streams_email_notifications' not in data_dict:
data_dict['activity_streams_email_notifications'] = False
user = get_action('user_update')(context, data_dict)
h.flash_success(_('Profile updated'))
h.redirect_to(controller='user', action='read', id=user['name'])
except NotAuthorized:
abort(401, _('Unauthorized to edit user %s') % id)
except NotFound, e:
abort(404, _('User not found'))
except DataError:
abort(400, _(u'Integrity Error'))
except ValidationError, e:
errors = e.error_dict
error_summary = e.error_summary
return self.edit(id, data_dict, errors, error_summary)
def login(self, error=None):
if 'error' in request.params:
h.flash_error(request.params['error'])
if request.environ['SCRIPT_NAME'] and g.openid_enabled:
# #1662 restriction
log.warn('Cannot mount CKAN at a URL and login with OpenID.')
g.openid_enabled = False
if not c.user:
came_from = request.params.get('came_from')
if not came_from:
came_from = h.url_for(controller='user', action='logged_in',
__ckan_no_root=True)
c.login_handler = h.url_for(
self._get_repoze_handler('login_handler_path'),
came_from=came_from)
if error:
vars = {'error_summary': {'': error}}
else:
vars = {}
return render('user/login.html', extra_vars=vars)
else:
return render('user/logout_first.html')
def logged_in(self):
# redirect if needed
came_from = request.params.get('came_from', '')
if self._sane_came_from(came_from):
return h.redirect_to(str(came_from))
if c.user:
context = None
data_dict = {'id': c.user}
user_dict = get_action('user_show')(context, data_dict)
h.flash_success(_("%s is now logged in") %
user_dict['display_name'])
return self.me()
else:
err = _('Login failed. Bad username or password.')
if g.openid_enabled:
err += _(' (Or if using OpenID, it hasn\'t been associated '
'with a user account.)')
if h.asbool(config.get('ckan.legacy_templates', 'false')):
h.flash_error(err)
h.redirect_to(controller='user',
action='login', came_from=came_from)
else:
return self.login(error=err)
def logout(self):
url = h.url_for(controller='user', action='logged_out_page',
__ckan_no_root=True)
h.redirect_to(self._get_repoze_handler('logout_handler_path') +
'?came_from=' + url)
def logged_out(self):
# redirect if needed
came_from = request.params.get('came_from', '')
if self._sane_came_from(came_from):
return h.redirect_to(str(came_from))
h.redirect_to(controller='user', action='logged_out_page')
def logged_out_page(self):
return render('user/logout.html')
def request_reset(self):
if request.method == 'POST':
id = request.params.get('user')
context = {'model': model,
'user': c.user}
data_dict = {'id': id}
user_obj = None
try:
user_dict = get_action('user_show')(context, data_dict)
user_obj = context['user_obj']
except NotFound:
# Try searching the user
del data_dict['id']
data_dict['q'] = id
if id and len(id) > 2:
user_list = get_action('user_list')(context, data_dict)
if len(user_list) == 1:
# This is ugly, but we need the user object for the
# mailer,
# and user_list does not return them
del data_dict['q']
data_dict['id'] = user_list[0]['id']
user_dict = get_action('user_show')(context, data_dict)
user_obj = context['user_obj']
elif len(user_list) > 1:
h.flash_error(_('"%s" matched several users') % (id))
else:
h.flash_error(_('No such user: %s') % id)
else:
h.flash_error(_('No such user: %s') % id)
if user_obj:
try:
mailer.send_reset_link(user_obj)
h.flash_success(_('Please check your inbox for '
'a reset code.'))
h.redirect_to('/')
except mailer.MailerException, e:
h.flash_error(_('Could not send reset link: %s') %
unicode(e))
return render('user/request_reset.html')
def perform_reset(self, id):
# FIXME 403 error for invalid key is a non helpful page
context = {'model': model, 'session': model.Session,
'user': c.user,
'keep_sensitive_data': True}
data_dict = {'id': id}
try:
user_dict = get_action('user_show')(context, data_dict)
# Be a little paranoid, and get rid of sensitive data that's
# not needed.
user_dict.pop('apikey', None)
user_dict.pop('reset_key', None)
user_obj = context['user_obj']
except NotFound, e:
abort(404, _('User not found'))
c.reset_key = request.params.get('key')
if not mailer.verify_reset_link(user_obj, c.reset_key):
h.flash_error(_('Invalid reset key. Please try again.'))
abort(403)
if request.method == 'POST':
try:
context['reset_password'] = True
new_password = self._get_form_password()
user_dict['password'] = new_password
user_dict['reset_key'] = c.reset_key
user = get_action('user_update')(context, user_dict)
mailer.create_reset_key(user_obj)
h.flash_success(_("Your password has been reset."))
h.redirect_to('/')
except NotAuthorized:
h.flash_error(_('Unauthorized to edit user %s') % id)
except NotFound, e:
h.flash_error(_('User not found'))
except DataError:
h.flash_error(_(u'Integrity Error'))
except ValidationError, e:
h.flash_error(u'%r' % e.error_dict)
except ValueError, ve:
h.flash_error(unicode(ve))
c.user_dict = user_dict
return render('user/perform_reset.html')
def _get_form_password(self):
password1 = request.params.getone('password1')
password2 = request.params.getone('password2')
if (password1 is not None and password1 != ''):
if not len(password1) >= 4:
raise ValueError(_('Your password must be 4 '
'characters or longer.'))
elif not password1 == password2:
raise ValueError(_('The passwords you entered'
' do not match.'))
return password1
raise ValueError(_('You must provide a password'))
def followers(self, id=None):
context = {'for_view': True}
data_dict = {'id': id, 'user_obj': c.userobj}
self._setup_template_variables(context, data_dict)
f = get_action('user_follower_list')
try:
c.followers = f(context, {'id': c.user_dict['id']})
except NotAuthorized:
abort(401, _('Unauthorized to view followers %s') % '')
return render('user/followers.html')
def activity(self, id, offset=0):
'''Render this user's public activity stream page.'''
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'for_view': True}
data_dict = {'id': id, 'user_obj': c.userobj}
try:
check_access('user_show', context, data_dict)
except NotAuthorized:
abort(401, _('Not authorized to see this page'))
self._setup_template_variables(context, data_dict)
c.user_activity_stream = get_action('user_activity_list_html')(
context, {'id': c.user_dict['id'], 'offset': offset})
return render('user/activity_stream.html')
def _get_dashboard_context(self, filter_type=None, filter_id=None,
q=None):
'''Return a dict needed by the dashboard view to determine context.'''
def display_name(followee):
'''Return a display name for a user, group or dataset dict.'''
display_name = followee.get('display_name')
fullname = followee.get('fullname')
title = followee.get('title')
name = followee.get('name')
return display_name or fullname or title or name
if (filter_type and filter_id):
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'for_view': True}
data_dict = {'id': filter_id}
followee = None
action_functions = {
'dataset': 'package_show',
'user': 'user_show',
'group': 'group_show'
}
action_function = logic.get_action(action_functions.get(filter_type))
# Is this a valid type?
if action_function is None:
raise abort(404, _('Follow item not found'))
try:
followee = action_function(context, data_dict)
except NotFound:
abort(404, _('{0} not found').format(filter_type))
except NotAuthorized:
abort(401, _('Unauthorized to read {0} {1}').format(
filter_type, id))
if followee is not None:
return {
'filter_type': filter_type,
'q': q,
'context': display_name(followee),
'selected_id': followee.get('id'),
'dict': followee,
}
return {
'filter_type': filter_type,
'q': q,
'context': _('Everything'),
'selected_id': False,
'dict': None,
}
def dashboard(self, id=None, offset=0):
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'for_view': True}
data_dict = {'id': id, 'user_obj': c.userobj, 'offset': offset}
self._setup_template_variables(context, data_dict)
q = request.params.get('q', u'')
filter_type = request.params.get('type', u'')
filter_id = request.params.get('name', u'')
c.followee_list = get_action('followee_list')(
context, {'id': c.userobj.id, 'q': q})
c.dashboard_activity_stream_context = self._get_dashboard_context(
filter_type, filter_id, q)
c.dashboard_activity_stream = h.dashboard_activity_stream(
c.userobj.id, filter_type, filter_id, offset
)
# Mark the user's new activities as old whenever they view their
# dashboard page.
get_action('dashboard_mark_activities_old')(context, {})
return render('user/dashboard.html')
def follow(self, id):
'''Start following this user.'''
context = {'model': model,
'session': model.Session,
'user': c.user or c.author}
data_dict = {'id': id}
try:
get_action('follow_user')(context, data_dict)
user_dict = get_action('user_show')(context, data_dict)
h.flash_success(_("You are now following {0}").format(
user_dict['display_name']))
except ValidationError as e:
error_message = (e.extra_msg or e.message or e.error_summary
or e.error_dict)
h.flash_error(error_message)
except NotAuthorized as e:
h.flash_error(e.extra_msg)
h.redirect_to(controller='user', action='read', id=id)
def unfollow(self, id):
'''Stop following this user.'''
context = {'model': model,
'session': model.Session,
'user': c.user or c.author}
data_dict = {'id': id}
try:
get_action('unfollow_user')(context, data_dict)
user_dict = get_action('user_show')(context, data_dict)
h.flash_success(_("You are no longer following {0}").format(
user_dict['display_name']))
except (NotFound, NotAuthorized) as e:
error_message = e.extra_msg or e.message
h.flash_error(error_message)
except ValidationError as e:
error_message = (e.error_summary or e.message or e.extra_msg
or e.error_dict)
h.flash_error(error_message)
h.redirect_to(controller='user', action='read', id=id)
def _sane_came_from(self, url):
'''Returns True if came_from is local'''
if not url or (len(url) >= 2 and url.startswith('//')):
return False
parsed = urlparse(url)
if parsed.scheme:
domain = urlparse(h.url_for('/', qualified=True)).netloc
if domain != parsed.netloc:
return False
return True