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

« back to all changes in this revision

Viewing changes to www/dispatch/login.py

  • Committer: William Grant
  • Date: 2012-06-28 01:52:02 UTC
  • Revision ID: me@williamgrant.id.au-20120628015202-f6ru7o367gt6nvgz
Hah

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# IVLE - Informatics Virtual Learning Environment
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.login
19
 
# Author: Matt Giuca
20
 
# Date: 21/12/2007
21
 
 
22
 
# Provides services for checking logins and presenting the login page.
23
 
 
24
 
from mod_python import (util, Session)
25
 
 
26
 
from auth import authenticate
27
 
 
28
 
def login(req):
29
 
    """Determines whether the user is logged in or not (looking at sessions),
30
 
    and if not, presents the login page. Returns a String username, or None
31
 
    if not logged in.
32
 
 
33
 
    If the user was already logged in, nothing is written to req. Returns
34
 
    a string of the username.
35
 
 
36
 
    If the user was not logged in, but manages to authenticate due to
37
 
    included postdata with a valid username/password, throws a redirect
38
 
    back to the same page (to avoid leaving POSTDATA in the browser).
39
 
 
40
 
    If the user is not logged in, or fails to authenticate, a full page is
41
 
    written to req. Returns None. The caller should immediately terminate.
42
 
    """
43
 
    session = req.get_session()
44
 
 
45
 
    # Check the session to see if someone is logged in. If so, go with it.
46
 
    # No security is required here. You must have already been authenticated
47
 
    # in order to get a 'login_name' variable in the session.
48
 
    try:
49
 
        return session['login_name']
50
 
    except KeyError:
51
 
        pass
52
 
 
53
 
    badlogin = False
54
 
    # Check if there is any postdata containing login information
55
 
    if req.method == 'POST':
56
 
        fields = req.get_fieldstorage()
57
 
        username = fields.getfirst('user')
58
 
        password = fields.getfirst('pass')
59
 
        if username is not None:
60
 
            # From this point onwards, we will be showing an error message
61
 
            # if unsuccessful.
62
 
            # Authenticate
63
 
            if password is None:
64
 
                badlogin = True
65
 
            else:
66
 
                login_details = \
67
 
                    authenticate.authenticate(username.value, password.value)
68
 
                if login_details is None:
69
 
                    badlogin = True
70
 
                else:
71
 
                    # Success - Set the session and redirect to avoid POSTDATA
72
 
                    session['login_name'] = username.value
73
 
                    session['nick'] = login_details['nick']
74
 
                    session['fullname'] = login_details['fullname']
75
 
                    session['rolenm'] = login_details['rolenm']
76
 
                    session['studentid'] = login_details['studentid']
77
 
                    session.save()
78
 
                    req.throw_redirect(req.uri)
79
 
 
80
 
    # User is not logged in. Present the login box.
81
 
    # Give a 403 Forbidden status, but present a full HTML login page
82
 
    # instead of the usual 403 error.
83
 
    req.status = req.HTTP_FORBIDDEN
84
 
    req.content_type = "text/html"
85
 
    req.title = "Login"
86
 
    req.write_html_head_foot = True
87
 
 
88
 
    # Write the HTML for the login page
89
 
    # If badlogin, display an error message indicating a failed login
90
 
    req.write("""<div id="ivle_padding">
91
 
<p>Welcome to the Informatics Virtual Learning Environment.
92
 
   Please log in to access your files and assessment.</p>""")
93
 
    if badlogin:
94
 
        req.write("""<p class="error">Invalid username or password.</p>""")
95
 
    req.write("""<form action="" method="post">
96
 
  <table>
97
 
    <tr><td>Username:</td><td><input name="user" type="text" /></td></tr>
98
 
    <tr><td>Password:</td><td><input name="pass" type="password" /></td></tr>
99
 
    <tr><td colspan="2"><input type="submit" value="Login" /></td></tr>
100
 
  </table>
101
 
</form>
102
 
</div>
103
 
""")
104
 
 
105
 
    return None
106
 
 
107
 
def get_username(req):
108
 
    """Gets the name of the logged in user, without presenting a login box
109
 
    or attempting to authenticate.
110
 
    Returns None if there is no user logged in.
111
 
    """
112
 
    session = req.get_session()
113
 
 
114
 
    # Check the session to see if someone is logged in. If so, go with it.
115
 
    # No security is required here. You must have already been authenticated
116
 
    # in order to get a 'login_name' variable in the session.
117
 
    try:
118
 
        return session['login_name']
119
 
    except KeyError:
120
 
        return None