53
54
user['local_password'] = self.context.passhash is not None
56
def PATCH(self, req, data):
58
# XXX Admins can set extra fields
59
# Note: Cannot change password here (use change_password named op)
61
for f in user_fields_list:
64
if isinstance(field, str):
65
field = unicode(field)
66
setattr(self.context, f, field)
70
class UserSettingsView(XHTMLView):
71
template = 'user-settings.html'
74
def __init__(self, req, login):
75
self.context = ivle.database.User.get_by_login(req.store, login)
76
if self.context is None:
79
if req.user is None or (req.user is not self.context and
80
req.user.rolenm != 'admin'):
83
def populate(self, req, ctx):
84
self.plugin_scripts[Plugin] = ['settings.js']
85
req.scripts_init = ['revert_settings']
87
ctx['login'] = self.context.login
57
class UserEditSchema(formencode.Schema):
58
nick = formencode.validators.UnicodeString(not_empty=True)
59
email = formencode.validators.Email(not_empty=False,
62
class UserEditView(XHTMLView):
63
"""A form to change a user's details."""
64
template = 'templates/user-edit.html'
68
def filter(self, stream, ctx):
69
return stream | HTMLFormFiller(data=ctx['data'])
71
def populate(self, req, ctx):
72
if req.method == 'POST':
73
data = dict(req.get_fieldstorage())
75
validator = UserEditSchema()
76
data = validator.to_python(data, state=req)
77
self.context.nick = data['nick']
78
self.context.email = unicode(data['email']) if data['email'] \
81
req.throw_redirect(req.uri)
82
except formencode.Invalid, e:
83
errors = e.unpack_errors()
85
data = {'nick': self.context.nick,
86
'email': self.context.email
90
ctx['format_datetime'] = ivle.date.make_date_nice
91
ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
94
ctx['user'] = self.context
96
ctx['errors'] = errors
98
class UserAdminSchema(formencode.Schema):
99
admin = formencode.validators.StringBoolean(if_missing=False)
100
fullname = formencode.validators.UnicodeString(not_empty=True)
101
studentid = formencode.validators.UnicodeString(not_empty=False,
105
class UserAdminView(XHTMLView):
106
"""A form for admins to change more of a user's details."""
107
template = 'templates/user-admin.html'
110
def authorize(self, req):
111
"""Only allow access if the requesting user is an admin."""
112
return req.user.admin
114
def filter(self, stream, ctx):
115
return stream | HTMLFormFiller(data=ctx['data'])
117
def populate(self, req, ctx):
118
if req.method == 'POST':
119
data = dict(req.get_fieldstorage())
121
validator = UserAdminSchema()
122
data = validator.to_python(data, state=req)
124
self.context.admin = data['admin']
125
self.context.fullname = data['fullname'] \
126
if data['fullname'] else None
127
self.context.studentid = data['studentid'] \
128
if data['studentid'] else None
130
req.throw_redirect(req.uri)
131
except formencode.Invalid, e:
132
errors = e.unpack_errors()
134
data = {'admin': self.context.admin,
135
'fullname': self.context.fullname,
136
'studentid': self.context.studentid,
141
ctx['user'] = self.context
143
ctx['errors'] = errors
145
class PasswordChangeView(XHTMLView):
146
"""A form to change a user's password, with knowledge of the old one."""
147
template = 'templates/user-password-change.html'
151
def authorize(self, req):
152
"""Only allow access if the requesting user holds the permission,
153
and the target user has a password set. Otherwise we might be
154
clobbering external authn.
156
return super(PasswordChangeView, self).authorize(req) and \
157
self.context.passhash is not None
159
def populate(self, req, ctx):
161
if req.method == 'POST':
162
data = dict(req.get_fieldstorage())
163
if data.get('old_password') is None or \
164
not self.context.authenticate(data.get('old_password')):
165
error = 'Incorrect password.'
166
elif data.get('new_password') != data.get('new_password_again'):
167
error = 'New passwords do not match.'
168
elif not data.get('new_password'):
169
error = 'New password cannot be empty.'
171
self.context.password = data['new_password']
173
req.throw_redirect(req.uri)
176
ctx['user'] = self.context
179
class PasswordResetView(XHTMLView):
180
"""A form to reset a user's password, without knowledge of the old one."""
181
template = 'templates/user-password-reset.html'
184
def authorize(self, req):
185
"""Only allow access if the requesting user is an admin."""
186
return req.user.admin
188
def populate(self, req, ctx):
190
if req.method == 'POST':
191
data = dict(req.get_fieldstorage())
192
if data.get('new_password') != data.get('new_password_again'):
193
error = 'New passwords do not match.'
194
elif not data.get('new_password'):
195
error = 'New password cannot be empty.'
197
self.context.password = data['new_password']
199
req.throw_redirect(req.uri)
201
ctx['user'] = self.context
89
204
class Plugin(ViewPlugin, MediaPlugin):
91
206
The Plugin class for the user plugin.
93
# Magic attribute: urls
94
# Sequence of pairs/triples of
95
# (regex str, handler class, kwargs dict)
96
# The kwargs dict is passed to the __init__ of the view object
98
('~:login/+settings', UserSettingsView),
99
('api/~:login', UserRESTView),
209
forward_routes = (root_to_user,)
210
reverse_routes = (user_url,)
211
views = [(ivle.database.User, '+edit', UserEditView),
212
(ivle.database.User, '+admin', UserAdminView),
213
(ivle.database.User, '+changepassword', PasswordChangeView),
214
(ivle.database.User, '+resetpassword', PasswordResetView),
215
(ivle.database.User, '+index', UserRESTView, 'api'),
218
public_forward_routes = forward_routes
219
public_reverse_routes = reverse_routes
102
221
media = 'user-media'