69
54
user['local_password'] = self.context.passhash is not None
72
class UserEditSchema(formencode.Schema):
73
nick = formencode.validators.UnicodeString(not_empty=True)
74
email = formencode.validators.Email(not_empty=False,
77
class UserEditView(XHTMLView):
78
"""A form to change a user's details."""
79
template = 'templates/user-edit.html'
83
def filter(self, stream, ctx):
84
return stream | HTMLFormFiller(data=ctx['data'])
86
def populate(self, req, ctx):
87
if req.method == 'POST':
88
data = dict(req.get_fieldstorage())
90
validator = UserEditSchema()
91
data = validator.to_python(data, state=req)
92
self.context.nick = data['nick']
93
self.context.email = unicode(data['email']) if data['email'] \
96
req.throw_redirect(req.uri)
97
except formencode.Invalid, e:
98
errors = e.unpack_errors()
100
data = {'nick': self.context.nick,
101
'email': self.context.email
105
ctx['format_datetime'] = ivle.date.make_date_nice
106
ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
109
ctx['user'] = self.context
111
ctx['errors'] = errors
113
class UserAdminSchema(formencode.Schema):
114
admin = formencode.validators.StringBoolean(if_missing=False)
115
fullname = formencode.validators.UnicodeString(not_empty=True)
116
studentid = formencode.validators.UnicodeString(not_empty=False,
120
class UserAdminView(XHTMLView):
121
"""A form for admins to change more of a user's details."""
122
template = 'templates/user-admin.html'
125
def authorize(self, req):
126
"""Only allow access if the requesting user is an admin."""
127
return req.user.admin
129
def filter(self, stream, ctx):
130
return stream | HTMLFormFiller(data=ctx['data'])
132
def populate(self, req, ctx):
133
if req.method == 'POST':
134
data = dict(req.get_fieldstorage())
136
validator = UserAdminSchema()
137
data = validator.to_python(data, state=req)
139
self.context.admin = data['admin']
140
self.context.fullname = data['fullname'] \
141
if data['fullname'] else None
142
self.context.studentid = data['studentid'] \
143
if data['studentid'] else None
145
req.throw_redirect(req.uri)
146
except formencode.Invalid, e:
147
errors = e.unpack_errors()
149
data = {'admin': self.context.admin,
150
'fullname': self.context.fullname,
151
'studentid': self.context.studentid,
156
ctx['user'] = self.context
158
ctx['errors'] = errors
160
class PasswordChangeView(XHTMLView):
161
"""A form to change a user's password, with knowledge of the old one."""
162
template = 'templates/user-password-change.html'
166
def authorize(self, req):
167
"""Only allow access if the requesting user holds the permission,
168
and the target user has a password set. Otherwise we might be
169
clobbering external authn.
171
return super(PasswordChangeView, self).authorize(req) and \
172
self.context.passhash is not None
174
def populate(self, req, ctx):
176
if req.method == 'POST':
177
data = dict(req.get_fieldstorage())
178
if data.get('old_password') is None or \
179
not self.context.authenticate(data.get('old_password')):
180
error = 'Incorrect password.'
181
elif data.get('new_password') != data.get('new_password_again'):
182
error = 'New passwords do not match.'
183
elif not data.get('new_password'):
184
error = 'New password cannot be empty.'
186
self.context.password = data['new_password']
188
req.throw_redirect(req.uri)
191
ctx['user'] = self.context
194
class PasswordResetView(XHTMLView):
195
"""A form to reset a user's password, without knowledge of the old one."""
196
template = 'templates/user-password-reset.html'
199
def authorize(self, req):
200
"""Only allow access if the requesting user is an admin."""
201
return req.user.admin
203
def populate(self, req, ctx):
205
if req.method == 'POST':
206
data = dict(req.get_fieldstorage())
207
if data.get('new_password') != data.get('new_password_again'):
208
error = 'New passwords do not match.'
209
elif not data.get('new_password'):
210
error = 'New password cannot be empty.'
212
self.context.password = data['new_password']
214
req.throw_redirect(req.uri)
216
ctx['user'] = self.context
57
class UserSettingsView(XHTMLView):
58
template = '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
req.scripts_init = ['revert_settings']
71
ctx['login'] = self.context.login
219
73
class Plugin(ViewPlugin, MediaPlugin):
221
75
The Plugin class for the user plugin.
224
forward_routes = (root_to_user,)
225
reverse_routes = (user_url,)
226
views = [(ApplicationRoot, 'users', UsersView),
227
(ivle.database.User, '+index', UserEditView),
228
(ivle.database.User, '+admin', UserAdminView),
229
(ivle.database.User, '+changepassword', PasswordChangeView),
230
(ivle.database.User, '+resetpassword', PasswordResetView),
231
(ivle.database.User, '+index', UserRESTView, 'api'),
234
public_forward_routes = forward_routes
235
public_reverse_routes = reverse_routes
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),
237
86
media = 'user-media'