~azzar1/unity/add-show-desktop-key

465 by mattgiuca
Added new app: userservice, which is an ajax service for user management
1
# IVLE
2
# Copyright (C) 2007-2008 The University of Melbourne
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18
# App: userservice
19
# Author: Matt Giuca
20
# Date: 14/2/2008
21
22
import os
23
import sys
24
25
import cjson
26
27
from common import (util, chat)
28
import conf
29
30
# TODO: Config these in setup.py
31
USERMGT_HOST = "localhost"
32
USERMGT_PORT = 3000
33
USERMGT_MAGIC = "magicuser"
34
35
# The user must send this declaration message to ensure they acknowledge the
36
# TOS
37
USER_DECLARATION = "I accept the IVLE Terms of Service"
38
39
def handle(req):
40
    """Handler for the Console Service AJAX backend application."""
41
    if len(req.path) > 0 and req.path[-1] == os.sep:
42
        path = req.path[:-1]
43
    else:
44
        path = req.path
45
    # The path determines which "command" we are receiving
46
    if req.path == "createme":
47
        handle_createme(req)
48
    else:
49
        req.throw_error(req.HTTP_BAD_REQUEST)
50
51
def handle_createme(req):
52
    """Create the jail, svn, etc, for the currently logged in user (this is
53
    put in the queue for usermgt to do).
54
    This will block until usermgt returns, which could take seconds to minutes
55
    in the extreme. Therefore, it is designed to be called by Ajax, with a
56
    nice "Please wait" message on the frontend.
57
58
    This will signal that the user has accepted the terms of the license
59
    agreement, and will result in the user's database status being set to
60
    "enabled". (Note that it will be set to "pending" for the duration of the
61
    handling).
62
63
    As such, it takes a single POST field, "declaration", which
64
    must have the value, "I accept the IVLE Terms of Service".
65
    (Otherwise users could navigate to /userservice/createme without
66
    "accepting" the terms - at least this way requires them to acknowledge
67
    their acceptance). It must only be called through a POST request.
68
    """
69
    if req.method != "POST":
70
        req.throw_error(req.HTTP_BAD_REQUEST)
71
    fields = req.get_fieldstorage()
72
    try:
73
        declaration = fields.getfirst('declaration')
74
    except AttributeError:
75
        req.throw_error(req.HTTP_BAD_REQUEST)
76
    if declaration != USER_DECLARATION:
77
        req.throw_error(req.HTTP_BAD_REQUEST)
78
79
    # Get the arguments for usermgt.create_user from the session
80
    # (The user must have already logged in to use this app)
81
    session = req.get_session()
82
    args = {
83
        "username": session['login_name'],
84
        "uid": session['unixid'],
85
    }
86
    msg = {'create_user': args}
87
88
    response = chat.chat(USERMGT_HOST, USERMGT_PORT, msg, USERMGT_MAGIC,
89
        decode = False)
90
    req.content_type = "text/plain"
91
    req.write(response)
92