diff --git a/library/Icinga/Session/Session.php b/library/Icinga/Session/Session.php index c65c239f7..8682fea34 100644 --- a/library/Icinga/Session/Session.php +++ b/library/Icinga/Session/Session.php @@ -78,6 +78,28 @@ abstract class Session extends SessionNamespace return $this->namespaces[$identifier]; } + /** + * Return whether the given session namespace exists + * + * @param string $identifier The namespace's identifier to check + * + * @return bool + */ + public function hasNamespace($identifier) + { + return isset($this->namespaces[$identifier]); + } + + /** + * Remove the given session namespace + * + * @param string $identifier The identifier of the namespace to remove + */ + public function removeNamespace($identifier) + { + unset($this->namespaces[$identifier]); + } + /** * Clear all values and namespaces from the session cache */ diff --git a/library/Icinga/Session/SessionNamespace.php b/library/Icinga/Session/SessionNamespace.php index 5770889af..e169b5093 100644 --- a/library/Icinga/Session/SessionNamespace.php +++ b/library/Icinga/Session/SessionNamespace.php @@ -30,11 +30,13 @@ namespace Icinga\Session; use \Exception; +use \ArrayIterator; +use \IteratorAggregate; /** * Container for session values */ -class SessionNamespace +class SessionNamespace implements IteratorAggregate { /** * The actual values stored in this container @@ -43,6 +45,16 @@ class SessionNamespace */ protected $values = array(); + /** + * Return an iterator for all values in this namespace + * + * @return ArrayIterator + */ + public function getIterator() + { + return ArrayIterator($this->values); + } + /** * Set a session value by property access * diff --git a/library/Icinga/Web/Controller/ActionController.php b/library/Icinga/Web/Controller/ActionController.php index e5f4bdb82..740197d93 100644 --- a/library/Icinga/Web/Controller/ActionController.php +++ b/library/Icinga/Web/Controller/ActionController.php @@ -41,10 +41,10 @@ use Icinga\Util\Translator; use Icinga\Web\Widget\Tabs; use Icinga\Web\Url; use Icinga\Web\Notification; -use Icinga\Logger\Logger; -use Icinga\Web\Request; use Icinga\File\Pdf; use Icinga\Exception\ProgrammingError; +use Icinga\Web\Session; +use Icinga\Session\SessionNamespace; /** * Base class for all core action controllers @@ -149,6 +149,31 @@ class ActionController extends Zend_Controller_Action return $this->windowId; } + /** + * Return a window-aware session by using the given prefix + * + * @param string $prefix The prefix to use + * + * @return SessionNamespace + */ + public function getWindowSession($prefix) + { + $session = Session::getSession(); + $windowId = $this->getWindowId(); + $namespace = $session->getNamespace($prefix . '_' . $windowId); + + if ($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 * diff --git a/library/Icinga/Web/Session.php b/library/Icinga/Web/Session.php index 5fd4dc115..02b71ad00 100644 --- a/library/Icinga/Web/Session.php +++ b/library/Icinga/Web/Session.php @@ -30,7 +30,6 @@ namespace Icinga\Web; use Icinga\Session\PhpSession; -use Icinga\Session\SessionNamespace; use Icinga\Session\Session as BaseSession; use Icinga\Exception\ProgrammingError; @@ -78,21 +77,4 @@ class Session return self::$session; } - - /** - * Get or create a new session namespace - * - * @param string $identifier The namespace's identifier - * - * @return SessionNamespace - * @throws ProgrammingError - */ - public static function getNamespace($identifier) - { - if (self::$session === null) { - throw new ProgrammingError('No session created yet'); - } - - return self::$session->getNamespace($identifier); - } }