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.database import User
26
from ivle.pulldown_subj import enrol_user
27
from ivle.webapp import ApplicationRoot
28
from ivle.webapp.base.forms import BaseFormView, URLNameValidator
29
from ivle.webapp.base.xhtml import XHTMLView
30
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
31
from ivle.webapp.admin.publishing import root_to_user, user_url
34
class UsersView(XHTMLView):
35
"""A list of all IVLE users."""
36
template = 'templates/users.html'
38
breadcrumb_text = 'Users'
40
def authorize(self, req):
41
return req.user and req.user.admin
43
def populate(self, req, ctx):
45
ctx['users'] = req.store.find(User).order_by(User.login)
48
class UserEditSchema(formencode.Schema):
49
nick = formencode.validators.UnicodeString(not_empty=True)
50
email = formencode.validators.Email(not_empty=False,
53
class UserEditView(BaseFormView):
54
"""A form to change a user's details."""
55
template = 'templates/user-edit.html'
61
return UserEditSchema()
63
def get_default_data(self, req):
64
return {'nick': self.context.nick,
65
'email': self.context.email
68
def save_object(self, req, data):
69
self.context.nick = data['nick']
70
self.context.email = unicode(data['email']) if data['email'] \
74
def populate(self, req, ctx):
75
super(UserEditView, self).populate(req, ctx)
76
ctx['format_datetime'] = ivle.date.make_date_nice
77
ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
78
ctx['svn_pass'] = req.user.svn_pass
81
class UserAdminSchema(formencode.Schema):
82
admin = formencode.validators.StringBoolean(if_missing=False)
83
disabled = formencode.validators.StringBoolean(if_missing=False)
84
fullname = formencode.validators.UnicodeString(not_empty=True)
85
studentid = formencode.validators.UnicodeString(not_empty=False,
89
class UserAdminView(BaseFormView):
90
"""A form for admins to change more of a user's details."""
91
template = 'templates/user-admin.html'
94
def authorize(self, req):
95
"""Only allow access if the requesting user is an admin."""
96
return req.user and req.user.admin
100
return UserAdminSchema()
102
def get_default_data(self, req):
103
return {'admin': self.context.admin,
104
'disabled': self.context.state == u'disabled',
105
'fullname': self.context.fullname,
106
'studentid': self.context.studentid,
109
def save_object(self, req, data):
110
if self.context is req.user:
111
# Admin checkbox is disabled -- assume unchanged
112
data['admin'] = self.context.admin
113
data['disabled'] = self.context.state == u'disabled'
115
self.context.admin = data['admin']
116
if self.context.state in (u'enabled', u'disabled'):
117
self.context.state = (u'disabled' if data['disabled']
119
self.context.fullname = data['fullname'] \
120
if data['fullname'] else None
121
self.context.studentid = data['studentid'] \
122
if data['studentid'] else None
125
def populate(self, req, ctx):
126
super(UserAdminView, self).populate(req, ctx)
128
# Disable the admin checkbox if editing oneself
129
ctx['disable_admin'] = self.context is req.user
131
class PasswordChangeView(XHTMLView):
132
"""A form to change a user's password, with knowledge of the old one."""
133
template = 'templates/user-password-change.html'
137
def authorize(self, req):
138
"""Only allow access if the requesting user holds the permission,
139
and the target user has a password set. Otherwise we might be
140
clobbering external authn.
142
return super(PasswordChangeView, self).authorize(req) and \
143
self.context.passhash is not None
145
def populate(self, req, ctx):
147
if req.method == 'POST':
148
data = dict(req.get_fieldstorage())
149
if data.get('old_password') is None or \
150
not self.context.authenticate(data.get('old_password')):
151
error = 'Incorrect password.'
152
elif data.get('new_password') != data.get('new_password_again'):
153
error = 'New passwords do not match.'
154
elif not data.get('new_password'):
155
error = 'New password cannot be empty.'
157
self.context.password = data['new_password']
159
req.throw_redirect(req.uri)
162
ctx['user'] = self.context
165
class PasswordResetView(XHTMLView):
166
"""A form to reset a user's password, without knowledge of the old one."""
167
template = 'templates/user-password-reset.html'
170
def authorize(self, req):
171
"""Only allow access if the requesting user is an admin."""
172
return req.user and req.user.admin
174
def populate(self, req, ctx):
176
if req.method == 'POST':
177
data = dict(req.get_fieldstorage())
178
if data.get('new_password') != data.get('new_password_again'):
179
error = 'New passwords do not match.'
180
elif not data.get('new_password'):
181
error = 'New password cannot be empty.'
183
self.context.password = data['new_password']
185
req.throw_redirect(req.uri)
187
ctx['user'] = self.context
191
class UserNewSchema(formencode.Schema):
192
login = URLNameValidator() # XXX: Validate uniqueness.
193
admin = formencode.validators.StringBoolean(if_missing=False)
194
fullname = formencode.validators.UnicodeString(not_empty=True)
195
studentid = formencode.validators.UnicodeString(not_empty=False,
198
email = formencode.validators.Email(not_empty=False,
202
class UserNewView(BaseFormView):
203
"""A form for admins to create new users."""
204
template = 'templates/user-new.html'
207
def authorize(self, req):
208
"""Only allow access if the requesting user is an admin."""
209
return req.user and req.user.admin
213
return UserNewSchema()
215
def get_default_data(self, req):
218
def save_object(self, req, data):
219
data['nick'] = data['fullname']
220
data['email'] = unicode(data['email']) if data['email'] else None
221
userobj = User(**data)
222
req.store.add(userobj)
223
enrol_user(req.config, req.store, userobj)
228
class Plugin(ViewPlugin, MediaPlugin):
230
The Plugin class for the user plugin.
233
forward_routes = (root_to_user,)
234
reverse_routes = (user_url,)
235
views = [(ApplicationRoot, ('users', '+index'), UsersView),
236
(ApplicationRoot, ('users', '+new'), UserNewView),
237
(User, '+index', UserEditView),
238
(User, '+admin', UserAdminView),
239
(User, '+changepassword', PasswordChangeView),
240
(User, '+resetpassword', PasswordResetView),
244
('users', 'Users', 'Display and edit all users',
245
'users.png', 'users', 90, True)
248
public_forward_routes = forward_routes
249
public_reverse_routes = reverse_routes