18
18
# Author: Will Grant, Nick Chadwick
23
import mod_python.Cookie
25
# This needs to be importable from outside Apache.
29
from ivle.auth import authenticate, AuthError
20
30
from ivle.webapp.base.xhtml import XHTMLView
31
from ivle.webapp.base.plugins import CookiePlugin
32
from ivle.dispatch.login import get_user_details
34
class LoginView(XHTMLView):
35
'''A view to allow a user to log in.'''
36
template = 'login.html'
38
def authorize(self, req):
41
def populate(self, req, ctx):
42
fields = req.get_fieldstorage()
43
nexturl = fields.getfirst('url')
48
# We are already logged in. Don't bother logging in again.
49
if req.user is not None:
50
req.throw_redirect(nexturl)
52
ctx['path'] = ivle.util.make_path('+login') + \
53
'?' + urllib.urlencode([('url', nexturl)])
55
if req.method == "POST":
56
# While req.user is normally set to get_user_details, it won't set
57
# it if the account isn't valid. So we get it ourselves.
58
user = get_user_details(req)
62
username = fields.getfirst('user')
63
password = fields.getfirst('pass')
64
if username is not None:
65
# From this point onwards, we will be showing an error message
69
badlogin = "No password supplied."
73
user = authenticate.authenticate(req.store,
74
username.value, password.value)
75
except AuthError, msg:
78
# Must have got an error. Do not authenticate.
80
elif user.password_expired:
81
badlogin = "Your password has expired."
82
elif user.account_expired:
83
badlogin = "Your account has expired."
85
# Success - Set the session and redirect to the URL.
86
session = req.get_session()
87
session['login'] = user.login
89
user.last_login = datetime.datetime.now()
92
# Create cookies for plugins that might request them.
93
for plugin in req.plugin_index[CookiePlugin]:
94
for cookie in plugin.cookies:
95
# The function can be None if they just need to be
97
if plugin.cookies[cookie] is not None:
98
req.add_cookie(mod_python.Cookie.Cookie(cookie,
99
plugin.cookies[cookie](user), path='/'))
101
req.throw_redirect(nexturl)
104
# Render the login form with the error message.
105
ctx['error'] = badlogin
23
108
class LogoutView(XHTMLView):
24
109
'''A view to log the current session out.'''