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

« back to all changes in this revision

Viewing changes to ivle/webapp/security/views.py

Move the login machinery to the new framework.

The login page is now redirected to by the XHTML Unauthorized page, and knows
to return to the right URL when authentication succeeds. It also means that we
no longer return a 200 with login page content for a totally unrelated page...

Some of the user stuff (disabling, Tos, etc.) is now broken, and /logout moved
to /+logout.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
# Author: Will Grant, Nick Chadwick
19
19
 
 
20
import urllib
 
21
import datetime
 
22
try:
 
23
    import mod_python.Cookie
 
24
except ImportError:
 
25
    # This needs to be importable from outside Apache.
 
26
    pass
 
27
 
 
28
import ivle.util
 
29
from ivle.auth import authenticate, AuthError
20
30
from ivle.webapp.base.xhtml import XHTMLView
21
 
import ivle.util
 
31
from ivle.webapp.base.plugins import CookiePlugin
 
32
from ivle.dispatch.login import get_user_details
 
33
 
 
34
class LoginView(XHTMLView):
 
35
    '''A view to allow a user to log in.'''
 
36
    template = 'login.html'
 
37
 
 
38
    def authorize(self, req):
 
39
        return True
 
40
 
 
41
    def populate(self, req, ctx):
 
42
        fields = req.get_fieldstorage()
 
43
        nexturl = fields.getfirst('url')
 
44
 
 
45
        if nexturl is None:
 
46
            nexturl = '/'
 
47
 
 
48
        # We are already logged in. Don't bother logging in again.
 
49
        if req.user is not None:
 
50
            req.throw_redirect(nexturl)
 
51
 
 
52
        ctx['path'] = ivle.util.make_path('+login') + \
 
53
                         '?' + urllib.urlencode([('url', nexturl)])
 
54
 
 
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)
 
59
 
 
60
            badlogin = None
 
61
 
 
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
 
66
                # if unsuccessful.
 
67
                # Authenticate
 
68
                if password is None:
 
69
                    badlogin = "No password supplied."
 
70
                else:
 
71
                    user = None
 
72
                    try:
 
73
                        user = authenticate.authenticate(req.store,
 
74
                                    username.value, password.value)
 
75
                    except AuthError, msg:
 
76
                        badlogin = msg
 
77
                    if user is None:
 
78
                        # Must have got an error. Do not authenticate.
 
79
                        pass
 
80
                    elif user.password_expired:
 
81
                        badlogin = "Your password has expired."
 
82
                    elif user.account_expired:
 
83
                        badlogin = "Your account has expired."
 
84
                    else:
 
85
                        # Success - Set the session and redirect to the URL.
 
86
                        session = req.get_session()
 
87
                        session['login'] = user.login
 
88
                        session.save()
 
89
                        user.last_login = datetime.datetime.now()
 
90
                        req.store.commit()
 
91
 
 
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
 
96
                                # deleted at logout.
 
97
                                if plugin.cookies[cookie] is not None:
 
98
                                    req.add_cookie(mod_python.Cookie.Cookie(cookie,
 
99
                                          plugin.cookies[cookie](user), path='/'))
 
100
 
 
101
                        req.throw_redirect(nexturl)
 
102
 
 
103
                    # We didn't succeed.
 
104
                    # Render the login form with the error message.
 
105
                    ctx['error'] = badlogin
 
106
 
22
107
 
23
108
class LogoutView(XHTMLView):
24
109
    '''A view to log the current session out.'''
31
116
        if req.method == "POST":
32
117
            req.logout()
33
118
        else:
34
 
            ctx['path'] =  ivle.util.make_path('logout')
 
119
            ctx['path'] =  ivle.util.make_path('+logout')