26
26
from mod_python import Session
29
from ivle import (util, db, caps, forumutil)
29
from ivle import (util, caps, forumutil)
30
30
from ivle.auth import authenticate, autherror
33
34
"""Determines whether the user is logged in or not (looking at sessions),
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)
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
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."
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:
79
if login_details is None:
80
81
# Must have got an error. Do not authenticate.
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."
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
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()
94
req.add_cookie(forumutil.make_forum_cookie(user))
94
95
req.throw_redirect(req.uri)
96
97
# Present the HTML login page
99
100
req.write_html_head_foot = True
101
102
# User is not logged in or their account is not enabled.
102
if login_details 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
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)
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()
125
db.DB().update_user(login_details.login, state='no_agreement')
126
user.state = u'no_agreement'
126
128
req.throw_redirect(req.uri)
128
130
# Write the HTML for the login page
159
161
session = req.get_session()
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.
165
return session['user']
165
login = session['login']
169
# Get the full User object from the db associated with this login
170
return ivle.database.User.get_by_login(req.store, login)
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