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

« back to all changes in this revision

Viewing changes to www/dispatch/login.py

  • Committer: mattgiuca
  • Date: 2008-02-15 05:43:52 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:475
Commited some earlier changes to users.sql (not committed earlier due to
misunderstanding :|) Previous changes today actually depended on the database
being updated with this schema.

users.sql: Added "pending" option to login.state, and added a bunch of new
fields to login: expiry timers, time of last login.

login.py: Checks if account has expired. Sets last login.
    (Currently buggy with some XXXs, because we don't have a way to convert
    Python time values into SQL).

db.py: Added last_login to list of allowable fields for login.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
# Provides services for checking logins and presenting the login page.
23
23
import os
 
24
import time
24
25
 
25
26
from mod_python import Session
26
27
 
27
28
from common import util
 
29
from common import db
28
30
from auth import authenticate
29
31
 
 
32
def has_expired(details, field):
 
33
    """Determines whether the given expiry field indicates that
 
34
       login should be denied.
 
35
    """
 
36
    return field in details     \
 
37
           and details[field]   \
 
38
           and time.localtime() > details[field]
 
39
 
30
40
def login(req):
31
41
    """Determines whether the user is logged in or not (looking at sessions),
32
42
    and if not, presents the login page. Returns a String username, or None
47
57
    # Check the session to see if someone is logged in. If so, go with it.
48
58
    # No security is required here. You must have already been authenticated
49
59
    # in order to get a 'login_name' variable in the session.
50
 
    try:
51
 
        if session['state'] == "enabled":
52
 
            # Only allow users to authenticate if their account is ENABLED
53
 
            return session['login_name']
54
 
    except KeyError:
55
 
        pass
 
60
    if 'state' in session and session['state'] == "enabled":
 
61
        # Only allow users to authenticate if their account is ENABLED
 
62
        return session['login_name']
56
63
 
57
64
    badlogin = None
58
65
    # Check if there is any postdata containing login information
71
78
                    authenticate.authenticate(username.value, password.value)
72
79
                if login_details is None:
73
80
                    badlogin = "Invalid username or password."
 
81
                elif has_expired(login_details, 'pass_exp'):
 
82
                    badlogin = "Your password has expired."
 
83
                elif has_expired(login_details, 'acct_exp'):
 
84
                    badlogin = "Your account has expired."
74
85
                else:
75
86
                    # Success - Set the session and redirect to avoid POSTDATA
76
87
                    session['login_name'] = username.value
82
93
                    session['rolenm'] = login_details['rolenm']
83
94
                    session['studentid'] = login_details['studentid']
84
95
                    session.save()
 
96
                    # XXX time.localtime() (a tuple of ints) is not valid for
 
97
                    # inserting as a TIMESTAMP in the DB.
 
98
                    #db.DB().update_user(username.value,
 
99
                    #                    last_login=time.localtime())
85
100
                    req.throw_redirect(req.uri)
86
101
 
87
102
    # Give a 403 Forbidden status, but present a full HTML login page
92
107
    req.write_html_head_foot = True
93
108
 
94
109
    # User is not logged in or their account is not enabled.
95
 
    if 'state' in session:
 
110
    if 'state' in session:      # Only possible if no errors occured thus far
96
111
        if session['state'] == "no_agreement":
97
112
            # User has authenticated but has not accepted the TOS.
98
113
            # Present them with the TOS page.
108
123
            return None
109
124
        elif session['state'] == "disabled":
110
125
            # User has authenticated but their account is disabled
111
 
            badlogin = "Can't log in: Your account has been disabled."
 
126
            badlogin = "Your account has been disabled."
112
127
    # Else, just fall through (failed to authenticate)
113
128
 
114
129
    # Write the HTML for the login page