54
71
user['local_password'] = self.context.passhash is not None
57
class UserSettingsView(XHTMLView):
58
template = 'user-settings.html'
62
def __init__(self, req, login):
63
self.context = ivle.database.User.get_by_login(req.store, login)
64
if self.context is None:
67
def populate(self, req, ctx):
68
self.plugin_scripts[Plugin] = ['settings.js']
69
req.scripts_init = ['revert_settings']
71
ctx['login'] = self.context.login
74
class UserEditSchema(formencode.Schema):
75
nick = formencode.validators.UnicodeString(not_empty=True)
76
email = formencode.validators.Email(not_empty=False,
79
class UserEditView(XHTMLView):
80
"""A form to change a user's details."""
81
template = 'templates/user-edit.html'
85
def filter(self, stream, ctx):
86
return stream | HTMLFormFiller(data=ctx['data'])
88
def populate(self, 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
107
ctx['format_datetime'] = ivle.date.make_date_nice
108
ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
111
ctx['user'] = self.context
113
ctx['errors'] = errors
115
class UserAdminSchema(formencode.Schema):
116
admin = formencode.validators.StringBoolean(if_missing=False)
117
fullname = formencode.validators.UnicodeString(not_empty=True)
118
studentid = formencode.validators.UnicodeString(not_empty=False,
122
class UserAdminView(XHTMLView):
123
"""A form for admins to change more of a user's details."""
124
template = 'templates/user-admin.html'
127
def authorize(self, req):
128
"""Only allow access if the requesting user is an admin."""
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()
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
162
class PasswordChangeView(XHTMLView):
163
"""A form to change a user's password, with knowledge of the old one."""
164
template = 'templates/user-password-change.html'
168
def authorize(self, req):
169
"""Only allow access if the requesting user holds the permission,
170
and the target user has a password set. Otherwise we might be
171
clobbering external authn.
173
return super(PasswordChangeView, self).authorize(req) and \
174
self.context.passhash is not None
176
def populate(self, req, ctx):
178
if req.method == 'POST':
179
data = dict(req.get_fieldstorage())
180
if data.get('old_password') is None or \
181
not self.context.authenticate(data.get('old_password')):
182
error = 'Incorrect password.'
183
elif data.get('new_password') != data.get('new_password_again'):
184
error = 'New passwords do not match.'
185
elif not data.get('new_password'):
186
error = 'New password cannot be empty.'
188
self.context.password = data['new_password']
190
req.throw_redirect(req.uri)
193
ctx['user'] = self.context
196
class PasswordResetView(XHTMLView):
197
"""A form to reset a user's password, without knowledge of the old one."""
198
template = 'templates/user-password-reset.html'
201
def authorize(self, req):
202
"""Only allow access if the requesting user is an admin."""
203
return req.user.admin
205
def populate(self, req, ctx):
207
if req.method == 'POST':
208
data = dict(req.get_fieldstorage())
209
if data.get('new_password') != data.get('new_password_again'):
210
error = 'New passwords do not match.'
211
elif not data.get('new_password'):
212
error = 'New password cannot be empty.'
214
self.context.password = data['new_password']
216
req.throw_redirect(req.uri)
218
ctx['user'] = self.context
73
221
class Plugin(ViewPlugin, MediaPlugin):
75
223
The Plugin class for the user plugin.
77
# Magic attribute: urls
78
# Sequence of pairs/triples of
79
# (regex str, handler class, kwargs dict)
80
# The kwargs dict is passed to the __init__ of the view object
82
('~:login/+settings', UserSettingsView),
83
('api/~:login', UserRESTView),
226
forward_routes = (root_to_user,)
227
reverse_routes = (user_url,)
228
views = [(ApplicationRoot, 'users', UsersView),
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'),
237
('users', 'Users', 'Display and edit all users',
238
'users.png', 'users', 0, True)
241
public_forward_routes = forward_routes
242
public_reverse_routes = reverse_routes
86
244
media = 'user-media'