Web\Window: move window handle to dedicated class

One more step in cleaning up our ActionController
This commit is contained in:
Thomas Gelf 2014-06-22 12:55:44 +02:00
parent d69a6d1640
commit b350e3640d
2 changed files with 87 additions and 58 deletions

View File

@ -40,6 +40,7 @@ use Icinga\Application\Benchmark;
use Icinga\Application\Config;
use Icinga\Util\Translator;
use Icinga\Web\Widget\Tabs;
use Icinga\Web\Window;
use Icinga\Web\Url;
use Icinga\Web\Notification;
use Icinga\File\Pdf;
@ -108,10 +109,7 @@ class ActionController extends Zend_Controller_Action
->_setInvokeArgs($invokeArgs);
$this->_helper = new Zend_Controller_Action_HelperBroker($this);
$this->_helper->addPath('../application/controllers/helpers');
if ($this->_request->isXmlHttpRequest()) {
$this->windowId = $this->_request->getHeader('X-Icinga-WindowId', null);
}
$this->handleWindowId();
$module = $request->getModuleName();
$this->view->translationDomain = $module === 'default' ? 'icinga' : $module;
@ -153,61 +151,25 @@ class ActionController extends Zend_Controller_Action
return $this->auth;
}
protected function handleWindowId()
{
if ($this->_request->isXmlHttpRequest()) {
$windowId = $this->_request->getHeader('X-Icinga-WindowId', null);
if ($windowId === Window::UNDEFINED) {
$this->_response->setHeader('X-Icinga-WindowId', Window::generateId());
}
}
}
protected function moduleInit()
{
}
protected function getWindowId()
{
if ($this->windowId === null) {
return 'undefined';
}
return $this->windowId;
}
protected function generateWindowId()
{
$letters = 'abcefghijklmnopqrstuvwxyz';
$this->windowId = substr(str_shuffle($letters), 0, 12);
return $this->windowId;
}
protected function reloadCss()
{
$this->reloadCss = true;
}
/**
* Return a window-aware session by using the given prefix
*
* @param string $prefix The prefix to use
* @param bool $reset Whether to reset any existing session-data
*
* @return SessionNamespace
*/
public function getWindowSession($prefix, $reset = false)
{
$session = Session::getSession();
$windowId = $this->getWindowId();
$identifier = $prefix . '_' . $windowId;
if ($reset && $session->hasNamespace($identifier)) {
$session->removeNamespace($identifier);
}
$namespace = $session->getNamespace($identifier);
if (!$reset && $windowId !== 'undefined' && $session->hasNamespace($prefix . '_undefined')) {
// We do not have any window-id on the very first request. Now we add all values from the
// namespace, that has been created in this case, to the new one and remove it afterwards.
foreach ($session->getNamespace($prefix . '_undefined') as $name => $value) {
$namespace->set($name, $value);
}
$session->removeNamespace($prefix . '_undefined');
}
return $namespace;
}
/**
* Return restriction information for an eventually authenticated user
*
@ -373,10 +335,12 @@ class ActionController extends Zend_Controller_Action
{
$url = preg_replace('~&~', '&', $url);
if ($this->_request->isXmlHttpRequest()) {
header('X-Icinga-Redirect: ' . rawurlencode($url));
// $this->getResponse()->sendHeaders() ??
// Session shutdown
exit; // Really?
$this->getResponse()
->setHeader('X-Icinga-Redirect', rawurlencode($url))
->sendHeaders();
// TODO: Session shutdown?
exit;
} else {
$this->_helper->Redirector->gotoUrlAndExit(Url::fromPath($url)->getRelativeUrl());
}
@ -438,10 +402,6 @@ class ActionController extends Zend_Controller_Action
return;
}
if ($isXhr && $this->getWindowId() === 'undefined') {
header('X-Icinga-WindowId: ' . $this->generateWindowId());
}
if ($this->view->title) {
if (preg_match('~[\r\n]~', $this->view->title)) {
// TODO: Innocent exception and error log for hack attempts
@ -459,6 +419,10 @@ class ActionController extends Zend_Controller_Action
}
}
protected function Window()
{
}
protected function sendAsPdf()
{
$pdf = new Pdf();

View File

@ -0,0 +1,65 @@
<?php
namespace Icinga\Web;
use Icinga\Web\Session;
class Window
{
const UNDEFINED = 'undefined';
protected $id;
public function __construct($id)
{
$this->id = $id;
}
public function isUndefined()
{
return $this->id === self::UNDEFINED;
}
public function getId()
{
return $this->id;
}
/**
* Return a window-aware session by using the given prefix
*
* @param string $prefix The prefix to use
* @param bool $reset Whether to reset any existing session-data
*
* @return SessionNamespace
*/
public function getSessionNamespace($prefix, $reset = false)
{
$session = Session::getSession();
$identifier = $prefix . '_' . $this->id;
if ($reset && $session->hasNamespace($identifier)) {
$session->removeNamespace($identifier);
}
$namespace = $session->getNamespace($identifier);
$nsUndef = $prefix . '_' . self::UNDEFINED;
if (!$reset && $this->id !== self::UNDEFINED && $session->hasNamespace($nsUndef)) {
// We do not have any window-id on the very first request. Now we add
// all values from the namespace, that has been created in this case,
// to the new one and remove it afterwards.
foreach ($session->getNamespace($nsUndef) as $name => $value) {
$namespace->set($name, $value);
}
$session->removeNamespace($nsUndef);
}
return $namespace;
}
public static function generateId()
{
$letters = 'abcefghijklmnopqrstuvwxyz';
return substr(str_shuffle($letters), 0, 12);
}
}