54
59
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
62
class UserEditSchema(formencode.Schema):
63
nick = formencode.validators.UnicodeString(not_empty=True)
64
email = formencode.validators.Email(not_empty=False,
67
class UserEditView(XHTMLView):
68
"""A form to change a user's details."""
69
template = 'templates/user-edit.html'
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
def filter(self, stream, ctx):
79
return stream | HTMLFormFiller(data=ctx['data'])
81
def populate(self, req, ctx):
82
if req.method == 'POST':
83
data = dict(req.get_fieldstorage())
85
validator = UserEditSchema()
86
data = validator.to_python(data, state=req)
87
self.context.nick = data['nick']
88
self.context.email = unicode(data['email']) if data['email'] \
91
req.throw_redirect(req.uri)
92
except formencode.Invalid, e:
93
errors = e.unpack_errors()
95
data = {'nick': self.context.nick,
96
'email': self.context.email
100
ctx['format_datetime'] = ivle.date.make_date_nice
101
ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
104
ctx['user'] = self.context
106
ctx['errors'] = errors
108
class UserAdminSchema(formencode.Schema):
109
admin = formencode.validators.StringBoolean(if_missing=False)
110
fullname = formencode.validators.UnicodeString(not_empty=True)
111
studentid = formencode.validators.UnicodeString(not_empty=False,
115
class UserAdminView(XHTMLView):
116
"""A form for admins to change more of a user's details."""
117
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
def authorize(self, req):
126
"""Only allow access if the requesting user is an admin."""
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()
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
160
class PasswordChangeView(XHTMLView):
161
"""A form to change a user's password, with knowledge of the old one."""
162
template = 'templates/user-password-change.html'
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
def authorize(self, req):
172
"""Only allow access if the requesting user holds the permission,
173
and the target user has a password set. Otherwise we might be
174
clobbering external authn.
176
return super(PasswordChangeView, self).authorize(req) and \
177
self.context.passhash is not None
179
def populate(self, req, ctx):
181
if req.method == 'POST':
182
data = dict(req.get_fieldstorage())
183
if data.get('old_password') is None or \
184
not self.context.authenticate(data.get('old_password')):
185
error = 'Incorrect password.'
186
elif data.get('new_password') != data.get('new_password_again'):
187
error = 'New passwords do not match.'
188
elif not data.get('new_password'):
189
error = 'New password cannot be empty.'
191
self.context.password = data['new_password']
193
req.throw_redirect(req.uri)
196
ctx['user'] = self.context
199
class PasswordResetView(XHTMLView):
200
"""A form to reset a user's password, without knowledge of the old one."""
201
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
def authorize(self, req):
210
"""Only allow access if the requesting user is an admin."""
211
return req.user.admin
213
def populate(self, req, ctx):
215
if req.method == 'POST':
216
data = dict(req.get_fieldstorage())
217
if data.get('new_password') != data.get('new_password_again'):
218
error = 'New passwords do not match.'
219
elif not data.get('new_password'):
220
error = 'New password cannot be empty.'
222
self.context.password = data['new_password']
224
req.throw_redirect(req.uri)
226
ctx['user'] = self.context
73
229
class Plugin(ViewPlugin, MediaPlugin):