From abf310d70377ce24edba54180804654301f971a8 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 23 May 2012 18:38:39 +0200 Subject: Redirect to login page and back to the original page afterwards when login is required. --- fbin.py | 13 ++++++++----- templates/login.tmpl | 3 +++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/fbin.py b/fbin.py index 46845ea..8a77738 100755 --- a/fbin.py +++ b/fbin.py @@ -284,12 +284,14 @@ class Application(object): c = Cookie.SimpleCookie(environ['HTTP_COOKIE'] if 'HTTP_COOKIE' in environ else None) user = self.validate_cookie(c) form = cgi.FieldStorage(fp = environ['wsgi.input'], environ = environ) + next = form.getvalue('next') if environ['REQUEST_METHOD'] != 'POST' or not 'username' in form or not 'password' in form: start_response('200 OK', [('Content-Type', 'text/html')]) return str(templates.login(searchList = { 'root': settings.virtual_root, 'user': user, 'error': None, + 'next': next, })) username = form.getvalue('username') @@ -303,6 +305,7 @@ class Application(object): 'root': settings.virtual_root, 'user': user, 'error': 'Login failed', + 'next': next, })) c = Cookie.SimpleCookie() @@ -315,7 +318,7 @@ class Application(object): c['identifier']['expires'] = expires start_response('302 Found', [ - ('Location', settings.virtual_root + 'u'), + ('Location', next if next else (settings.virtual_root + 'u')), ('Set-Cookie', c['uid'].OutputString()), ('Set-Cookie', c['identifier'].OutputString())]) return [] @@ -439,8 +442,8 @@ class Application(object): c = Cookie.SimpleCookie(environ['HTTP_COOKIE'] if 'HTTP_COOKIE' in environ else None) user = self.validate_cookie(c) if user == None: - start_response('200 OK', [('Content-Type', 'text/html')]) - return ['Not logged in.'] + start_response('302 Found', [('Location', settings.virtual_root + 'l?' + urllib.urlencode({'next': settings.virtual_root + 'm'}))]) + return [] files = self.get_files(user) start_response('200 OK', [('Content-Type', 'text/html')]) return str(templates.my(searchList = { @@ -454,8 +457,8 @@ class Application(object): c = Cookie.SimpleCookie(environ['HTTP_COOKIE'] if 'HTTP_COOKIE' in environ else None) user = self.validate_cookie(c) if user == None: - start_response('200 OK', [('Content-Type', 'text/html')]) - return ['Not logged in.'] + start_response('302 Found', [('Location', settings.virtual_root + 'l?' + urllib.urlencode({'next': settings.virtual_root + 'i'}))]) + return [] files = [f for f in self.get_files(user) if f.is_image()] start_response('200 OK', [('Content-Type', 'text/html')]) return str(templates.images(searchList = { diff --git a/templates/login.tmpl b/templates/login.tmpl index d8aaded..7638c07 100644 --- a/templates/login.tmpl +++ b/templates/login.tmpl @@ -5,6 +5,9 @@ #set error = $error or ''
$error
+#if next + +#end if

username

password

-- cgit v1.2.3