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

« back to all changes in this revision

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

  • Committer: William Grant
  • Date: 2009-02-17 05:06:35 UTC
  • mto: (1099.1.143 new-dispatch)
  • mto: This revision was merged to the branch mainline in revision 1100.
  • Revision ID: grantw@unimelb.edu.au-20090217050635-9u85hgrreg9flp2h
Remove interpretservice, and clean up ivle.webapp.filesystem.serve.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
    # This needs to be importable from outside Apache.
26
26
    pass
27
27
 
28
 
import ivle.pulldown_subj
29
 
import ivle.webapp.security
 
28
import ivle.util
 
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
33
34
 
34
35
class LoginView(XHTMLView):
35
36
    '''A view to allow a user to log in.'''
46
47
        if nexturl is None:
47
48
            nexturl = '/'
48
49
 
49
 
        # We are already logged in. If it is a POST, they might be trying to
50
 
        # clobber their session with some new credentials. That's their own
51
 
        # business, so we let them do it. Otherwise, we don't bother prompting
52
 
        # and just redirect to the destination.
 
50
        # We are already logged in. Don't bother logging in again.
53
51
        # Note that req.user is None even if we are 'logged in', if the user is
54
 
        # invalid (state != enabled, or expired).
55
 
        if req.method != "POST" and req.user is not None:
 
52
        # invalid.
 
53
        if req.user is not None:
56
54
            req.throw_redirect(nexturl)
57
55
 
58
 
        # Don't give any URL if we want /.
59
 
        if nexturl == '/':
60
 
            query_string = ''
61
 
        else:
62
 
            query_string = '?url=' + urllib.quote(nexturl, safe="/~")
63
 
 
64
 
        ctx['path'] = req.make_path('+login') + query_string
 
56
        ctx['path'] = ivle.util.make_path('+login') + \
 
57
                         '?' + urllib.urlencode([('url', nexturl)])
65
58
 
66
59
        # If this succeeds, the user is invalid.
67
 
        user = ivle.webapp.security.get_user_details(req)
 
60
        user = get_user_details(req)
68
61
        if user is not None:
69
62
            if user.state == "no_agreement":
70
63
                # Authenticated, but need to accept the ToS. Send them there.
72
65
                # if you are not planning to display a ToS page - the ToS
73
66
                # acceptance process actually calls usrmgt to create the user
74
67
                # jails and related stuff.
75
 
                req.throw_redirect(req.make_path('+tos') + query_string)
 
68
                req.throw_redirect(ivle.util.make_path('+tos') + \
 
69
                        '?' + urllib.urlencode([('url', nexturl)]))
76
70
            elif user.state == "pending":
77
71
                # FIXME: this isn't quite the right answer, but it
78
72
                # should be more robust in the short term.
86
80
        if req.method == "POST":
87
81
            # While req.user is normally set to get_user_details, it won't set
88
82
            # it if the account isn't valid. So we get it ourselves.
89
 
            user = ivle.webapp.security.get_user_details(req)
 
83
            user = get_user_details(req)
90
84
 
91
85
            badlogin = None
92
86
 
101
95
                else:
102
96
                    user = None
103
97
                    try:
104
 
                        # Username is case insensitive
105
 
                        user = authenticate.authenticate(req.config, req.store,
106
 
                                    username.value.lower(), password.value)
 
98
                        user = authenticate.authenticate(req.store,
 
99
                                    username.value, password.value)
107
100
                    except AuthError, msg:
108
101
                        badlogin = msg
109
102
                    if user is None:
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
 
                        for plugin in req.config.plugin_index[CookiePlugin]:
 
115
                        for plugin in req.plugin_index[CookiePlugin]:
123
116
                            for cookie in plugin.cookies:
124
117
                                # The function can be None if they just need to be
125
118
                                # deleted at logout.
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.config, 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":
153
142
            req.logout()
154
143
        else:
155
 
            ctx['path'] =  req.make_path('+logout')
 
144
            ctx['path'] =  ivle.util.make_path('+logout')