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

« back to all changes in this revision

Viewing changes to ivle/dispatch/login.py

  • Committer: matt.giuca
  • Date: 2009-01-14 10:10:12 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:branches%2Fstorm:1132
The new ivle.database.User class is now used in Request and usrmgt, which
    means it is now almost universally used in favour of ivle.user.User (now
    deprecated).

Noticeable change: The minor bug where the change to a user object in the
    database is not reflected in the user's session (eg. changing nick doesn't
    update title until log out).

ivle.dispatch:
    Session now contains 'login' (username string) rather than 'user' (full
        ivle.user.User object). This is a unicode string now.

    req.user is now a ivle.database.User object rather than an ivle.user.User
        object. This makes for a whole lot of really subtle differences, but
        largely conforms to the same interface. Note that strings must now all
        be unicode.

    login: Removed use of ivle.db. Now uses User object.

    html: Now handles unicode login and config options.

ivle.db: Removed update_user. Now replaced with Storm model.

ivle.database: Renamed has_cap back to hasCap (saved for later). Fixed small
    unicode bug.

ivle.makeuser.make_svn_auth now takes a store object.

usrmgt-server: Use new User class.

userservice: Now uses User class internally.
    get_user action now returns ISO 8601 date format, rather than a
        time tuple. (Wasn't being used).
    get_user action no longer transmits local_password (small security risk;
        note that it wasn't possible to see this for any user other than
        yourself unless admin).

ivle.util - added function object_to_dict.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
from mod_python import Session
27
27
 
28
28
import ivle.conf
29
 
from ivle import (util, db, caps, forumutil)
 
29
from ivle import (util, caps, forumutil)
30
30
from ivle.auth import authenticate, autherror
 
31
import ivle.database
31
32
 
32
33
def login(req):
33
34
    """Determines whether the user is logged in or not (looking at sessions),
46
47
    """
47
48
    # Get the user details from the session, if already logged in
48
49
    # (None means not logged in yet)
49
 
    login_details = get_user_details(req)
 
50
    user = get_user_details(req)
50
51
 
51
52
    # Check the session to see if someone is logged in. If so, go with it.
52
53
    # No security is required here. You must have already been authenticated
53
54
    # in order to get a 'login_name' variable in the session.
54
 
    if login_details is not None and login_details.state == "enabled":
 
55
    if user is not None and user.state == "enabled":
55
56
        # Only allow users to authenticate if their account is ENABLED
56
 
        return login_details
 
57
        return user
57
58
 
58
59
    badlogin = None
59
60
 
60
61
    # Check if there is any postdata containing login information
61
 
    if login_details is None and req.method == 'POST':
 
62
    if user is None and req.method == 'POST':
62
63
        fields = req.get_fieldstorage()
63
64
        username = fields.getfirst('user')
64
65
        password = fields.getfirst('pass')
70
71
                badlogin = "No password supplied."
71
72
            else:
72
73
                try:
73
 
                    login_details = \
 
74
                    user = \
74
75
                        authenticate.authenticate(username.value, password.value)
75
76
                # NOTE: Can't catch AuthError, since each module throws a
76
77
                # different identity of AuthError.
77
78
                except Exception, msg:
78
79
                    badlogin = msg
79
 
                if login_details is None:
 
80
                if user is None:
80
81
                    # Must have got an error. Do not authenticate.
81
82
                    pass
82
 
                elif login_details.pass_expired():
 
83
                elif user.pass_expired():
83
84
                    badlogin = "Your password has expired."
84
 
                elif login_details.acct_expired():
 
85
                elif user.acct_expired():
85
86
                    badlogin = "Your account has expired."
86
87
                else:
87
88
                    # Success - Set the session and redirect to avoid POSTDATA
88
89
                    session = req.get_session()
89
 
                    session['user'] = login_details
 
90
                    session['login'] = user.login
90
91
                    session.save()
91
 
                    db.DB().update_user(username.value,
92
 
                                        last_login = time.localtime())
93
 
                    req.add_cookie(forumutil.make_forum_cookie(login_details))
 
92
                    user.last_login = time.localtime()
 
93
                    req.store.commit()
 
94
                    req.add_cookie(forumutil.make_forum_cookie(user))
94
95
                    req.throw_redirect(req.uri)
95
96
 
96
97
    # Present the HTML login page
99
100
    req.write_html_head_foot = True
100
101
 
101
102
    # User is not logged in or their account is not enabled.
102
 
    if login_details is not None:
 
103
    if user is not None:
103
104
        # Only possible if no errors occured thus far
104
 
        if login_details.state == "no_agreement":
 
105
        if user.state == "no_agreement":
105
106
            # User has authenticated but has not accepted the TOS.
106
107
            # Present them with the TOS page.
107
108
            # First set their username for display at the top, but make sure
108
109
            # the apps tabs are not displayed
109
 
            req.user = login_details
 
110
            req.user = user
110
111
            # IMPORTANT NOTE FOR HACKERS: You can't simply disable this check
111
112
            # if you are not planning to display a TOS page - the TOS
112
113
            # acceptance process actually calls usermgt to create the user
113
114
            # jails and related stuff.
114
 
            present_tos(req, login_details.fullname)
 
115
            present_tos(req, user.fullname)
115
116
            return None
116
 
        elif login_details.state == "disabled":
 
117
        elif user.state == "disabled":
117
118
            # User has authenticated but their account is disabled
118
119
            badlogin = "Your account has been disabled."
119
 
        elif login_details.state == "pending":
 
120
        elif user.state == "pending":
120
121
            # FIXME: this isn't quite the right answer, but it
121
122
            # should be more robust in the short term.
122
123
            session = req.get_session()
123
124
            session.invalidate()
124
125
            session.delete()
125
 
            db.DB().update_user(login_details.login, state='no_agreement')
 
126
            user.state = u'no_agreement'
 
127
            req.store.commit()
126
128
            req.throw_redirect(req.uri)
127
129
 
128
130
    # Write the HTML for the login page
159
161
    session = req.get_session()
160
162
 
161
163
    # Check the session to see if someone is logged in. If so, go with it.
162
 
    # No security is required here. You must have already been authenticated
163
 
    # in order to get a 'login_name' variable in the session.
164
164
    try:
165
 
        return session['user']
 
165
        login = session['login']
166
166
    except KeyError:
167
167
        return None
168
168
 
 
169
    # Get the full User object from the db associated with this login
 
170
    return ivle.database.User.get_by_login(req.store, login)
 
171
 
169
172
def present_tos(req, fullname):
170
173
    """Present the Terms of Service screen to the user (who has just logged in
171
174
    for the first time and needs to accept these before being admitted into