1076
by chadnickbok
Created a new app, logout, which when given a GET |
1 |
# IVLE
|
2 |
# Copyright (C) 2007-2009 The University of Melbourne
|
|
3 |
#
|
|
4 |
# This program is free software; you can redistribute it and/or modify
|
|
5 |
# it under the terms of the GNU General Public License as published by
|
|
6 |
# the Free Software Foundation; either version 2 of the License, or
|
|
7 |
# (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU General Public License for more details.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU General Public License
|
|
15 |
# along with this program; if not, write to the Free Software
|
|
16 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
17 |
||
1099.1.41
by William Grant
Port www/apps/logout to new framework (in ivle.webapp.security). |
18 |
# Author: Will Grant, Nick Chadwick
|
19 |
||
1099.1.120
by William Grant
Move the login machinery to the new framework. |
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 |
|
1099.1.161
by William Grant
Move ivle.dispatch.login.get_user_details() to ivle.webapp.security. |
29 |
import ivle.webapp.security |
1099.1.120
by William Grant
Move the login machinery to the new framework. |
30 |
from ivle.auth import authenticate, AuthError |
1099.1.41
by William Grant
Port www/apps/logout to new framework (in ivle.webapp.security). |
31 |
from ivle.webapp.base.xhtml import XHTMLView |
1099.1.120
by William Grant
Move the login machinery to the new framework. |
32 |
from ivle.webapp.base.plugins import CookiePlugin |
33 |
||
34 |
class LoginView(XHTMLView): |
|
35 |
'''A view to allow a user to log in.'''
|
|
36 |
template = 'login.html' |
|
1099.1.129
by William Grant
Allow XHTML views to specify that they cannot have overlays. |
37 |
allow_overlays = False |
1099.1.120
by William Grant
Move the login machinery to the new framework. |
38 |
|
39 |
def authorize(self, req): |
|
40 |
return True |
|
41 |
||
42 |
def populate(self, req, ctx): |
|
43 |
fields = req.get_fieldstorage() |
|
44 |
nexturl = fields.getfirst('url') |
|
45 |
||
46 |
if nexturl is None: |
|
47 |
nexturl = '/' |
|
48 |
||
49 |
# We are already logged in. Don't bother logging in again.
|
|
1099.1.125
by William Grant
Move the pending/no_agreement handling into the new login machinery. |
50 |
# Note that req.user is None even if we are 'logged in', if the user is
|
51 |
# invalid.
|
|
1099.1.120
by William Grant
Move the login machinery to the new framework. |
52 |
if req.user is not None: |
53 |
req.throw_redirect(nexturl) |
|
54 |
||
55 |
ctx['path'] = ivle.util.make_path('+login') + \ |
|
56 |
'?' + urllib.urlencode([('url', nexturl)]) |
|
57 |
||
1099.1.125
by William Grant
Move the pending/no_agreement handling into the new login machinery. |
58 |
# If this succeeds, the user is invalid.
|
1099.1.161
by William Grant
Move ivle.dispatch.login.get_user_details() to ivle.webapp.security. |
59 |
user = ivle.webapp.security.get_user_details(req) |
1099.1.125
by William Grant
Move the pending/no_agreement handling into the new login machinery. |
60 |
if user is not None: |
61 |
if user.state == "no_agreement": |
|
62 |
# Authenticated, but need to accept the ToS. Send them there.
|
|
63 |
# IMPORTANT NOTE FOR HACKERS: You can't simply disable this
|
|
64 |
# if you are not planning to display a ToS page - the ToS
|
|
65 |
# acceptance process actually calls usrmgt to create the user
|
|
66 |
# jails and related stuff.
|
|
67 |
req.throw_redirect(ivle.util.make_path('+tos') + \ |
|
68 |
'?' + urllib.urlencode([('url', nexturl)])) |
|
69 |
elif user.state == "pending": |
|
70 |
# FIXME: this isn't quite the right answer, but it
|
|
71 |
# should be more robust in the short term.
|
|
72 |
session = req.get_session() |
|
73 |
session.invalidate() |
|
74 |
session.delete() |
|
75 |
user.state = u'no_agreement' |
|
76 |
req.store.commit() |
|
77 |
req.throw_redirect(nexturl) |
|
78 |
||
1099.1.120
by William Grant
Move the login machinery to the new framework. |
79 |
if req.method == "POST": |
80 |
# While req.user is normally set to get_user_details, it won't set
|
|
81 |
# it if the account isn't valid. So we get it ourselves.
|
|
1099.1.161
by William Grant
Move ivle.dispatch.login.get_user_details() to ivle.webapp.security. |
82 |
user = ivle.webapp.security.get_user_details(req) |
1099.1.120
by William Grant
Move the login machinery to the new framework. |
83 |
|
84 |
badlogin = None |
|
85 |
||
86 |
username = fields.getfirst('user') |
|
87 |
password = fields.getfirst('pass') |
|
88 |
if username is not None: |
|
89 |
# From this point onwards, we will be showing an error message
|
|
90 |
# if unsuccessful.
|
|
91 |
# Authenticate
|
|
92 |
if password is None: |
|
93 |
badlogin = "No password supplied." |
|
94 |
else: |
|
95 |
user = None |
|
96 |
try: |
|
97 |
user = authenticate.authenticate(req.store, |
|
98 |
username.value, password.value) |
|
99 |
except AuthError, msg: |
|
100 |
badlogin = msg |
|
101 |
if user is None: |
|
102 |
# Must have got an error. Do not authenticate.
|
|
1099.1.123
by William Grant
Don't crash when not authenticated, and display an error on password absence. |
103 |
# The except: above will have set a message.
|
1099.1.120
by William Grant
Move the login machinery to the new framework. |
104 |
pass
|
105 |
else: |
|
106 |
# Success - Set the session and redirect to the URL.
|
|
107 |
session = req.get_session() |
|
108 |
session['login'] = user.login |
|
109 |
session.save() |
|
110 |
user.last_login = datetime.datetime.now() |
|
111 |
req.store.commit() |
|
112 |
||
113 |
# Create cookies for plugins that might request them.
|
|
1092.1.59
by William Grant
Move the plugin loading/indexing logic into ivle.config.Config. |
114 |
for plugin in req.config.plugin_index[CookiePlugin]: |
1099.1.120
by William Grant
Move the login machinery to the new framework. |
115 |
for cookie in plugin.cookies: |
116 |
# The function can be None if they just need to be
|
|
117 |
# deleted at logout.
|
|
118 |
if plugin.cookies[cookie] is not None: |
|
119 |
req.add_cookie(mod_python.Cookie.Cookie(cookie, |
|
120 |
plugin.cookies[cookie](user), path='/')) |
|
121 |
||
122 |
req.throw_redirect(nexturl) |
|
123 |
||
1099.1.123
by William Grant
Don't crash when not authenticated, and display an error on password absence. |
124 |
# We didn't succeed.
|
125 |
# Render the login form with the error message.
|
|
126 |
ctx['error'] = badlogin |
|
1099.1.120
by William Grant
Move the login machinery to the new framework. |
127 |
|
1099.1.41
by William Grant
Port www/apps/logout to new framework (in ivle.webapp.security). |
128 |
|
129 |
class LogoutView(XHTMLView): |
|
130 |
'''A view to log the current session out.'''
|
|
131 |
template = 'logout.html' |
|
1099.1.129
by William Grant
Allow XHTML views to specify that they cannot have overlays. |
132 |
allow_overlays = False |
1099.1.41
by William Grant
Port www/apps/logout to new framework (in ivle.webapp.security). |
133 |
|
1099.1.110
by William Grant
Implement an authorization system in the new framework. This breaks the REST |
134 |
def authorize(self, req): |
1099.1.126
by William Grant
Allow any authenticated (even invalid) user to use the logout view. |
135 |
# This can be used by any authenticated user, even if they haven't
|
136 |
# accepted the ToS yet.
|
|
1099.1.161
by William Grant
Move ivle.dispatch.login.get_user_details() to ivle.webapp.security. |
137 |
return ivle.webapp.security.get_user_details(req) is not None |
1099.1.110
by William Grant
Implement an authorization system in the new framework. This breaks the REST |
138 |
|
1099.1.41
by William Grant
Port www/apps/logout to new framework (in ivle.webapp.security). |
139 |
def populate(self, req, ctx): |
140 |
if req.method == "POST": |
|
141 |
req.logout() |
|
142 |
else: |
|
1099.1.120
by William Grant
Move the login machinery to the new framework. |
143 |
ctx['path'] = ivle.util.make_path('+logout') |