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

« back to all changes in this revision

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

Quick port of fileservice to the new framework. It's still very much old-style,
though.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
    pass
27
27
 
28
28
import ivle.util
29
 
import ivle.pulldown_subj
30
 
import ivle.webapp.security
 
29
import ivle.dispatch.login
31
30
from ivle.auth import authenticate, AuthError
32
31
from ivle.webapp.base.xhtml import XHTMLView
33
32
from ivle.webapp.base.plugins import CookiePlugin
 
33
from ivle.dispatch.login import get_user_details
34
34
 
35
35
class LoginView(XHTMLView):
36
36
    '''A view to allow a user to log in.'''
47
47
        if nexturl is None:
48
48
            nexturl = '/'
49
49
 
50
 
        # We are already logged in. If it is a POST, they might be trying to
51
 
        # clobber their session with some new credentials. That's their own
52
 
        # business, so we let them do it. Otherwise, we don't bother prompting
53
 
        # and just redirect to the destination.
 
50
        # We are already logged in. Don't bother logging in again.
54
51
        # Note that req.user is None even if we are 'logged in', if the user is
55
 
        # invalid (state != enabled, or expired).
56
 
        if req.method != "POST" and req.user is not None:
 
52
        # invalid.
 
53
        if req.user is not None:
57
54
            req.throw_redirect(nexturl)
58
55
 
59
 
        # Don't give any URL if we want /.
60
 
        if nexturl == '/':
61
 
            query_string = ''
62
 
        else:
63
 
            query_string = '?url=' + urllib.quote(nexturl, safe="/~")
64
 
 
65
 
        ctx['path'] = ivle.util.make_path('+login') + query_string
 
56
        ctx['path'] = ivle.util.make_path('+login') + \
 
57
                         '?' + urllib.urlencode([('url', nexturl)])
66
58
 
67
59
        # If this succeeds, the user is invalid.
68
 
        user = ivle.webapp.security.get_user_details(req)
 
60
        user = get_user_details(req)
69
61
        if user is not None:
70
62
            if user.state == "no_agreement":
71
63
                # Authenticated, but need to accept the ToS. Send them there.
73
65
                # if you are not planning to display a ToS page - the ToS
74
66
                # acceptance process actually calls usrmgt to create the user
75
67
                # jails and related stuff.
76
 
                req.throw_redirect(ivle.util.make_path('+tos') + query_string)
 
68
                req.throw_redirect(ivle.util.make_path('+tos') + \
 
69
                        '?' + urllib.urlencode([('url', nexturl)]))
77
70
            elif user.state == "pending":
78
71
                # FIXME: this isn't quite the right answer, but it
79
72
                # should be more robust in the short term.
87
80
        if req.method == "POST":
88
81
            # While req.user is normally set to get_user_details, it won't set
89
82
            # it if the account isn't valid. So we get it ourselves.
90
 
            user = ivle.webapp.security.get_user_details(req)
 
83
            user = get_user_details(req)
91
84
 
92
85
            badlogin = None
93
86
 
115
108
                        session = req.get_session()
116
109
                        session['login'] = user.login
117
110
                        session.save()
118
 
                        session.unlock()
119
111
                        user.last_login = datetime.datetime.now()
 
112
                        req.store.commit()
120
113
 
121
114
                        # Create cookies for plugins that might request them.
122
115
                        for plugin in req.config.plugin_index[CookiePlugin]:
127
120
                                    req.add_cookie(mod_python.Cookie.Cookie(cookie,
128
121
                                          plugin.cookies[cookie](user), path='/'))
129
122
 
130
 
                        # Add any new enrolments.
131
 
                        ivle.pulldown_subj.enrol_user(req.store, user)
132
 
                        req.store.commit()
133
 
 
134
123
                        req.throw_redirect(nexturl)
135
124
 
136
125
                # We didn't succeed.
146
135
    def authorize(self, req):
147
136
        # This can be used by any authenticated user, even if they haven't
148
137
        # accepted the ToS yet.
149
 
        return ivle.webapp.security.get_user_details(req) is not None
 
138
        return ivle.dispatch.login.get_user_details(req) is not None
150
139
 
151
140
    def populate(self, req, ctx):
152
141
        if req.method == "POST":