1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2009 The University of Melbourne
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
# Author: Matt Giuca, Will Grant
21
import formencode.validators
22
from genshi.filters import HTMLFormFiller
24
from ivle.webapp import ApplicationRoot
25
from ivle.webapp.base.rest import JSONRESTView, require_permission
26
from ivle.webapp.base.xhtml import XHTMLView
27
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
28
from ivle.webapp.admin.publishing import root_to_user, user_url
29
from ivle.database import User
35
class UsersView(XHTMLView):
36
"""A list of all IVLE users."""
37
template = 'templates/users.html'
39
breadcrumb_text = 'Users'
41
def authorize(self, req):
44
def populate(self, req, ctx):
46
ctx['users'] = req.store.find(User).order_by(User.login)
49
# List of fields returned as part of the user JSON dictionary
50
# (as returned by the get_user action)
52
"login", "state", "unixid", "email", "nick", "fullname",
53
"admin", "studentid", "acct_exp", "pass_exp", "last_login",
57
class UserRESTView(JSONRESTView):
59
A REST interface to the user object.
62
@require_permission('view')
65
user = ivle.util.object_to_dict(user_fields_list, self.context)
66
# Convert time stamps to nice strings
67
for k in 'pass_exp', 'acct_exp', 'last_login':
68
if user[k] is not None:
69
user[k] = unicode(user[k])
71
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
fullname = formencode.validators.UnicodeString(not_empty=True)
118
studentid = formencode.validators.UnicodeString(not_empty=False,
122
class UserAdminView(XHTMLView):
123
"""A form for admins to change more of a user's details."""
124
template = 'templates/user-admin.html'
127
def authorize(self, req):
128
"""Only allow access if the requesting user is an admin."""
129
return req.user.admin
131
def filter(self, stream, ctx):
132
return stream | HTMLFormFiller(data=ctx['data'])
134
def populate(self, req, ctx):
135
if req.method == 'POST':
136
data = dict(req.get_fieldstorage())
138
validator = UserAdminSchema()
139
data = validator.to_python(data, state=req)
141
if self.context is req.user:
142
# Admin checkbox is disabled -- assume unchanged
143
data['admin'] = self.context.admin
145
self.context.admin = data['admin']
146
self.context.fullname = data['fullname'] \
147
if data['fullname'] else None
148
self.context.studentid = data['studentid'] \
149
if data['studentid'] else None
151
req.throw_redirect(req.uri)
152
except formencode.Invalid, e:
153
errors = e.unpack_errors()
155
data = {'admin': self.context.admin,
156
'fullname': self.context.fullname,
157
'studentid': self.context.studentid,
162
ctx['user'] = self.context
163
# Disable the Admin checkbox if editing oneself
164
ctx['disable_admin'] = self.context is req.user
166
ctx['errors'] = errors
168
class PasswordChangeView(XHTMLView):
169
"""A form to change a user's password, with knowledge of the old one."""
170
template = 'templates/user-password-change.html'
174
def authorize(self, req):
175
"""Only allow access if the requesting user holds the permission,
176
and the target user has a password set. Otherwise we might be
177
clobbering external authn.
179
return super(PasswordChangeView, self).authorize(req) and \
180
self.context.passhash is not None
182
def populate(self, req, ctx):
184
if req.method == 'POST':
185
data = dict(req.get_fieldstorage())
186
if data.get('old_password') is None or \
187
not self.context.authenticate(data.get('old_password')):
188
error = 'Incorrect password.'
189
elif data.get('new_password') != data.get('new_password_again'):
190
error = 'New passwords do not match.'
191
elif not data.get('new_password'):
192
error = 'New password cannot be empty.'
194
self.context.password = data['new_password']
196
req.throw_redirect(req.uri)
199
ctx['user'] = self.context
202
class PasswordResetView(XHTMLView):
203
"""A form to reset a user's password, without knowledge of the old one."""
204
template = 'templates/user-password-reset.html'
207
def authorize(self, req):
208
"""Only allow access if the requesting user is an admin."""
209
return req.user.admin
211
def populate(self, req, ctx):
213
if req.method == 'POST':
214
data = dict(req.get_fieldstorage())
215
if data.get('new_password') != data.get('new_password_again'):
216
error = 'New passwords do not match.'
217
elif not data.get('new_password'):
218
error = 'New password cannot be empty.'
220
self.context.password = data['new_password']
222
req.throw_redirect(req.uri)
224
ctx['user'] = self.context
227
class Plugin(ViewPlugin, MediaPlugin):
229
The Plugin class for the user plugin.
232
forward_routes = (root_to_user,)
233
reverse_routes = (user_url,)
234
views = [(ApplicationRoot, 'users', UsersView),
235
(ivle.database.User, '+index', UserEditView),
236
(ivle.database.User, '+admin', UserAdminView),
237
(ivle.database.User, '+changepassword', PasswordChangeView),
238
(ivle.database.User, '+resetpassword', PasswordResetView),
239
(ivle.database.User, '+index', UserRESTView, 'api'),
243
('users', 'Users', 'Display and edit all users',
244
'users.png', 'users', 0, True)
247
public_forward_routes = forward_routes
248
public_reverse_routes = reverse_routes