Skip to content

Commit

Permalink
ActionController: JS should redirect to __SELF__...
Browse files Browse the repository at this point in the history
...once you lost your session. That's the only way to do this in a
smooth way. When the server get's an unauthenticated request for a
single container, redirecting to auth/login?redir=<that-container>
was not what you expect.

Together with JS handling __SELF__ in redirection URLs this will
play fine.

refs #6935
  • Loading branch information
Thomas-Gelf committed Aug 19, 2014
1 parent b9cc964 commit 9d4a4f4
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions library/Icinga/Web/Controller/ActionController.php
Expand Up @@ -256,8 +256,20 @@ public function disableAutoRefresh()
*/
protected function redirectToLogin($afterLogin = '/dashboard')
{
if (! $afterLogin instanceof Url) {
$afterLogin = Url::fromPath($afterLogin);
}
if ($this->isXhr()) {
$redir = '__SELF__';
} else {
// TODO: Ignore /?
$redir = $afterLogin->getRelativeUrl();
}
$url = Url::fromPath('authentication/login');
$url->setParam('redirect', $afterLogin);
if ($redir) {
$url->setParam('redirect', $redir);
}

$this->rerenderLayout()->redirectNow($url);
}

Expand All @@ -273,33 +285,41 @@ public function isXhr()
return $this->getRequest()->isXmlHttpRequest();
}

protected function redirectXhr($url)
{
if (! $url instanceof Url) {
$url = Url::fromPath($url);
}

if ($this->rerenderLayout) {
$this->getResponse()->setHeader('X-Icinga-Rerender-Layout', 'yes');
}
if ($this->reloadCss) {
$this->getResponse()->setHeader('X-Icinga-Reload-Css', 'now');
}

$this->getResponse()
->setHeader('X-Icinga-Redirect', rawurlencode($url->getAbsoluteUrl()))
->sendHeaders();

// TODO: Session shutdown?
exit;
}

/**
* Redirect to a specific url, updating the browsers URL field
*
* @param Url|string $url The target to redirect to
**/
public function redirectNow($url)
{
if (! $url instanceof Url) {
$url = Url::fromPath($url);
}
$url = preg_replace('~&amp;~', '&', $url);
if ($this->isXhr()) {
if ($this->rerenderLayout) {
$this->getResponse()->setHeader('X-Icinga-Rerender-Layout', 'yes');
}
if ($this->reloadCss) {
$this->getResponse()->setHeader('X-Icinga-Reload-Css', 'now');
}

$this->getResponse()
->setHeader('X-Icinga-Redirect', rawurlencode($url))
->sendHeaders();

// TODO: Session shutdown?
exit;
$this->redirectXhr($url);
} else {
$this->_helper->Redirector->gotoUrlAndExit(Url::fromPath($url)->getRelativeUrl());
if (! $url instanceof Url) {
$url = Url::fromPath($url);
}
$this->_helper->Redirector->gotoUrlAndExit($url->getRelativeUrl());
}
}

Expand Down

0 comments on commit 9d4a4f4

Please sign in to comment.