Changeset - b98f4431671c
[Not reviewed]
stable
0 2 0
Mads Kiilerich - 10 years ago 2015-09-20 22:22:50
madski@unity3d.com
login: inline _redirect_to_origin

Refactor to simplify code and make next changes simpler.

Let the controller handle came_from early and more explicit, thus simplifying
the redirect flow.
2 files changed with 8 insertions and 11 deletions:
0 comments (0 inline, 0 general)
kallithea/controllers/login.py
Show inline comments
 
@@ -71,28 +71,25 @@ class LoginController(BaseController):
 
        if server_parsed.netloc != parsed.netloc:
 
            log.error('Suspicious NETLOC detected %s for url %s server url '
 
                      'is: %s' % (parsed.netloc, parsed, server_parsed))
 
            return False
 
        return True
 

	
 
    def _redirect_to_origin(self, origin):
 
        '''redirect to the original page, preserving any get arguments given'''
 
        request.GET.pop('came_from', None)
 
        raise HTTPFound(location=url(origin, **request.GET))
 

	
 
    def index(self):
 
        c.came_from = safe_str(request.GET.get('came_from', ''))
 
        if not self._validate_came_from(c.came_from):
 
            c.came_from = url('home')
 
        c.came_from = safe_str(request.GET.pop('came_from', ''))
 
        if self._validate_came_from(c.came_from):
 
            came_from = url(c.came_from, **request.GET)
 
        else:
 
            c.came_from = came_from = url('home')
 

	
 
        not_default = self.authuser.username != User.DEFAULT_USER
 
        ip_allowed = AuthUser.check_ip_allowed(self.authuser, self.ip_addr)
 

	
 
        # redirect if already logged in
 
        if self.authuser.is_authenticated and not_default and ip_allowed:
 
            return self._redirect_to_origin(c.came_from)
 
            raise HTTPFound(location=came_from)
 

	
 
        if request.POST:
 
            # import Login Form validator class
 
            login_form = LoginForm()
 
            try:
 
                c.form_result = login_form.to_python(dict(request.POST))
 
@@ -116,13 +113,13 @@ class LoginController(BaseController):
 
                # with user creation, explanation should be provided in
 
                # Exception itself
 
                h.flash(e, 'error')
 
            else:
 
                log_in_user(user, c.form_result['remember'],
 
                    is_external_auth=False)
 
                return self._redirect_to_origin(c.came_from)
 
                raise HTTPFound(location=came_from)
 

	
 
        return render('/login.html')
 

	
 
    @HasPermissionAnyDecorator('hg.admin', 'hg.register.auto_activate',
 
                               'hg.register.manual_activate')
 
    def register(self):
kallithea/templates/login.html
Show inline comments
 
@@ -13,13 +13,13 @@
 
            <h5>${_('Log In to %s') % c.site_name}</h5>
 
        %else:
 
            <h5>${_('Log In')}</h5>
 
        %endif
 
    </div>
 
    <div class="panel-body inner">
 
        ${h.form(h.url.current(**request.GET))}
 
        ${h.form(h.url.current(came_from=c.came_from, **request.GET))}
 
        <div class="form">
 
            <i class="icon-lock"></i>
 
            <!-- fields -->
 

	
 
            <div class="form-horizontal">
 
                <div class="form-group">
0 comments (0 inline, 0 general)