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

93 by mattgiuca
New directory hierarchy.
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
# Module: dispatch
19
# Author: Matt Giuca
20
# Date: 11/12/2007
21
22
# This is a mod_python handler program. The correct way to call it is to have
23
# Apache send all requests to be handled by the module 'dispatch'.
24
25
# Top-level handler. Handles all requests to all pages in IVLE.
26
# Handles authentication (not authorization).
27
# Then passes the request along to the appropriate ivle app.
28
29
import mod_python
30
from mod_python import apache
31
32
import os
33
import os.path
34
import conf
35
import conf.apps
36
import apps
37
38
from request import Request
39
import html
124 by mattgiuca
dispatch/request: Added new fields: method and username.
40
import login
493 by dcoles
session.php: More interfaceing between IVLE and phpBB. Adds groups, emails and
41
from common import (util, forumutil)
93 by mattgiuca
New directory hierarchy.
42
43
def handler(req):
44
    """Handles a request which may be to anywhere in the site except media.
45
    Intended to be called by mod_python, as a handler.
46
47
    req: An Apache request object.
48
    """
49
    # Make the request object into an IVLE request which can be passed to apps
50
    apachereq = req
51
    req = Request(req, html.write_html_head)
52
53
    # Check req.app to see if it is valid. 404 if not.
54
    if req.app is not None and req.app not in conf.apps.app_url:
124 by mattgiuca
dispatch/request: Added new fields: method and username.
55
        # Maybe it is a special app!
56
        if req.app == 'logout':
57
            logout(req)
58
        else:
59
            # TODO: Nicer 404 message?
60
            req.throw_error(Request.HTTP_NOT_FOUND)
93 by mattgiuca
New directory hierarchy.
61
259 by mattgiuca
setup.py: Added a new config variable "public_host", which lets the admin set
62
    # Special handling for public mode - just call public app and get out
63
    # NOTE: This will not behave correctly if the public app uses
64
    # write_html_head_foot, but "serve" does not.
65
    if req.publicmode:
66
        app = conf.apps.app_url[conf.apps.public_app]
67
        apps.call_app(app.dir, req)
68
        return req.OK
69
93 by mattgiuca
New directory hierarchy.
70
    # app is the App object for the chosen app
71
    if req.app is None:
195 by mattgiuca
Configuration: Moved "default_app" setting from conf/conf.py to conf/apps.py.
72
        app = conf.apps.app_url[conf.apps.default_app]
93 by mattgiuca
New directory hierarchy.
73
    else:
74
        app = conf.apps.app_url[req.app]
75
76
    # Check if app requires auth. If so, perform authentication and login.
504 by mattgiuca
Warning: Broken build, but rather unavoidable or this commit will spiral out
77
    # This will either return a User object, None, or perform a redirect
78
    # which we will not catch here.
93 by mattgiuca
New directory hierarchy.
79
    if app.requireauth:
504 by mattgiuca
Warning: Broken build, but rather unavoidable or this commit will spiral out
80
        req.user = login.login(req)
81
        logged_in = req.user is not None
93 by mattgiuca
New directory hierarchy.
82
    else:
504 by mattgiuca
Warning: Broken build, but rather unavoidable or this commit will spiral out
83
        req.user = login.get_user_details(req)
124 by mattgiuca
dispatch/request: Added new fields: method and username.
84
        logged_in = True
85
86
    if logged_in:
338 by mattgiuca
dispatch: Saves the session every time a request is made. This keeps the users
87
        # Keep the user's session alive by writing to the session object.
444 by drtomc
dispatch: Change the session timeout to 24 hours (there doesn't seem to be
88
        # req.get_session().save()
89
        # Well, it's a fine idea, but it creates considerable grief in the
90
        # concurrent update department, so instead, we'll just make the
91
        # sessions not time out.
92
        
124 by mattgiuca
dispatch/request: Added new fields: method and username.
93
        # If user did not specify an app, HTTP redirect to default app and
94
        # exit.
95
        if req.app is None:
195 by mattgiuca
Configuration: Moved "default_app" setting from conf/conf.py to conf/apps.py.
96
            req.throw_redirect(util.make_path(conf.apps.default_app))
124 by mattgiuca
dispatch/request: Added new fields: method and username.
97
98
        # Set the default title to the app's tab name, if any. Otherwise URL
99
        # name.
100
        if app.name is not None:
101
            req.title = app.name
102
        else:
103
            req.title = req.app
104
105
        # Call the specified app with the request object
106
        apps.call_app(app.dir, req)
107
    # if not logged in, login.login will have written the login box.
108
    # Just clean up and exit.
93 by mattgiuca
New directory hierarchy.
109
110
    # MAKE SURE we write the HTTP (and possibly HTML) header. This
111
    # wouldn't happen if nothing else ever got written, so we have to make
112
    # sure.
113
    req.ensure_headers_written()
114
115
    # When done, write out the HTML footer if the app has requested it
116
    if req.write_html_head_foot:
117
        html.write_html_foot(req)
118
124 by mattgiuca
dispatch/request: Added new fields: method and username.
119
    # Note: Apache will not write custom HTML error messages here.
120
    # Use req.throw_error to do that.
121
    return req.OK
93 by mattgiuca
New directory hierarchy.
122
124 by mattgiuca
dispatch/request: Added new fields: method and username.
123
def logout(req):
124
    """Log out the current user (if any) by destroying the session state.
125
    Then redirect to the top-level IVLE page."""
126
    session = req.get_session()
127
    session.invalidate()
128
    session.delete()
493 by dcoles
session.php: More interfaceing between IVLE and phpBB. Adds groups, emails and
129
    req.add_cookie(forumutil.invalidated_forum_cookie())
124 by mattgiuca
dispatch/request: Added new fields: method and username.
130
    req.throw_redirect(util.make_path(''))