21
21
import formencode.validators
22
22
from genshi.filters import HTMLFormFiller
24
from ivle.webapp import ApplicationRoot
24
25
from ivle.webapp.base.rest import JSONRESTView, require_permission
25
26
from ivle.webapp.base.xhtml import XHTMLView
26
27
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
27
from ivle.webapp.errors import NotFound, Unauthorized
28
from ivle.webapp.admin.publishing import root_to_user, user_url
29
from ivle.database import User
28
30
import ivle.database
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):
42
return req.user and req.user.admin
44
def populate(self, req, ctx):
46
ctx['users'] = req.store.find(User).order_by(User.login)
32
49
# List of fields returned as part of the user JSON dictionary
33
50
# (as returned by the get_user action)
34
51
user_fields_list = (
42
59
A REST interface to the user object.
44
def __init__(self, req, login):
45
super(UserRESTView, self).__init__(self, req, login)
46
self.context = ivle.database.User.get_by_login(req.store, login)
47
if self.context is None:
50
62
@require_permission('view')
51
63
def GET(self, req):
67
79
class UserEditView(XHTMLView):
68
80
"""A form to change a user's details."""
69
81
template = 'templates/user-edit.html'
71
83
permission = 'edit'
73
def __init__(self, req, login):
74
self.context = ivle.database.User.get_by_login(req.store, login)
75
if self.context is None:
78
85
def filter(self, stream, ctx):
79
86
return stream | HTMLFormFiller(data=ctx['data'])
108
115
class UserAdminSchema(formencode.Schema):
109
116
admin = formencode.validators.StringBoolean(if_missing=False)
117
disabled = formencode.validators.StringBoolean(if_missing=False)
110
118
fullname = formencode.validators.UnicodeString(not_empty=True)
111
119
studentid = formencode.validators.UnicodeString(not_empty=False,
115
123
class UserAdminView(XHTMLView):
116
124
"""A form for admins to change more of a user's details."""
117
125
template = 'templates/user-admin.html'
120
def __init__(self, req, login):
121
self.context = ivle.database.User.get_by_login(req.store, login)
122
if self.context is None:
125
128
def authorize(self, req):
126
129
"""Only allow access if the requesting user is an admin."""
127
return req.user.admin
130
return req.user and req.user.admin
129
132
def filter(self, stream, ctx):
130
133
return stream | HTMLFormFiller(data=ctx['data'])
136
139
validator = UserAdminSchema()
137
140
data = validator.to_python(data, state=req)
139
self.context.admin = data['admin']
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']
140
151
self.context.fullname = data['fullname'] \
141
152
if data['fullname'] else None
142
153
self.context.studentid = data['studentid'] \
147
158
errors = e.unpack_errors()
149
160
data = {'admin': self.context.admin,
161
'disabled': self.context.state == u'disabled',
150
162
'fullname': self.context.fullname,
151
163
'studentid': self.context.studentid,
156
168
ctx['user'] = self.context
169
# Disable the Admin checkbox if editing oneself
170
ctx['disable_admin'] = self.context is req.user
157
171
ctx['data'] = data
158
172
ctx['errors'] = errors
160
174
class PasswordChangeView(XHTMLView):
161
175
"""A form to change a user's password, with knowledge of the old one."""
162
176
template = 'templates/user-password-change.html'
164
178
permission = 'edit'
166
def __init__(self, req, login):
167
self.context = ivle.database.User.get_by_login(req.store, login)
168
if self.context is None:
171
180
def authorize(self, req):
172
181
"""Only allow access if the requesting user holds the permission,
173
182
and the target user has a password set. Otherwise we might be
199
208
class PasswordResetView(XHTMLView):
200
209
"""A form to reset a user's password, without knowledge of the old one."""
201
210
template = 'templates/user-password-reset.html'
204
def __init__(self, req, login):
205
self.context = ivle.database.User.get_by_login(req.store, login)
206
if self.context is None:
209
213
def authorize(self, req):
210
214
"""Only allow access if the requesting user is an admin."""
211
return req.user.admin
215
return req.user and req.user.admin
213
217
def populate(self, req, ctx):
231
235
The Plugin class for the user plugin.
233
# Magic attribute: urls
234
# Sequence of pairs/triples of
235
# (regex str, handler class, kwargs dict)
236
# The kwargs dict is passed to the __init__ of the view object
238
('~:login/+edit', UserEditView),
239
('~:login/+admin', UserAdminView),
240
('~:login/+changepassword', PasswordChangeView),
241
('~:login/+resetpassword', PasswordResetView),
242
('api/~:login', UserRESTView),
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', 0, True)
253
public_forward_routes = forward_routes
254
public_reverse_routes = reverse_routes
245
256
media = 'user-media'