54
70
user['local_password'] = self.context.passhash is not None
57
class UserSettingsView(XHTMLView):
58
template = 'templates/user-settings.html'
62
def __init__(self, req, login):
63
self.context = ivle.database.User.get_by_login(req.store, login)
64
if self.context is None:
67
def populate(self, req, ctx):
68
self.plugin_scripts[Plugin] = ['settings.js']
69
self.scripts_init = ['revert_settings']
71
ctx['login'] = self.context.login
73
class UserEditSchema(formencode.Schema):
74
nick = formencode.validators.UnicodeString(not_empty=True)
75
email = formencode.validators.Email(not_empty=False,
78
class UserEditView(XHTMLView):
79
"""A form to change a user's details."""
80
template = 'templates/user-edit.html'
84
def filter(self, stream, ctx):
85
return stream | HTMLFormFiller(data=ctx['data'])
87
def populate(self, req, ctx):
88
if req.method == 'POST':
89
data = dict(req.get_fieldstorage())
91
validator = UserEditSchema()
92
data = validator.to_python(data, state=req)
93
self.context.nick = data['nick']
94
self.context.email = unicode(data['email']) if data['email'] \
97
req.throw_redirect(req.uri)
98
except formencode.Invalid, e:
99
errors = e.unpack_errors()
101
data = {'nick': self.context.nick,
102
'email': self.context.email
106
ctx['format_datetime'] = ivle.date.make_date_nice
107
ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
110
ctx['user'] = self.context
112
ctx['errors'] = errors
114
class UserAdminSchema(formencode.Schema):
115
admin = formencode.validators.StringBoolean(if_missing=False)
116
fullname = formencode.validators.UnicodeString(not_empty=True)
117
studentid = formencode.validators.UnicodeString(not_empty=False,
121
class UserAdminView(XHTMLView):
122
"""A form for admins to change more of a user's details."""
123
template = 'templates/user-admin.html'
126
def authorize(self, req):
127
"""Only allow access if the requesting user is an admin."""
128
return req.user.admin
130
def filter(self, stream, ctx):
131
return stream | HTMLFormFiller(data=ctx['data'])
133
def populate(self, req, ctx):
134
if req.method == 'POST':
135
data = dict(req.get_fieldstorage())
137
validator = UserAdminSchema()
138
data = validator.to_python(data, state=req)
140
self.context.admin = data['admin']
141
self.context.fullname = data['fullname'] \
142
if data['fullname'] else None
143
self.context.studentid = data['studentid'] \
144
if data['studentid'] else None
146
req.throw_redirect(req.uri)
147
except formencode.Invalid, e:
148
errors = e.unpack_errors()
150
data = {'admin': self.context.admin,
151
'fullname': self.context.fullname,
152
'studentid': self.context.studentid,
157
ctx['user'] = self.context
159
ctx['errors'] = errors
161
class PasswordChangeView(XHTMLView):
162
"""A form to change a user's password, with knowledge of the old one."""
163
template = 'templates/user-password-change.html'
167
def authorize(self, req):
168
"""Only allow access if the requesting user holds the permission,
169
and the target user has a password set. Otherwise we might be
170
clobbering external authn.
172
return super(PasswordChangeView, self).authorize(req) and \
173
self.context.passhash is not None
175
def populate(self, req, ctx):
177
if req.method == 'POST':
178
data = dict(req.get_fieldstorage())
179
if data.get('old_password') is None or \
180
not self.context.authenticate(data.get('old_password')):
181
error = 'Incorrect password.'
182
elif data.get('new_password') != data.get('new_password_again'):
183
error = 'New passwords do not match.'
184
elif not data.get('new_password'):
185
error = 'New password cannot be empty.'
187
self.context.password = data['new_password']
189
req.throw_redirect(req.uri)
192
ctx['user'] = self.context
195
class PasswordResetView(XHTMLView):
196
"""A form to reset a user's password, without knowledge of the old one."""
197
template = 'templates/user-password-reset.html'
200
def authorize(self, req):
201
"""Only allow access if the requesting user is an admin."""
202
return req.user.admin
204
def populate(self, req, ctx):
206
if req.method == 'POST':
207
data = dict(req.get_fieldstorage())
208
if data.get('new_password') != data.get('new_password_again'):
209
error = 'New passwords do not match.'
210
elif not data.get('new_password'):
211
error = 'New password cannot be empty.'
213
self.context.password = data['new_password']
215
req.throw_redirect(req.uri)
217
ctx['user'] = self.context
73
220
class Plugin(ViewPlugin, MediaPlugin):
75
222
The Plugin class for the user plugin.
77
# Magic attribute: urls
78
# Sequence of pairs/triples of
79
# (regex str, handler class, kwargs dict)
80
# The kwargs dict is passed to the __init__ of the view object
82
('~:login/+settings', UserSettingsView),
83
('api/~:login', UserRESTView),
225
forward_routes = (root_to_user,)
226
reverse_routes = (user_url,)
227
views = [(ApplicationRoot, 'users', UsersView),
228
(ivle.database.User, '+index', UserEditView),
229
(ivle.database.User, '+admin', UserAdminView),
230
(ivle.database.User, '+changepassword', PasswordChangeView),
231
(ivle.database.User, '+resetpassword', PasswordResetView),
232
(ivle.database.User, '+index', UserRESTView, 'api'),
235
public_forward_routes = forward_routes
236
public_reverse_routes = reverse_routes
86
238
media = 'user-media'