mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-25 14:54:24 +02:00
parent
ac0b041523
commit
cf778d4609
@ -32,11 +32,19 @@ namespace Icinga\Session;
|
|||||||
use Icinga\Application\Logger;
|
use Icinga\Application\Logger;
|
||||||
use \Icinga\Exception\ConfigurationError;
|
use \Icinga\Exception\ConfigurationError;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Session implementation in PHP
|
* Session implementation in PHP
|
||||||
*/
|
*/
|
||||||
class PhpSession extends Session
|
class PhpSession extends Session
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* The namespace prefix
|
||||||
|
*
|
||||||
|
* Used to differentiate between standard session keys and namespace identifiers
|
||||||
|
*/
|
||||||
|
const NAMESPACE_PREFIX = 'ns.';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Name of the session
|
* Name of the session
|
||||||
*
|
*
|
||||||
@ -111,7 +119,17 @@ class PhpSession extends Session
|
|||||||
public function read()
|
public function read()
|
||||||
{
|
{
|
||||||
$this->open();
|
$this->open();
|
||||||
$this->setAll($_SESSION);
|
|
||||||
|
foreach ($_SESSION as $key => $value) {
|
||||||
|
if (strpos($key, self::NAMESPACE_PREFIX) === 0) {
|
||||||
|
$namespace = new SessionNamespace();
|
||||||
|
$namespace->setAll($value);
|
||||||
|
$this->namespaces[substr($key, strlen(self::NAMESPACE_PREFIX))] = $namespace;
|
||||||
|
} else {
|
||||||
|
$this->set($key, $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
session_write_close();
|
session_write_close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,9 +139,14 @@ class PhpSession extends Session
|
|||||||
public function write()
|
public function write()
|
||||||
{
|
{
|
||||||
$this->open();
|
$this->open();
|
||||||
foreach ($this->getAll() as $key => $value) {
|
|
||||||
|
foreach ($this->values as $key => $value) {
|
||||||
$_SESSION[$key] = $value;
|
$_SESSION[$key] = $value;
|
||||||
}
|
}
|
||||||
|
foreach ($this->namespaces as $identifier => $namespace) {
|
||||||
|
$_SESSION[self::NAMESPACE_PREFIX . $identifier] = $namespace->getAll();
|
||||||
|
}
|
||||||
|
|
||||||
session_write_close();
|
session_write_close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,17 +29,18 @@
|
|||||||
|
|
||||||
namespace Icinga\Session;
|
namespace Icinga\Session;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for handling sessions
|
* Base class for handling sessions
|
||||||
*/
|
*/
|
||||||
abstract class Session
|
abstract class Session extends SessionNamespace
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Container for session values
|
* Container for session namespaces
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $sessionValues = array();
|
protected $namespaces = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read all values from the underlying session implementation
|
* Read all values from the underlying session implementation
|
||||||
@ -57,100 +58,27 @@ abstract class Session
|
|||||||
abstract public function purge();
|
abstract public function purge();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setter for session values
|
* Get or create a new session namespace
|
||||||
*
|
*
|
||||||
* Values need to be manually persisted with method write.
|
* @param string $identifier The namespace's identifier
|
||||||
*
|
*
|
||||||
* @param string $key Name of value
|
* @return SessionNamespace
|
||||||
* @param mixed $value Value to set
|
|
||||||
* @param string $namespace Namespace to use
|
|
||||||
*
|
|
||||||
* @return Session
|
|
||||||
* @see self::write
|
|
||||||
*/
|
*/
|
||||||
public function set($key, $value, $namespace = null)
|
public function getNamespace($identifier)
|
||||||
{
|
{
|
||||||
if ($namespace !== null) {
|
if (!isset($this->namespaces[$identifier])) {
|
||||||
if (!isset($this->sessionValues[$namespace])) {
|
$this->namespaces[$identifier] = new SessionNamespace();
|
||||||
$this->sessionValues[$namespace] = array();
|
|
||||||
}
|
|
||||||
$this->sessionValues[$namespace][$key] = $value;
|
|
||||||
} else {
|
|
||||||
$this->sessionValues[$key] = $value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this->namespaces[$identifier];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Getter for session values
|
* Clear all values and namespaces from the session cache
|
||||||
*
|
|
||||||
* Values are available after populating the session with method read.
|
|
||||||
*
|
|
||||||
* @param string $key Name of the value to return
|
|
||||||
* @param mixed $defaultValue Default value to return
|
|
||||||
* @param string $namespace Namespace to use
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
* @see self::read
|
|
||||||
*/
|
|
||||||
public function get($key, $defaultValue = null, $namespace = null)
|
|
||||||
{
|
|
||||||
if ($namespace !== null) {
|
|
||||||
if (isset($this->sessionValues[$namespace]) && isset($this->sessionValues[$namespace][$key])) {
|
|
||||||
return $this->sessionValues[$namespace][$key];
|
|
||||||
}
|
|
||||||
return $defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return isset($this->sessionValues[$key]) ? $this->sessionValues[$key] : $defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Getter for all session values
|
|
||||||
*
|
|
||||||
* Values are available after populating the session with method read.
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getAll()
|
|
||||||
{
|
|
||||||
return $this->sessionValues;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put an array into the session
|
|
||||||
*
|
|
||||||
* @param array $values Values to set
|
|
||||||
* @param bool $overwrite Overwrite existing values
|
|
||||||
* @param strign $namespace Namespace to use
|
|
||||||
*/
|
|
||||||
public function setAll(array $values, $overwrite = false, $namespace = null)
|
|
||||||
{
|
|
||||||
if ($namespace !== null && !isset($this->sessionValues[$namespace])) {
|
|
||||||
$this->sessionValues[$namespace] = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($values as $key => $value) {
|
|
||||||
if ($namespace !== null) {
|
|
||||||
if (isset($this->sessionValues[$namespace][$key]) && !overwrite) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$this->sessionValues[$namespace][$key] = $value;
|
|
||||||
} else {
|
|
||||||
if (isset($this->sessionValues[$key]) && !$overwrite) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$this->sessionValues[$key] = $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clear all values from the session cache
|
|
||||||
*/
|
*/
|
||||||
public function clear()
|
public function clear()
|
||||||
{
|
{
|
||||||
$this->sessionValues = array();
|
$this->values = array();
|
||||||
|
$this->namespaces = array();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
97
library/Icinga/Session/SessionNamespace.php
Normal file
97
library/Icinga/Session/SessionNamespace.php
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
/**
|
||||||
|
* This file is part of Icinga Web 2.
|
||||||
|
*
|
||||||
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
||||||
|
* Copyright (C) 2014 Icinga Development Team
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
* @copyright 2014 Icinga Development Team <info@icinga.org>
|
||||||
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
||||||
|
* @author Icinga Development Team <info@icinga.org>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
|
||||||
|
namespace Icinga\Session;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Container for session values
|
||||||
|
*/
|
||||||
|
class SessionNamespace
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The actual values stored in this container
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $values = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setter for session values
|
||||||
|
*
|
||||||
|
* @param string $key Name of value
|
||||||
|
* @param mixed $value Value to set
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function set($key, $value)
|
||||||
|
{
|
||||||
|
$this->values[$key] = $value;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter for session values
|
||||||
|
*
|
||||||
|
* @param string $key Name of the value to return
|
||||||
|
* @param mixed $default Default value to return
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function get($key, $default = null)
|
||||||
|
{
|
||||||
|
return isset($this->values[$key]) ? $this->values[$key] : $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter for all session values
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getAll()
|
||||||
|
{
|
||||||
|
return $this->values;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Put an array into the session
|
||||||
|
*
|
||||||
|
* @param array $values Values to set
|
||||||
|
* @param bool $overwrite Overwrite existing values
|
||||||
|
*/
|
||||||
|
public function setAll(array $values, $overwrite = false)
|
||||||
|
{
|
||||||
|
foreach ($values as $key => $value) {
|
||||||
|
if (isset($this->values[$key]) && !$overwrite) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$this->values[$key] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -30,6 +30,7 @@
|
|||||||
namespace Icinga\Web;
|
namespace Icinga\Web;
|
||||||
|
|
||||||
use Icinga\Session\PhpSession;
|
use Icinga\Session\PhpSession;
|
||||||
|
use Icinga\Session\SessionNamespace;
|
||||||
use Icinga\Session\Session as BaseSession;
|
use Icinga\Session\Session as BaseSession;
|
||||||
use Icinga\Exception\ProgrammingError;
|
use Icinga\Exception\ProgrammingError;
|
||||||
|
|
||||||
@ -68,6 +69,7 @@ class Session
|
|||||||
* Return the current session
|
* Return the current session
|
||||||
*
|
*
|
||||||
* @return BaseSession
|
* @return BaseSession
|
||||||
|
* @throws ProgrammingError
|
||||||
*/
|
*/
|
||||||
public static function getSession()
|
public static function getSession()
|
||||||
{
|
{
|
||||||
@ -77,4 +79,21 @@ class Session
|
|||||||
|
|
||||||
return self::$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
|
|
||||||
namespace Tests\Icinga\Authentication;
|
namespace Tests\Icinga\Authentication;
|
||||||
|
|
||||||
|
require_once("../../library/Icinga/Session/SessionNamespace.php");
|
||||||
require_once("../../library/Icinga/Session/Session.php");
|
require_once("../../library/Icinga/Session/Session.php");
|
||||||
|
|
||||||
use Icinga\Session\Session;
|
use Icinga\Session\Session;
|
||||||
|
@ -99,4 +99,23 @@ class PhpSessionTest extends BaseTestCase
|
|||||||
$session->read();
|
$session->read();
|
||||||
$this->assertEquals(null, $session->get('key2'));
|
$this->assertEquals(null, $session->get('key2'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test whether session namespaces are properly written and loaded
|
||||||
|
*
|
||||||
|
* @runInSeparateProcess
|
||||||
|
*/
|
||||||
|
public function testNamespaceReadWrite()
|
||||||
|
{
|
||||||
|
$session = $this->getSession();
|
||||||
|
$namespace = $session->getNamespace('test');
|
||||||
|
$namespace->set('some_key', 'some_val');
|
||||||
|
$namespace->set('an_array', array(1, 2, 3));
|
||||||
|
$session->write();
|
||||||
|
$session->clear();
|
||||||
|
$session->read();
|
||||||
|
$namespace = $session->getNamespace('test');
|
||||||
|
$this->assertEquals($namespace->get('some_key'), 'some_val');
|
||||||
|
$this->assertEquals($namespace->get('an_array'), array(1, 2, 3));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user