71
54
user['local_password'] = self.context.passhash is not None
74
class UserEditSchema(formencode.Schema):
75
nick = formencode.validators.UnicodeString(not_empty=True)
76
email = formencode.validators.Email(not_empty=False,
79
class UserEditView(XHTMLView):
80
"""A form to change a user's details."""
81
template = 'templates/user-edit.html'
85
def filter(self, stream, ctx):
86
return stream | HTMLFormFiller(data=ctx['data'])
88
def populate(self, req, ctx):
89
if req.method == 'POST':
90
data = dict(req.get_fieldstorage())
92
validator = UserEditSchema()
93
data = validator.to_python(data, state=req)
94
self.context.nick = data['nick']
95
self.context.email = unicode(data['email']) if data['email'] \
98
req.throw_redirect(req.uri)
99
except formencode.Invalid, e:
100
errors = e.unpack_errors()
102
data = {'nick': self.context.nick,
103
'email': self.context.email
107
ctx['format_datetime'] = ivle.date.make_date_nice
108
ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
111
ctx['user'] = self.context
113
ctx['errors'] = errors
115
class UserAdminSchema(formencode.Schema):
116
admin = formencode.validators.StringBoolean(if_missing=False)
117
disabled = formencode.validators.StringBoolean(if_missing=False)
118
fullname = formencode.validators.UnicodeString(not_empty=True)
119
studentid = formencode.validators.UnicodeString(not_empty=False,
123
class UserAdminView(XHTMLView):
124
"""A form for admins to change more of a user's details."""
125
template = 'templates/user-admin.html'
128
def authorize(self, req):
129
"""Only allow access if the requesting user is an admin."""
130
return req.user and req.user.admin
132
def filter(self, stream, ctx):
133
return stream | HTMLFormFiller(data=ctx['data'])
135
def populate(self, req, ctx):
136
if req.method == 'POST':
137
data = dict(req.get_fieldstorage())
139
validator = UserAdminSchema()
140
data = validator.to_python(data, state=req)
142
if self.context is req.user:
143
# Admin checkbox is disabled -- assume unchanged
144
data['admin'] = self.context.admin
145
data['disabled'] = self.context.state == u'disabled'
147
self.context.admin = data['admin']
148
if self.context.state in (u'enabled', u'disabled'):
149
self.context.state = (u'disabled' if data['disabled']
151
self.context.fullname = data['fullname'] \
152
if data['fullname'] else None
153
self.context.studentid = data['studentid'] \
154
if data['studentid'] else None
156
req.throw_redirect(req.uri)
157
except formencode.Invalid, e:
158
errors = e.unpack_errors()
160
data = {'admin': self.context.admin,
161
'disabled': self.context.state == u'disabled',
162
'fullname': self.context.fullname,
163
'studentid': self.context.studentid,
168
ctx['user'] = self.context
169
# Disable the Admin checkbox if editing oneself
170
ctx['disable_admin'] = self.context is req.user
172
ctx['errors'] = errors
174
class PasswordChangeView(XHTMLView):
175
"""A form to change a user's password, with knowledge of the old one."""
176
template = 'templates/user-password-change.html'
180
def authorize(self, req):
181
"""Only allow access if the requesting user holds the permission,
182
and the target user has a password set. Otherwise we might be
183
clobbering external authn.
185
return super(PasswordChangeView, self).authorize(req) and \
186
self.context.passhash is not None
188
def populate(self, req, ctx):
190
if req.method == 'POST':
191
data = dict(req.get_fieldstorage())
192
if data.get('old_password') is None or \
193
not self.context.authenticate(data.get('old_password')):
194
error = 'Incorrect password.'
195
elif data.get('new_password') != data.get('new_password_again'):
196
error = 'New passwords do not match.'
197
elif not data.get('new_password'):
198
error = 'New password cannot be empty.'
200
self.context.password = data['new_password']
202
req.throw_redirect(req.uri)
205
ctx['user'] = self.context
208
class PasswordResetView(XHTMLView):
209
"""A form to reset a user's password, without knowledge of the old one."""
210
template = 'templates/user-password-reset.html'
213
def authorize(self, req):
214
"""Only allow access if the requesting user is an admin."""
215
return req.user and req.user.admin
217
def populate(self, req, ctx):
219
if req.method == 'POST':
220
data = dict(req.get_fieldstorage())
221
if data.get('new_password') != data.get('new_password_again'):
222
error = 'New passwords do not match.'
223
elif not data.get('new_password'):
224
error = 'New password cannot be empty.'
226
self.context.password = data['new_password']
228
req.throw_redirect(req.uri)
230
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
233
73
class Plugin(ViewPlugin, MediaPlugin):
235
75
The Plugin class for the user plugin.
238
forward_routes = (root_to_user,)
239
reverse_routes = (user_url,)
240
views = [(ApplicationRoot, 'users', UsersView),
241
(ivle.database.User, '+index', UserEditView),
242
(ivle.database.User, '+admin', UserAdminView),
243
(ivle.database.User, '+changepassword', PasswordChangeView),
244
(ivle.database.User, '+resetpassword', PasswordResetView),
245
(ivle.database.User, '+index', UserRESTView, 'api'),
249
('users', 'Users', 'Display and edit all users',
250
'users.png', 'users', 90, True)
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),
253
public_forward_routes = forward_routes
254
public_reverse_routes = reverse_routes
256
86
media = 'user-media'