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

« back to all changes in this revision

Viewing changes to ivle/dispatch/login.py

Move the login machinery to the new framework.

The login page is now redirected to by the XHTML Unauthorized page, and knows
to return to the right URL when authentication succeeds. It also means that we
no longer return a 200 with login page content for a totally unrelated page...

Some of the user stuff (disabling, Tos, etc.) is now broken, and /logout moved
to /+logout.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
from ivle.webapp.base.plugins import CookiePlugin
36
36
import ivle.database
37
37
 
38
 
def login(req):
39
 
    """Determines whether the user is logged in or not (looking at sessions),
40
 
    and if not, presents the login page. Returns a User object, or None
41
 
    if not logged in.
42
 
 
43
 
    If the user was already logged in, nothing is written to req. Returns
44
 
    the User object for the logged in user.
45
 
 
46
 
    If the user was not logged in, but manages to authenticate due to
47
 
    included postdata with a valid username/password, throws a redirect
48
 
    back to the same page (to avoid leaving POSTDATA in the browser).
49
 
 
50
 
    If the user is not logged in, or fails to authenticate, a full page is
51
 
    written to req. Returns None. The caller should immediately terminate.
52
 
    """
53
 
    # Get the user details from the session, if already logged in
54
 
    # (None means not logged in yet)
55
 
    user = get_user_details(req)
56
 
 
57
 
    # Check the session to see if someone is logged in. If so, go with it.
58
 
    # No security is required here. You must have already been authenticated
59
 
    # in order to get a 'login_name' variable in the session.
60
 
    if user is not None and user.state == "enabled":
61
 
        # Only allow users to authenticate if their account is ENABLED
62
 
        return user
63
 
 
64
 
    badlogin = None
65
 
 
66
 
    # Check if there is any postdata containing login information
67
 
    if user is None and req.method == 'POST':
68
 
        fields = req.get_fieldstorage()
69
 
        username = fields.getfirst('user')
70
 
        password = fields.getfirst('pass')
71
 
        if username is not None:
72
 
            # From this point onwards, we will be showing an error message
73
 
            # if unsuccessful.
74
 
            # Authenticate
75
 
            if password is None:
76
 
                badlogin = "No password supplied."
77
 
            else:
78
 
                try:
79
 
                    user = authenticate.authenticate(req.store,
80
 
                                username.value, password.value)
81
 
                except AuthError, msg:
82
 
                    badlogin = msg
83
 
                if user is None:
84
 
                    # Must have got an error. Do not authenticate.
85
 
                    pass
86
 
                elif user.password_expired:
87
 
                    badlogin = "Your password has expired."
88
 
                elif user.account_expired:
89
 
                    badlogin = "Your account has expired."
90
 
                else:
91
 
                    # Success - Set the session and redirect to avoid POSTDATA
92
 
                    session = req.get_session()
93
 
                    session['login'] = user.login
94
 
                    session.save()
95
 
                    user.last_login = datetime.datetime.now()
96
 
                    req.store.commit()
97
 
 
98
 
                    # Create cookies for plugins that might request them.
99
 
                    for plugin in req.plugin_index[CookiePlugin]:
100
 
                        for cookie in plugin.cookies:
101
 
                            # The function can be None if they just need to be
102
 
                            # deleted at logout.
103
 
                            if plugin.cookies[cookie] is not None:
104
 
                                req.add_cookie(mod_python.Cookie.Cookie(cookie,
105
 
                                      plugin.cookies[cookie](user), path='/'))
106
 
 
107
 
                    req.throw_redirect(req.uri)
108
 
 
109
 
    # Present the HTML login page
110
 
    req.content_type = "text/html"
111
 
    req.title = "Login"
112
 
    req.write_html_head_foot = True
113
 
 
 
38
 
 
39
# XXX: Move this elsewhere, as it's just in storage now...
 
40
def tos_stuff():
114
41
    # User is not logged in or their account is not enabled.
115
42
    if user is not None:
116
43
        # Only possible if no errors occured thus far
139
66
            req.store.commit()
140
67
            req.throw_redirect(req.uri)
141
68
 
142
 
    # Write the HTML for the login page
143
 
    # If badlogin, display an error message indicating a failed login
144
 
    req.write("""<div id="ivle_padding">
145
 
<p>Welcome to the Informatics Virtual Learning Environment.
146
 
   Please log in to access your files and assessment.</p>
147
 
""")
148
 
    if badlogin is not None:
149
 
        req.write("""<p class="error">%s</p>
150
 
""" % badlogin)
151
 
    req.write("""<form action="" method="post">
152
 
  <table>
153
 
    <tr><td>Username:</td><td><input name="user" type="text" /></td></tr>
154
 
    <tr><td>Password:</td><td><input name="pass" type="password" /></td></tr>
155
 
    <tr><td colspan="2"><input type="submit" value="Login" /></td></tr>
156
 
  </table>
157
 
</form>
158
 
""")
159
 
    # Write the "Message of the Day" document, if it exists.
160
 
    try:
161
 
        req.sendfile(ivle.conf.motd_path)
162
 
    except IOError:
163
 
        pass
164
 
    req.write('</div>\n')
165
 
 
166
 
    return None
167
 
 
168
69
def get_user_details(req):
169
70
    """Gets the name of the logged in user, without presenting a login box
170
71
    or attempting to authenticate.