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

« back to all changes in this revision

Viewing changes to www/dispatch/__init__.py

  • Committer: mattgiuca
  • Date: 2007-12-21 06:15:19 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:124
dispatch/request: Added new fields: method and username.
                  Added new methods: get_session and get_fieldstorage.
dispatch/html: Writes out username of logged in user, and a "logout" link.
dispatch/__init__: Handles special logout app.
                   Performs authentication, and stores username in req.
                   No longer lets apache print HTML errors. Just returns OK.
dispatch: Added login module.

Added new package: auth (handles authentication and authorization).
    Currently just does hard-coded dummy authentication.

debuginfo: Added new info for new fields of request, and also prints out
        fieldstorage and session information.

conf/apps.py: debuginfo does not require auth (careful!)
              Commented out debuginfo (no longer displays warning)

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
 
38
38
from request import Request
39
39
import html
 
40
import login
40
41
from common import util
41
42
 
42
43
def handler(req):
51
52
 
52
53
    # Check req.app to see if it is valid. 404 if not.
53
54
    if req.app is not None and req.app not in conf.apps.app_url:
54
 
        # TODO: Nicer 404 message?
55
 
        req.throw_error(Request.HTTP_NOT_FOUND)
 
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)
56
61
 
57
62
    # app is the App object for the chosen app
58
63
    if req.app is None:
62
67
 
63
68
    # Check if app requires auth. If so, perform authentication and login.
64
69
    if app.requireauth:
65
 
        # TODO: Perform authentication
66
 
        pass
67
 
 
68
 
    # If user did not specify an app, HTTP redirect to default app and exit.
69
 
    if req.app is None:
70
 
        req.throw_redirect(util.make_path(conf.default_app))
71
 
 
72
 
    # Set the default title to the app's tab name, if any. Otherwise URL name.
73
 
    if app.name is not None:
74
 
        req.title = app.name
 
70
        req.username = login.login(req)
 
71
        logged_in = req.username is not None
75
72
    else:
76
 
        req.title = req.app
77
 
 
78
 
    # Call the specified app with the request object
79
 
    apps.call_app(app.dir, req)
 
73
        req.username = login.get_username(req)
 
74
        logged_in = True
 
75
 
 
76
    if logged_in:
 
77
        # If user did not specify an app, HTTP redirect to default app and
 
78
        # exit.
 
79
        if req.app is None:
 
80
            req.throw_redirect(util.make_path(conf.default_app))
 
81
 
 
82
        # Set the default title to the app's tab name, if any. Otherwise URL
 
83
        # name.
 
84
        if app.name is not None:
 
85
            req.title = app.name
 
86
        else:
 
87
            req.title = req.app
 
88
 
 
89
        # Call the specified app with the request object
 
90
        apps.call_app(app.dir, req)
 
91
    # if not logged in, login.login will have written the login box.
 
92
    # Just clean up and exit.
80
93
 
81
94
    # MAKE SURE we write the HTTP (and possibly HTML) header. This
82
95
    # wouldn't happen if nothing else ever got written, so we have to make
87
100
    if req.write_html_head_foot:
88
101
        html.write_html_foot(req)
89
102
 
90
 
    # Have Apache output its own HTML code if non-200 status codes were found
91
 
    return req.status
 
103
    # Note: Apache will not write custom HTML error messages here.
 
104
    # Use req.throw_error to do that.
 
105
    return req.OK
92
106
 
 
107
def logout(req):
 
108
    """Log out the current user (if any) by destroying the session state.
 
109
    Then redirect to the top-level IVLE page."""
 
110
    session = req.get_session()
 
111
    session.invalidate()
 
112
    session.delete()
 
113
    req.throw_redirect(util.make_path(''))