25
25
# This needs to be importable from outside Apache.
28
import ivle.pulldown_subj
29
import ivle.webapp.security
29
import ivle.dispatch.login
30
30
from ivle.auth import authenticate, AuthError
31
31
from ivle.webapp.base.xhtml import XHTMLView
32
32
from ivle.webapp.base.plugins import CookiePlugin
33
from ivle.dispatch.login import get_user_details
34
35
class LoginView(XHTMLView):
35
36
'''A view to allow a user to log in.'''
43
44
fields = req.get_fieldstorage()
44
45
nexturl = fields.getfirst('url')
46
# XXX Warning that Internet Explorer is unsupported
47
# Test if the user is in Internet Explorer
49
useragent = req.headers_in['User-Agent']
50
# A bit of very basic UA string detection
51
ctx['msie'] = ('MSIE' in useragent
52
and 'AppleWebKit' not in useragent
53
and 'Gecko' not in useragent
54
and 'Opera' not in useragent)
58
47
if nexturl is None:
61
# We are already logged in. If it is a POST, they might be trying to
62
# clobber their session with some new credentials. That's their own
63
# business, so we let them do it. Otherwise, we don't bother prompting
64
# and just redirect to the destination.
50
# We are already logged in. Don't bother logging in again.
65
51
# Note that req.user is None even if we are 'logged in', if the user is
66
# invalid (state != enabled, or expired).
67
if req.method != "POST" and req.user is not None:
53
if req.user is not None:
68
54
req.throw_redirect(nexturl)
70
# Don't give any URL if we want /.
74
query_string = '?url=' + urllib.quote(nexturl, safe="/~")
76
ctx['path'] = req.make_path('+login') + query_string
56
ctx['path'] = ivle.util.make_path('+login') + \
57
'?' + urllib.urlencode([('url', nexturl)])
78
59
# If this succeeds, the user is invalid.
79
user = ivle.webapp.security.get_user_details(req)
60
user = get_user_details(req)
80
61
if user is not None:
81
62
if user.state == "no_agreement":
82
63
# Authenticated, but need to accept the ToS. Send them there.
84
65
# if you are not planning to display a ToS page - the ToS
85
66
# acceptance process actually calls usrmgt to create the user
86
67
# jails and related stuff.
87
req.throw_redirect(req.make_path('+tos') + query_string)
68
req.throw_redirect(ivle.util.make_path('+tos') + \
69
'?' + urllib.urlencode([('url', nexturl)]))
88
70
elif user.state == "pending":
89
71
# FIXME: this isn't quite the right answer, but it
90
72
# should be more robust in the short term.
98
80
if req.method == "POST":
99
81
# While req.user is normally set to get_user_details, it won't set
100
82
# it if the account isn't valid. So we get it ourselves.
101
user = ivle.webapp.security.get_user_details(req)
83
user = get_user_details(req)
116
# Username is case insensitive
117
user = authenticate.authenticate(req.config, req.store,
118
username.value.lower(), password.value)
98
user = authenticate.authenticate(req.store,
99
username.value, password.value)
119
100
except AuthError, msg:
127
108
session = req.get_session()
128
109
session['login'] = user.login
131
111
user.last_login = datetime.datetime.now()
133
114
# Create cookies for plugins that might request them.
134
for plugin in req.config.plugin_index[CookiePlugin]:
115
for plugin in req.plugin_index[CookiePlugin]:
135
116
for cookie in plugin.cookies:
136
117
# The function can be None if they just need to be
137
118
# deleted at logout.
139
120
req.add_cookie(mod_python.Cookie.Cookie(cookie,
140
121
plugin.cookies[cookie](user), path='/'))
142
# Add any new enrolments.
143
ivle.pulldown_subj.enrol_user(req.config, req.store, user)
146
123
req.throw_redirect(nexturl)
148
125
# We didn't succeed.
158
135
def authorize(self, req):
159
136
# This can be used by any authenticated user, even if they haven't
160
137
# accepted the ToS yet.
161
return ivle.webapp.security.get_user_details(req) is not None
138
return ivle.dispatch.login.get_user_details(req) is not None
163
140
def populate(self, req, ctx):
164
141
if req.method == "POST":
167
ctx['path'] = req.make_path('+logout')
144
ctx['path'] = ivle.util.make_path('+logout')