37
39
breadcrumb_text = 'Users'
39
41
def authorize(self, req):
40
return req.user and req.user.admin
42
44
def populate(self, req, ctx):
44
46
ctx['users'] = req.store.find(User).order_by(User.login)
49
# List of fields returned as part of the user JSON dictionary
50
# (as returned by the get_user action)
52
"login", "state", "unixid", "email", "nick", "fullname",
53
"admin", "studentid", "acct_exp", "pass_exp", "last_login",
57
class UserRESTView(JSONRESTView):
59
A REST interface to the user object.
62
@require_permission('view')
65
user = ivle.util.object_to_dict(user_fields_list, self.context)
66
# Convert time stamps to nice strings
67
for k in 'pass_exp', 'acct_exp', 'last_login':
68
if user[k] is not None:
69
user[k] = unicode(user[k])
71
user['local_password'] = self.context.passhash is not None
47
74
class UserEditSchema(formencode.Schema):
48
75
nick = formencode.validators.UnicodeString(not_empty=True)
49
76
email = formencode.validators.Email(not_empty=False,
52
class UserEditView(BaseFormView):
79
class UserEditView(XHTMLView):
53
80
"""A form to change a user's details."""
54
81
template = 'templates/user-edit.html'
56
83
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'] \
85
def filter(self, stream, ctx):
86
return stream | HTMLFormFiller(data=ctx['data'])
73
88
def populate(self, req, ctx):
74
super(UserEditView, self).populate(req, ctx)
89
if req.method == 'POST':
90
data = dict(req.get_fieldstorage())
92
validator = UserEditSchema()
93
data = validator.to_python(data, state=req)
94
self.context.nick = data['nick']
95
self.context.email = unicode(data['email']) if data['email'] \
98
req.throw_redirect(req.uri)
99
except formencode.Invalid, e:
100
errors = e.unpack_errors()
102
data = {'nick': self.context.nick,
103
'email': self.context.email
75
107
ctx['format_datetime'] = ivle.date.make_date_nice
76
108
ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
111
ctx['user'] = self.context
113
ctx['errors'] = errors
79
115
class UserAdminSchema(formencode.Schema):
80
116
admin = formencode.validators.StringBoolean(if_missing=False)
81
disabled = formencode.validators.StringBoolean(if_missing=False)
82
117
fullname = formencode.validators.UnicodeString(not_empty=True)
83
118
studentid = formencode.validators.UnicodeString(not_empty=False,
87
class UserAdminView(BaseFormView):
122
class UserAdminView(XHTMLView):
88
123
"""A form for admins to change more of a user's details."""
89
124
template = 'templates/user-admin.html'
92
127
def authorize(self, req):
93
128
"""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'
129
return req.user.admin
131
def filter(self, stream, ctx):
132
return stream | HTMLFormFiller(data=ctx['data'])
134
def populate(self, req, ctx):
135
if req.method == 'POST':
136
data = dict(req.get_fieldstorage())
138
validator = UserAdminSchema()
139
data = validator.to_python(data, state=req)
141
self.context.admin = data['admin']
142
self.context.fullname = data['fullname'] \
143
if data['fullname'] else None
144
self.context.studentid = data['studentid'] \
145
if data['studentid'] else None
147
req.throw_redirect(req.uri)
148
except formencode.Invalid, e:
149
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
151
data = {'admin': self.context.admin,
152
'fullname': self.context.fullname,
153
'studentid': self.context.studentid,
158
ctx['user'] = self.context
160
ctx['errors'] = errors
129
162
class PasswordChangeView(XHTMLView):
130
163
"""A form to change a user's password, with knowledge of the old one."""
193
226
forward_routes = (root_to_user,)
194
227
reverse_routes = (user_url,)
195
228
views = [(ApplicationRoot, 'users', UsersView),
196
(User, '+index', UserEditView),
197
(User, '+admin', UserAdminView),
198
(User, '+changepassword', PasswordChangeView),
199
(User, '+resetpassword', PasswordResetView),
229
(ivle.database.User, '+index', UserEditView),
230
(ivle.database.User, '+admin', UserAdminView),
231
(ivle.database.User, '+changepassword', PasswordChangeView),
232
(ivle.database.User, '+resetpassword', PasswordResetView),
233
(ivle.database.User, '+index', UserRESTView, 'api'),
203
237
('users', 'Users', 'Display and edit all users',
204
'users.png', 'users', 90, True)
238
'users.png', 'users', 0, True)
207
241
public_forward_routes = forward_routes