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'
38
breadcrumb_text = 'Users'
40
def authorize(self, req):
43
def populate(self, req, ctx):
45
ctx['users'] = req.store.find(User).order_by(User.login)
48
# List of fields returned as part of the user JSON dictionary
49
# (as returned by the get_user action)
51
"login", "state", "unixid", "email", "nick", "fullname",
52
"admin", "studentid", "acct_exp", "pass_exp", "last_login",
56
class UserRESTView(JSONRESTView):
58
A REST interface to the user object.
61
@require_permission('view')
64
user = ivle.util.object_to_dict(user_fields_list, self.context)
65
# Convert time stamps to nice strings
66
for k in 'pass_exp', 'acct_exp', 'last_login':
67
if user[k] is not None:
68
user[k] = unicode(user[k])
70
user['local_password'] = self.context.passhash is not None
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
220
class Plugin(ViewPlugin, MediaPlugin):
222
The Plugin class for the user plugin.
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