params/_render: replace it everywhere

Cleaning up controllers and JS, using headers only and respecting
history.
This commit is contained in:
Thomas Gelf 2014-06-22 20:06:45 +02:00
parent aded901aa5
commit 23ed744747
7 changed files with 59 additions and 45 deletions

View File

@ -65,16 +65,12 @@ class AuthenticationController extends ActionController
$this->view->title = $this->translate('Icingaweb Login');
try {
$redirectUrl = Url::fromPath($this->_request->getParam('redirect', 'dashboard'));
if ($this->_request->isXmlHttpRequest()) {
$redirectUrl->setParam('_render', 'layout');
}
$redirectUrl = Url::fromPath($this->params->get('redirect', 'dashboard'));
$auth = AuthManager::getInstance();
if ($auth->isAuthenticated()) {
$this->redirectNow($redirectUrl);
$this->rerenderLayout()->redirectNow($redirectUrl);
}
try {
@ -99,7 +95,7 @@ class AuthenticationController extends ActionController
$authenticated = $backend->authenticate($user);
if ($authenticated === true) {
$auth->setAuthenticated($user);
$this->redirectNow($redirectUrl);
$this->rerenderLayout()->redirectNow($redirectUrl);
}
}
}
@ -123,7 +119,7 @@ class AuthenticationController extends ActionController
}
if ($authenticated === true) {
$auth->setAuthenticated($user);
$this->redirectNow($redirectUrl);
$this->rerenderLayout()->redirectNow($redirectUrl);
}
}
if ($backendsWithError === $backendsTried) {
@ -161,8 +157,7 @@ class AuthenticationController extends ActionController
$this->_helper->layout->setLayout('login');
$this->_response->setHttpResponseCode(401);
} else {
$this->_helper->layout->setLayout('inline');
$this->redirectToLogin();
$this->rerenderLayout()->redirectToLogin();
}
}
}

View File

