Skip to content

Commit

Permalink
adding a redirected to the originaly requested page on login
Browse files Browse the repository at this point in the history
user not authenticated and requesting an url are redirected to
the signin page with the requested page url as the query string (in
base64). It permits to redirect them to the requested page once they
've log in.
  • Loading branch information
kakwa committed Jul 6, 2015
1 parent 7241b6f commit 1f0cc50
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
32 changes: 23 additions & 9 deletions ldapcherry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import logging.handlers
from operator import itemgetter
from socket import error as socket_error
import base64

from exceptions import *
from ldapcherry.roles import Roles
Expand Down Expand Up @@ -475,12 +476,18 @@ def _check_auth(self, must_admin):
if self.auth_mode == 'none':
return 'anonymous'
username = cherrypy.session.get(SESSION_KEY)

if cherrypy.request.query_string == '':
qs = ''
else:
qs = '?' + cherrypy.request.query_string
b64requrl = base64.b64encode(cherrypy.url() + qs)
if not username:
raise cherrypy.HTTPRedirect("/signin")
raise cherrypy.HTTPRedirect("/signin?url=%(url)s" % {'url': b64requrl})

if not 'connected' in cherrypy.session \
or not cherrypy.session['connected']:
raise cherrypy.HTTPRedirect("/signin")
raise cherrypy.HTTPRedirect("/signin?url=%(url)s" % {'url': b64requrl})
if cherrypy.session['connected'] and \
not cherrypy.session['isadmin']:
if must_admin:
Expand All @@ -492,8 +499,7 @@ def _check_auth(self, must_admin):
cherrypy.session['isadmin']:
return username
else:
raise cherrypy.HTTPRedirect("/signin")

raise cherrypy.HTTPRedirect("/signin?url=%(url)s" % {'url': b64requrl})

def _adduser(self, params):
cherrypy.log.error(
Expand Down Expand Up @@ -700,13 +706,13 @@ def _checkppolicy(self, password):
return ret

@cherrypy.expose
def signin(self):
def signin(self, url=None):
"""simple signin page
"""
return self.temp_login.render()
return self.temp_login.render(url=url)

@cherrypy.expose
def login(self, login, password):
def login(self, login, password, url=None):
"""login page
"""
auth = self._auth(login, password)
Expand All @@ -727,7 +733,11 @@ def login(self, login, password):
severity = logging.INFO
)
cherrypy.session[SESSION_KEY] = cherrypy.request.login = login
raise cherrypy.HTTPRedirect("/")
if url is None:
redirect = "/"
else:
redirect = base64.b64decode(url)
raise cherrypy.HTTPRedirect(redirect)
else:
message = "login failed for user '%(user)s'" % {
'user': login
Expand All @@ -736,7 +746,11 @@ def login(self, login, password):
msg = message,
severity = logging.WARNING
)
raise cherrypy.HTTPRedirect("/signin")
if url is None:
qs = ''
else:
qs = '?url=' + url
raise cherrypy.HTTPRedirect("/signin" + qs)

@cherrypy.expose
def logout(self):
Expand Down
8 changes: 7 additions & 1 deletion resources/templates/login.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
<div class="row clearfix" style="margin-top:30px">
<div class="col-md-4 column"></div>
<div class="col-md-4 column well">
<form method='POST' action='/login' role="form" class="form-signin">
<%
if url is None:
qs=''
else:
qs='?url=' + url
%>
<form method='POST' action='/login${qs}' role="form" class="form-signin">
<div class="form-group">
<h2 class="form-signin-heading">Please sign in</h2>
<div class="input-group">
Expand Down

0 comments on commit 1f0cc50

Please sign in to comment.