1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
# IVLE
# Copyright (C) 2007-2008 The University of Melbourne
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# App: userservice
# Author: Matt Giuca
# Date: 14/2/2008
# Provides an Ajax service for handling user management requests.
# This includes when a user logs in for the first time.
# NOTE: This app does NOT require authentication. This is because otherwise it
# would be blocked from receiving requests to activate when the user is trying
# to accept the TOS.
# It must do its own authentication and authorization.
### Actions ###
# The one-and-only path segment to userservice determines the action being
# undertaken.
# All actions require that you are logged in.
# All actions require method = POST, unless otherwise stated.
# userservice/activate_me
# Required cap: None
# declaration = "I accept the IVLE Terms of Service"
# Activate the currently-logged-in user's account. Requires that "declaration"
# is as above, and that the user's state is "no_agreement".
# userservice/create_user
# Required cap: CAP_CREATEUSER
# Arguments are the same as the database columns for the "login" table.
# Required:
# login, fullname, rolenm
# Optional:
# password, nick, email, studentid
# userservice/get_user
# method: May be GET
# Required cap: None to see yourself.
# CAP_GETUSER to see another user.
# Gets the login details of a user. Returns as a JSON object.
# login = Optional login name of user to get. If omitted, get yourself.
# userservice/update_user
# Required cap: None to update yourself.
# CAP_UPDATEUSER to update another user (and also more fields).
# (This is all-powerful so should be only for admins)
# login = Optional login name of user to update. If omitted, update yourself.
# Other fields are optional, and will set the given field of the user.
# Without CAP_UPDATEUSER, you may change the following fields of yourself:
# password, nick, email
# With CAP_UPDATEUSER, you may also change the following fields of any user:
# password, nick, email, login, rolenm, unixid, fullname, studentid
# (You can't change "state", but see userservice/[en|dis]able_user).
# TODO
# userservice/enable_user
# Required cap: CAP_UPDATEUSER
# Enable a user whose account has been disabled. Does not work for
# no_agreement or pending users.
# login = Login name of user to enable.
# TODO
# userservice/disable_user
# Required cap: CAP_UPDATEUSER
# Disable a user's account. Does not work for no_agreement or pending users.
# login = Login name of user to disable.
import os
import sys
import cjson
import common
import common.db
from common import (util, chat, caps)
import conf
from conf import (usrmgt_host, usrmgt_port, usrmgt_magic)
# The user must send this declaration message to ensure they acknowledge the
# TOS
USER_DECLARATION = "I accept the IVLE Terms of Service"
def handle(req):
"""Handler for the Console Service AJAX backend application."""
if req.user is None:
# Not logged in
req.throw_error(req.HTTP_FORBIDDEN,
"You are not logged in to IVLE.")
if len(req.path) > 0 and req.path[-1] == os.sep:
path = req.path[:-1]
else:
path = req.path
# The path determines which "command" we are receiving
fields = req.get_fieldstorage()
try:
func = actions_map[req.path]
except KeyError:
req.throw_error(req.HTTP_BAD_REQUEST,
"%s is not a valid userservice action." % repr(req.path))
func(req, fields)
def handle_activate_me(req, fields):
"""Create the jail, svn, etc, for the currently logged in user (this is
put in the queue for usermgt to do).
This will block until usermgt returns, which could take seconds to minutes
in the extreme. Therefore, it is designed to be called by Ajax, with a
nice "Please wait" message on the frontend.
This will signal that the user has accepted the terms of the license
agreement, and will result in the user's database status being set to
"enabled". (Note that it will be set to "pending" for the duration of the
handling).
As such, it takes a single POST field, "declaration", which
must have the value, "I accept the IVLE Terms of Service".
(Otherwise users could navigate to /userservice/createme without
"accepting" the terms - at least this way requires them to acknowledge
their acceptance). It must only be called through a POST request.
"""
db = common.db.DB()
try:
if req.method != "POST":
req.throw_error(req.HTTP_METHOD_NOT_ALLOWED,
"Only POST requests are valid methods to activate_me.")
try:
declaration = fields.getfirst('declaration')
except AttributeError:
declaration = None # Will fail next test
if declaration != USER_DECLARATION:
req.throw_error(req.HTTP_BAD_REQUEST,
"Please use the Terms of Service form instead of talking to "
"this service directly.")
# Make sure the user's status is "no_agreement", and set status to
# pending, within the one transaction. This ensures we only do this
# one time.
db.start_transaction()
try:
user_details = db.get_user(req.user.login)
# Check that the user's status is "no_agreement".
# (Both to avoid redundant calls, and to stop disabled users from
# re-enabling their accounts).
if user_details.state != "no_agreement":
req.throw_error(req.HTTP_BAD_REQUEST,
"You have already agreed to the terms.")
# Write state "pending" to ensure we don't try this again
db.update_user(req.user.login, state="pending")
except:
db.rollback()
raise
db.commit()
# Get the arguments for usermgt.activate_user from the session
# (The user must have already logged in to use this app)
args = {
"login": req.user.login,
}
msg = {'activate_user': args}
response = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic,
decode = False)
# Write to the user's session to allow them to be activated
req.user.state = "enabled"
session = req.get_session()
session['user'] = req.user
session.save()
# Write the response
req.content_type = "text/plain"
req.write(response)
finally:
db.close()
def handle_do_checkout(req, fields):
"""Check out the user's repositories into their home directory, failing
silently for any that already exist.
This can be done in a limited form by any user, on their own account,
or with full powers by a user with CAP_UPDATEUSER on any account.
"""
if req.method != "POST":
req.throw_error(req.HTTP_METHOD_NOT_ALLOWED,
"Only POST requests are valid methods to do_checkout.")
# Only give full powers if this user has CAP_UPDATEUSER
fullpowers = req.user.hasCap(caps.CAP_UPDATEUSER)
# List of fields that may be changed
fieldlist = (update_user_fields_admin if fullpowers
else update_user_fields_anyone)
try:
login = fields.getfirst('login')
if login is None:
raise AttributeError()
if not fullpowers and login != req.user.login:
# Not allowed to edit other users
req.throw_error(req.HTTP_FORBIDDEN,
"You do not have permission to check out another user's "
"repository.")
except AttributeError:
# If login not specified, update yourself
login = req.user.login
msg = {'do_checkout': {"login": login}}
response = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic,
decode = False)
req.content_type = "text/plain"
req.write(response)
create_user_fields_required = [
'login', 'fullname', 'rolenm'
]
create_user_fields_optional = [
'password', 'nick', 'email', 'studentid', 'unixid'
]
def handle_create_user(req, fields):
"""Create a new user, whose state is no_agreement.
This does not create the user's jail, just an entry in the database which
allows the user to accept an agreement.
"""
if req.method != "POST":
req.throw_error(req.HTTP_METHOD_NOT_ALLOWED,
"Only POST requests are valid methods to create_user.")
# Check if this user has CAP_UPDATEUSER
if not req.user.hasCap(caps.CAP_UPDATEUSER):
req.throw_error(req.HTTP_FORBIDDEN,
"You do not have permission to create users.")
# Make a dict of fields to create
create = {}
for f in create_user_fields_required:
val = fields.getfirst(f)
if val is not None:
create[f] = val
else:
req.throw_error(req.HTTP_BAD_REQUEST,
"Required field %s missing." % repr(f))
for f in create_user_fields_optional:
val = fields.getfirst(f)
if val is not None:
create[f] = val
else:
pass
# Get the arguments for usermgt.create_user from the session
# (The user must have already logged in to use this app)
msg = {'create_user': create}
response = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic,
decode = False)
req.content_type = "text/plain"
req.write(response)
update_user_fields_anyone = [
'password', 'nick', 'email'
]
update_user_fields_admin = [
'password', 'nick', 'email', 'rolenm', 'unixid', 'fullname',
'studentid'
]
def handle_update_user(req, fields):
"""Update a user's account details.
This can be done in a limited form by any user, on their own account,
or with full powers by a user with CAP_UPDATEUSER on any account.
"""
if req.method != "POST":
req.throw_error(req.HTTP_METHOD_NOT_ALLOWED,
"Only POST requests are valid methods to update_user.")
# Only give full powers if this user has CAP_UPDATEUSER
fullpowers = req.user.hasCap(caps.CAP_UPDATEUSER)
# List of fields that may be changed
fieldlist = (update_user_fields_admin if fullpowers
else update_user_fields_anyone)
try:
login = fields.getfirst('login')
if login is None:
raise AttributeError()
if not fullpowers and login != req.user.login:
# Not allowed to edit other users
req.throw_error(req.HTTP_FORBIDDEN,
"You do not have permission to update another user.")
except AttributeError:
# If login not specified, update yourself
login = req.user.login
# Make a dict of fields to update
update = {}
for f in fieldlist:
val = fields.getfirst(f)
if val is not None:
update[f] = val
else:
pass
update['login'] = login
# Get the arguments for usermgt.create_user from the session
# (The user must have already logged in to use this app)
args = {
"login": req.user.login,
"update": update,
}
msg = {'update_user': args}
response = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic,
decode = False)
# Re-read the user's details from the DB so we can update their session
# XXX potentially-unsafe session write
if login == req.user.login:
db = common.db.DB()
user = db.get_user(login)
session = req.get_session()
session['user'] = user
session.save()
db.close()
req.content_type = "text/plain"
req.write(response)
def handle_get_user(req, fields):
"""
Retrieve a user's account details. This returns all details which the db
module is willing to give up, EXCEPT the following fields:
svn_pass
"""
# Only give full powers if this user has CAP_GETUSER
fullpowers = req.user.hasCap(caps.CAP_GETUSER)
try:
login = fields.getfirst('login')
if login is None:
raise AttributeError()
if not fullpowers and login != req.user.login:
# Not allowed to edit other users
req.throw_error(req.HTTP_FORBIDDEN,
"You do not have permission to see another user.")
except AttributeError:
# If login not specified, update yourself
login = req.user.login
# Just talk direct to the DB
db = common.db.DB()
user = db.get_user(login)
db.close()
user = dict(user)
if 'role' in user:
user['rolenm'] = str(user['role'])
del user['role']
try:
del user['svn_pass']
except KeyError:
pass
# Convert time stamps to nice strings
try:
if user['pass_exp'] is not None:
user['pass_exp'] = str(user['pass_exp'])
except KeyError:
pass
try:
if user['acct_exp'] is not None:
user['acct_exp'] = str(user['acct_exp'])
except KeyError:
pass
try:
if user['last_login'] is not None:
user['last_login'] = str(user['last_login'])
except KeyError:
pass
response = cjson.encode(user)
req.content_type = "text/plain"
req.write(response)
# Map action names (from the path)
# to actual function objects
actions_map = {
"activate_me": handle_activate_me,
"do_checkout": handle_do_checkout,
"create_user": handle_create_user,
"update_user": handle_update_user,
"get_user": handle_get_user,
}
|