@ -192,11 +192,7 @@ class ConfigController extends BaseConfigController
$manager->enableModule($module);
$manager->loadModule($module);
Notification::success('Module "' . $module . '" enabled');
$this->redirectNow(Url::fromPath('config/modules', array(
'_render' => 'layout',
'_reload' => 'css'
)));
return;
$this->rerenderLayout()->reloadCss()->redirectNow('config/modules');
} catch (Exception $e) {
$this->view->exceptionMesssage = $e->getMessage();
$this->view->moduleName = $module;
@ -215,11 +211,7 @@ class ConfigController extends BaseConfigController
try {
$manager->disableModule($module);
Notification::success('Module "' . $module . '" disabled');
$this->redirectNow(Url::fromPath('config/modules', array(
'_render' => 'layout',
'_reload' => 'css'
)));
return;
$this->rerenderLayout()->reloadCss()->redirectNow('config/modules');
} catch (Exception $e) {
$this->view->exceptionMessage = $e->getMessage();
$this->view->moduleName = $module;

View File

@ -45,12 +45,7 @@ class IndexController extends ActionController
public function preDispatch()
{
if ($this->getRequest()->getActionName() !== 'welcome') {
$url = Url::fromPath('dashboard');
$render = $this->_request->getParam('_render');
if ($render) {
$url->setParam('_render', $render);
}
$this->redirectNow($url);
$this->redirectNow('dashboard');
}
}

View File

@ -9,7 +9,7 @@ if (! $this->auth()->isAuthenticated()) {
}
// Current url
$url = Url::fromRequest()->remove('_render')->getRelativeUrl();
$url = Url::fromRequest()->getRelativeUrl();
$menu = Menu::fromConfig();
?>
<div id="menu" data-base-target="_main">

View File

@ -81,12 +81,14 @@ class ActionController extends Zend_Controller_Action
private $window;
protected $isRedirect = false;
private $rerenderLayout = false;
protected $params;
private $xhrLayout = 'inline';
private $auth;
protected $params;
/**
* The constructor starts benchmarking, loads the configuration and sets
* other useful controller properties
@ -109,7 +111,10 @@ class ActionController extends Zend_Controller_Action
$this->handlerBrowserWindows();
$this->view->translationDomain = 'icinga';
$this->_helper->layout()->isIframe = (bool) $this->params->shift('isIframe', false);
$this->_helper->layout()->isIframe = $this->params->shift('isIframe');
if ($this->rerenderLayout = $this->params->shift('renderLayout')) {
$this->xhrLayout = 'body';
}
if ($this->requiresConfig()) {
$this->redirectNow(Url::fromPath('install'));
}
@ -171,6 +176,7 @@ class ActionController extends Zend_Controller_Action
protected function reloadCss()
{
$this->reloadCss = true;
return $this;
}
/**
@ -314,12 +320,16 @@ class ActionController extends Zend_Controller_Action
*/
protected function redirectToLogin($afterLogin = '/dashboard')
{
$url = Url::fromPath('/authentication/login');
if ($this->isXhr()) {
$url->setParam('_render', 'layout');
}
$url = Url::fromPath('authentication/login');
$url->setParam('redirect', $afterLogin);
$this->redirectNow($url);
$this->rerenderLayout()->redirectNow($url);
}
protected function rerenderLayout()
{
$this->rerenderLayout = true;
$this->xhrLayout = 'layout';
return $this;
}
public function isXhr()
@ -334,8 +344,18 @@ class ActionController extends Zend_Controller_Action
**/
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();
@ -345,7 +365,6 @@ class ActionController extends Zend_Controller_Action
} else {
$this->_helper->Redirector->gotoUrlAndExit(Url::fromPath($url)->getRelativeUrl());
}
$this->isRedirect = true; // pretty useless right now
}
/**
@ -384,7 +403,7 @@ class ActionController extends Zend_Controller_Action
protected function postDispatchXhr()
{
$layout = $this->_helper->layout();
$layout->setLayout('inline');
$layout->setLayout($this->xhrLayout);
$resp = $this->getResponse();
$notifications = Notification::getInstance();
@ -393,10 +412,10 @@ class ActionController extends Zend_Controller_Action
foreach ($notifications->getMessages() as $m) {
$notificationList[] = rawurlencode($m->type . ' ' . $m->message);
}
$resp->setHeader('X-Icinga-Notification: ' . implode('&', $notificationList));
$resp->setHeader('X-Icinga-Notification', implode('&', $notificationList));
}
if ($this->reloadCss || $this->getParam('_reload') === 'css') {
if ($this->reloadCss) {
$resp->setHeader('X-Icinga-CssReload', 'now');
}
@ -411,9 +430,8 @@ class ActionController extends Zend_Controller_Action
);
}
if ($this->getParam('_render') === 'layout') {
$layout->setLayout('body');
$resp->setHeader('X-Icinga-Container', 'layout');
if ($this->rerenderLayout) {
$this->getResponse()->setHeader('X-Icinga-Container', 'layout');
}
if ($this->autorefreshInterval !== null) {

View File

@ -441,7 +441,6 @@ class Monitoring_ListController extends Controller
{
$params = clone $this->params;
$params->shift('_render');
$limit = $params->shift('limit');
$sort = $params->shift('sort');
$dir = $params->shift('dir');
@ -462,7 +461,7 @@ class Monitoring_ListController extends Controller
} else {
$filter->removeId($removeFilter);
$redirect->setQueryString($filter->toQueryString())
->getParams()->add('modifyFilter');;
->getParams()->add('modifyFilter');
}
$this->redirectNow($redirect);
}

View File

@ -234,6 +234,15 @@
return true;
},
addUrlFlag: function(url, flag)
{
if (url.match(/\?/)) {
return url + '&' + flag;
} else {
return url + '?' + flag;
}
},
processRedirectHeader: function(req) {
var redirect = req.getResponseHeader('X-Icinga-Redirect');
if (! redirect) return false;
@ -241,7 +250,13 @@
'Got redirect for ', req.$target, ', URL was ' + redirect
);
redirect = decodeURIComponent(redirect);
if (req.getResponseHeader('X-Icinga-Rerender-Layout')) {
var redirectionUrl = this.addUrlFlag(redirect, 'renderLayout');
this.loadUrl(redirectionUrl, $('#layout')).url = redirect;
} else {
this.loadUrl(redirect, req.$target);
}
return true;
},