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
|
# 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
# TODO
# 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
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.username is None:
# Not logged in
req.throw_error(req.HTTP_FORBIDDEN)
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()
if req.path == "activate_me":
handle_activate_me(req, fields)
else:
req.throw_error(req.HTTP_BAD_REQUEST)
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.
"""
if req.method != "POST":
req.throw_error(req.HTTP_BAD_REQUEST)
try:
declaration = fields.getfirst('declaration')
except AttributeError:
req.throw_error(req.HTTP_BAD_REQUEST)
if declaration != USER_DECLARATION:
req.throw_error(req.HTTP_BAD_REQUEST)
session = req.get_session()
# 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 session['state'] != "no_agreement":
req.throw_error(req.HTTP_BAD_REQUEST)
# Get the arguments for usermgt.activate_user from the session
# (The user must have already logged in to use this app)
args = {
"login": req.username,
}
msg = {'activate_user': args}
response = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic,
decode = False)
# TODO: Figure out a way to let the user be "enabled" in this session.
# (Would require a write to the session?)
req.content_type = "text/plain"
req.write(response)
create_user_fields_required = [
'login', 'fullname', 'rolenm'
]
create_user_fields_optional = [
'password', 'nick', 'email', 'studentid'
]
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.
"""
session = req.get_session()
if req.method != "POST":
req.throw_error(req.HTTP_BAD_REQUEST)
# Check if this user has CAP_CREATEUSER
if not session['role'].hasCap(caps.CAP_UPDATEUSER):
req.throw_error(req.HTTP_FORBIDDEN)
# Make a dict of fields to create
create = {}
for f in create_user_fields_required:
try:
create[f] = fields.getfirst(f)
except AttributeError:
req.throw_error(req.HTTP_BAD_REQUEST)
for f in create_user_fields_optional:
try:
create[f] = fields.getfirst(f)
except AttributeError:
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}
# TEMP
req.write(msg)
return
# END TEMP
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', 'login', '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.
"""
session = req.get_session()
if req.method != "POST":
req.throw_error(req.HTTP_BAD_REQUEST)
# Only give full powers if this user has CAP_UPDATEUSER
fullpowers = session['role'].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 not fullpowers and login != req.username:
# Not allowed to edit other users
req.throw_error(req.HTTP_FORBIDDEN)
except AttributeError:
# If login not specified, update yourself
login = req.username
# Make a dict of fields to update
update = {}
for f in fieldlist:
try:
update[f] = fields.getfirst(f)
except AttributeError:
pass
# Get the arguments for usermgt.create_user from the session
# (The user must have already logged in to use this app)
args = {
"login": req.username,
"update": update,
}
msg = {'update_user': args}
# TEMP
req.write(msg)
return
# END TEMP
response = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic,
decode = False)
req.content_type = "text/plain"
req.write(response)
|