22
22
from genshi.filters import HTMLFormFiller
24
24
from ivle.webapp import ApplicationRoot
25
from ivle.webapp.base.forms import BaseFormView
25
from ivle.webapp.base.rest import JSONRESTView, require_permission
26
26
from ivle.webapp.base.xhtml import XHTMLView
27
27
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
28
28
from ivle.webapp.admin.publishing import root_to_user, user_url
29
29
from ivle.database import User
33
35
class UsersView(XHTMLView):
34
36
"""A list of all IVLE users."""
35
37
template = 'templates/users.html'
37
38
breadcrumb_text = 'Users'
39
40
def authorize(self, req):
40
return req.user and req.user.admin
42
43
def populate(self, req, ctx):
44
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
47
73
class UserEditSchema(formencode.Schema):
48
74
nick = formencode.validators.UnicodeString(not_empty=True)
49
75
email = formencode.validators.Email(not_empty=False,
52
class UserEditView(BaseFormView):
78
class UserEditView(XHTMLView):
53
79
"""A form to change a user's details."""
54
80
template = 'templates/user-edit.html'
56
82
permission = 'edit'
60
return UserEditSchema()
62
def get_default_data(self, req):
63
return {'nick': self.context.nick,
64
'email': self.context.email
67
def save_object(self, req, data):
68
self.context.nick = data['nick']
69
self.context.email = unicode(data['email']) if data['email'] \
84
def filter(self, stream, ctx):
85
return stream | HTMLFormFiller(data=ctx['data'])
73
87
def populate(self, req, ctx):
74
super(UserEditView, self).populate(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
75
106
ctx['format_datetime'] = ivle.date.make_date_nice
76
107
ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
110
ctx['user'] = self.context
112
ctx['errors'] = errors
79
114
class UserAdminSchema(formencode.Schema):
80
115
admin = formencode.validators.StringBoolean(if_missing=False)
81
disabled = formencode.validators.StringBoolean(if_missing=False)
82
116
fullname = formencode.validators.UnicodeString(not_empty=True)
83
117
studentid = formencode.validators.UnicodeString(not_empty=False,
87
class UserAdminView(BaseFormView):
121
class UserAdminView(XHTMLView):
88
122
"""A form for admins to change more of a user's details."""
89
123
template = 'templates/user-admin.html'
92
126
def authorize(self, req):
93
127
"""Only allow access if the requesting user is an admin."""
94
return req.user and req.user.admin
98
return UserAdminSchema()
100
def get_default_data(self, req):
101
return {'admin': self.context.admin,
102
'disabled': self.context.state == u'disabled',
103
'fullname': self.context.fullname,
104
'studentid': self.context.studentid,
107
def save_object(self, req, data):
108
if self.context is req.user:
109
# Admin checkbox is disabled -- assume unchanged
110
data['admin'] = self.context.admin
111
data['disabled'] = self.context.state == u'disabled'
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()
113
self.context.admin = data['admin']
114
if self.context.state in (u'enabled', u'disabled'):
115
self.context.state = (u'disabled' if data['disabled']
117
self.context.fullname = data['fullname'] \
118
if data['fullname'] else None
119
self.context.studentid = data['studentid'] \
120
if data['studentid'] else None
123
def populate(self, req, ctx):
124
super(UserAdminView, self).populate(req, ctx)
126
# Disable the admin checkbox if editing oneself
127
ctx['disable_admin'] = self.context is req.user
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
129
161
class PasswordChangeView(XHTMLView):
130
162
"""A form to change a user's password, with knowledge of the old one."""
131
163
template = 'templates/user-password-change.html'
133
165
permission = 'edit'
135
167
def authorize(self, req):
193
225
forward_routes = (root_to_user,)
194
226
reverse_routes = (user_url,)
195
227
views = [(ApplicationRoot, 'users', UsersView),
196
(User, '+index', UserEditView),
197
(User, '+admin', UserAdminView),
198
(User, '+changepassword', PasswordChangeView),
199
(User, '+resetpassword', PasswordResetView),
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'),
203
('users', 'Users', 'Display and edit all users',
204
'users.png', 'users', 90, True)
207
235
public_forward_routes = forward_routes
208
236
public_reverse_routes = reverse_routes