21
21
import formencode.validators
22
22
from genshi.filters import HTMLFormFiller
24
from ivle.database import User
26
from ivle.pulldown_subj import enrol_user
27
24
from ivle.webapp import ApplicationRoot
28
from ivle.webapp.base.forms import BaseFormView, URLNameValidator
25
from ivle.webapp.base.rest import JSONRESTView, require_permission
29
26
from ivle.webapp.base.xhtml import XHTMLView
30
27
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
31
28
from ivle.webapp.admin.publishing import root_to_user, user_url
29
from ivle.database import User
34
35
class UsersView(XHTMLView):
35
36
"""A list of all IVLE users."""
36
37
template = 'templates/users.html'
38
breadcrumb_text = 'Users'
40
39
def authorize(self, req):
41
return req.user and req.user.admin
43
42
def populate(self, req, ctx):
45
44
ctx['users'] = req.store.find(User).order_by(User.login)
47
# List of fields returned as part of the user JSON dictionary
48
# (as returned by the get_user action)
50
"login", "state", "unixid", "email", "nick", "fullname",
51
"admin", "studentid", "acct_exp", "pass_exp", "last_login",
55
class UserRESTView(JSONRESTView):
57
A REST interface to the user object.
60
@require_permission('view')
63
user = ivle.util.object_to_dict(user_fields_list, self.context)
64
# Convert time stamps to nice strings
65
for k in 'pass_exp', 'acct_exp', 'last_login':
66
if user[k] is not None:
67
user[k] = unicode(user[k])
69
user['local_password'] = self.context.passhash is not None
48
72
class UserEditSchema(formencode.Schema):
49
73
nick = formencode.validators.UnicodeString(not_empty=True)
50
74
email = formencode.validators.Email(not_empty=False,
53
class UserEditView(BaseFormView):
77
class UserEditView(XHTMLView):
54
78
"""A form to change a user's details."""
55
79
template = 'templates/user-edit.html'
57
81
permission = 'edit'
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'] \
83
def filter(self, stream, ctx):
84
return stream | HTMLFormFiller(data=ctx['data'])
74
86
def populate(self, req, ctx):
75
super(UserEditView, self).populate(req, ctx)
87
if req.method == 'POST':
88
data = dict(req.get_fieldstorage())
90
validator = UserEditSchema()
91
data = validator.to_python(data, state=req)
92
self.context.nick = data['nick']
93
self.context.email = unicode(data['email']) if data['email'] \
96
req.throw_redirect(req.uri)
97
except formencode.Invalid, e:
98
errors = e.unpack_errors()
100
data = {'nick': self.context.nick,
101
'email': self.context.email
76
105
ctx['format_datetime'] = ivle.date.make_date_nice
77
106
ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
109
ctx['user'] = self.context
111
ctx['errors'] = errors
80
113
class UserAdminSchema(formencode.Schema):
81
114
admin = formencode.validators.StringBoolean(if_missing=False)
82
disabled = formencode.validators.StringBoolean(if_missing=False)
83
115
fullname = formencode.validators.UnicodeString(not_empty=True)
84
116
studentid = formencode.validators.UnicodeString(not_empty=False,
88
class UserAdminView(BaseFormView):
120
class UserAdminView(XHTMLView):
89
121
"""A form for admins to change more of a user's details."""
90
122
template = 'templates/user-admin.html'
93
125
def authorize(self, req):
94
126
"""Only allow access if the requesting user is an admin."""
95
return req.user and req.user.admin
99
return UserAdminSchema()
101
def get_default_data(self, req):
102
return {'admin': self.context.admin,
103
'disabled': self.context.state == u'disabled',
104
'fullname': self.context.fullname,
105
'studentid': self.context.studentid,
108
def save_object(self, req, data):
109
if self.context is req.user:
110
# Admin checkbox is disabled -- assume unchanged
111
data['admin'] = self.context.admin
112
data['disabled'] = self.context.state == u'disabled'
127
return req.user.admin
129
def filter(self, stream, ctx):
130
return stream | HTMLFormFiller(data=ctx['data'])
132
def populate(self, req, ctx):
133
if req.method == 'POST':
134
data = dict(req.get_fieldstorage())
136
validator = UserAdminSchema()
137
data = validator.to_python(data, state=req)
139
self.context.admin = data['admin']
140
self.context.fullname = data['fullname'] \
141
if data['fullname'] else None
142
self.context.studentid = data['studentid'] \
143
if data['studentid'] else None
145
req.throw_redirect(req.uri)
146
except formencode.Invalid, e:
147
errors = e.unpack_errors()
114
self.context.admin = data['admin']
115
if self.context.state in (u'enabled', u'disabled'):
116
self.context.state = (u'disabled' if data['disabled']
118
self.context.fullname = data['fullname'] \
119
if data['fullname'] else None
120
self.context.studentid = data['studentid'] \
121
if data['studentid'] else None
124
def populate(self, req, ctx):
125
super(UserAdminView, self).populate(req, ctx)
127
# Disable the admin checkbox if editing oneself
128
ctx['disable_admin'] = self.context is req.user
149
data = {'admin': self.context.admin,
150
'fullname': self.context.fullname,
151
'studentid': self.context.studentid,
156
ctx['user'] = self.context
158
ctx['errors'] = errors
130
160
class PasswordChangeView(XHTMLView):
131
161
"""A form to change a user's password, with knowledge of the old one."""
132
162
template = 'templates/user-password-change.html'
134
164
permission = 'edit'
136
166
def authorize(self, req):
232
224
forward_routes = (root_to_user,)
233
225
reverse_routes = (user_url,)
234
views = [(ApplicationRoot, ('users', '+index'), UsersView),
235
(ApplicationRoot, ('users', '+new'), UserNewView),
236
(User, '+index', UserEditView),
237
(User, '+admin', UserAdminView),
238
(User, '+changepassword', PasswordChangeView),
239
(User, '+resetpassword', PasswordResetView),
226
views = [(ApplicationRoot, 'users', UsersView),
227
(ivle.database.User, '+index', UserEditView),
228
(ivle.database.User, '+admin', UserAdminView),
229
(ivle.database.User, '+changepassword', PasswordChangeView),
230
(ivle.database.User, '+resetpassword', PasswordResetView),
231
(ivle.database.User, '+index', UserRESTView, 'api'),
243
('users', 'Users', 'Display and edit all users',
244
'users.png', 'users', 90, True)
247
234
public_forward_routes = forward_routes
248
235
public_reverse_routes = reverse_routes