Remove session handling from the authentication manager

refs #5510
This commit is contained in:
Johannes Meyer 2014-01-23 12:09:48 +01:00
parent fc1fb60b01
commit ac0b041523
15 changed files with 379 additions and 295 deletions

View File

@ -37,17 +37,19 @@ use \Zend_View_Helper_PaginationControl;
use \Zend_Controller_Action_HelperBroker; use \Zend_Controller_Action_HelperBroker;
use \Zend_Controller_Router_Route; use \Zend_Controller_Router_Route;
use \Zend_Controller_Front; use \Zend_Controller_Front;
use \Icinga\Application\Logger; use Icinga\Application\Logger;
use \Icinga\Authentication\Manager as AuthenticationManager; use Icinga\Authentication\Manager as AuthenticationManager;
use \Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
use \Icinga\User\Preferences; use Icinga\User\Preferences;
use \Icinga\User\Preferences\LoadInterface; use Icinga\User\Preferences\LoadInterface;
use \Icinga\User; use Icinga\User;
use \Icinga\Web\Request; use Icinga\Web\Request;
use \Icinga\Web\View; use Icinga\Web\View;
use \Icinga\User\Preferences\StoreFactory; use Icinga\User\Preferences\StoreFactory;
use \Icinga\User\Preferences\SessionStore; use Icinga\User\Preferences\SessionStore;
use \Icinga\Util\DateTimeFactory; use Icinga\Util\DateTimeFactory;
use Icinga\Session\Session as BaseSession;
use Icinga\Web\Session;
/** /**
* Use this if you want to make use of Icinga functionality in other web projects * Use this if you want to make use of Icinga functionality in other web projects
@ -81,6 +83,13 @@ class Web extends ApplicationBootstrap
*/ */
private $request; private $request;
/**
* Session object
*
* @var BaseSession
*/
private $session;
/** /**
* User object * User object
* *
@ -105,6 +114,7 @@ class Web extends ApplicationBootstrap
return $this->setupConfig() return $this->setupConfig()
->setupErrorHandling() ->setupErrorHandling()
->setupResourceFactory() ->setupResourceFactory()
->setupSession()
->setupUser() ->setupUser()
->setupTimezone() ->setupTimezone()
->setupRequest() ->setupRequest()
@ -239,7 +249,7 @@ class Web extends ApplicationBootstrap
$user = $authenticationManager->getUser(); $user = $authenticationManager->getUser();
// Needed to update values in user session // Needed to update values in user session
$sessionStore = new SessionStore($authenticationManager->getSession()); $sessionStore = new SessionStore($this->session);
// Performance: Do not ask provider if we've preferences // Performance: Do not ask provider if we've preferences
// stored in session // stored in session
@ -323,6 +333,17 @@ class Web extends ApplicationBootstrap
return $this; return $this;
} }
/**
* Initialize a session provider
*
* @return self
*/
private function setupSession()
{
$this->session = Session::create();
return $this;
}
/** /**
* Inject dependencies into request * Inject dependencies into request
* *

View File

@ -4,7 +4,7 @@
* This file is part of Icinga Web 2. * This file is part of Icinga Web 2.
* *
* Icinga Web 2 - Head for multiple monitoring backends. * Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team * Copyright (C) 2014 Icinga Development Team
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -20,7 +20,7 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* *
* @copyright 2013 Icinga Development Team <info@icinga.org> * @copyright 2014 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org> * @author Icinga Development Team <info@icinga.org>
* *
@ -30,15 +30,15 @@
namespace Icinga\Authentication; namespace Icinga\Authentication;
use \Exception; use \Exception;
use Icinga\Exception\ConfigurationError;
use \Zend_Config; use \Zend_Config;
use \Icinga\User; use Icinga\User;
use \Icinga\Data\ResourceFactory; use Icinga\Web\Session;
use \Icinga\Application\Logger; use Icinga\Data\ResourceFactory;
use \Icinga\Application\Config as IcingaConfig; use Icinga\Application\Logger;
use \Icinga\Authentication\Backend\DbUserBackend; use Icinga\Exception\ConfigurationError;
use \Icinga\Authentication\Backend\LdapUserBackend; use Icinga\Application\Config as IcingaConfig;
use \Icinga\Exception\ConfigurationError as ConfigError; use Icinga\Authentication\Backend\DbUserBackend;
use Icinga\Authentication\Backend\LdapUserBackend;
/** /**
@ -47,13 +47,7 @@ use \Icinga\Exception\ConfigurationError as ConfigError;
* *
* Direct instantiation is not permitted, the AuthenticationManager * Direct instantiation is not permitted, the AuthenticationManager
* must be created using the getInstance method. Subsequent getInstance * must be created using the getInstance method. Subsequent getInstance
* calls return the same object and ignore any additional configuration * calls return the same object and ignore any additional configuration.
*
* When creating the Authentication manager with standard PHP Sessions,
* you have to decide whether you want to modify the session on the first
* initialization and provide the 'writeSession' option if so, otherwise
* session changes won't be written to disk. This is done to prevent PHP
* from blocking concurrent requests
* *
* @TODO(mh): Group support is not implemented yet (#4624) * @TODO(mh): Group support is not implemented yet (#4624)
**/ **/
@ -64,19 +58,19 @@ class Manager
* *
* @var self * @var self
*/ */
private static $instance = null; private static $instance;
/** /**
* Instance of authenticated user * Instance of authenticated user
* *
* @var User * @var User
**/ **/
private $user = null; private $user;
/** /**
* Array of user backends * Array of user backends
* *
* @var UserBackend[] * @var array
**/ **/
private $userBackends = array(); private $userBackends = array();
@ -87,13 +81,6 @@ class Manager
**/ **/
private $groupBackends = array(); private $groupBackends = array();
/**
* Session
*
* @var Session
**/
private $session = null;
/** /**
* The configuration * The configuration
* *
@ -117,8 +104,6 @@ class Manager
* instead of the authentication.ini * instead of the authentication.ini
* @param array $options Additional options that affect the managers behaviour. * @param array $options Additional options that affect the managers behaviour.
* Supported values: * Supported values:
* * writeSession: Whether the session should be writable
* * sessionClass: Allows to provide a different session implementation)
* * noDefaultConfig: Disable default configuration from authentication.ini * * noDefaultConfig: Disable default configuration from authentication.ini
**/ **/
private function __construct(Zend_Config $config = null, array $options = array()) private function __construct(Zend_Config $config = null, array $options = array())
@ -126,11 +111,6 @@ class Manager
if ($config === null && !(isset($options['noDefaultConfig']) && $options['noDefaultConfig'] == true)) { if ($config === null && !(isset($options['noDefaultConfig']) && $options['noDefaultConfig'] == true)) {
$config = IcingaConfig::app('authentication'); $config = IcingaConfig::app('authentication');
} }
if (!isset($options['sessionClass'])) {
$this->session = new PhpSession();
} else {
$this->session = $options['sessionClass'];
}
$this->config = $config; $this->config = $config;
} }
@ -177,7 +157,7 @@ class Manager
} }
/** /**
* Create a single backend from Zend Config * Create a single backend from the given Zend_Config
* *
* @param Zend_Config $backendConfig * @param Zend_Config $backendConfig
* *
@ -211,14 +191,14 @@ class Manager
default: default:
Logger::warn('AuthManager: Resource type ' . $backendConfig->type . ' not available.'); Logger::warn('AuthManager: Resource type ' . $backendConfig->type . ' not available.');
} }
} catch (\Exception $e) { } catch (Exception $e) {
Logger::warn('AuthManager: Not able to create backend. Exception was thrown: %s', $e->getMessage()); Logger::warn('AuthManager: Not able to create backend. Exception was thrown: %s', $e->getMessage());
} }
return null; return null;
} }
/** /**
* Add a user backend to stack * Add a user backend to the stack
* *
* @param UserBackend $userBackend * @param UserBackend $userBackend
*/ */
@ -237,12 +217,11 @@ class Manager
public function getUserBackend($name) public function getUserBackend($name)
{ {
$this->initBackends(); $this->initBackends();
return (isset($this->userBackends[$name])) ? return (isset($this->userBackends[$name])) ? $this->userBackends[$name] : null;
$this->userBackends[$name] : null;
} }
/** /**
* Add a group backend to stack * Add a group backend to the stack
* *
* @param GroupBackend $groupBackend * @param GroupBackend $groupBackend
*/ */
@ -261,12 +240,11 @@ class Manager
public function getGroupBackend($name) public function getGroupBackend($name)
{ {
$this->initBackends(); $this->initBackends();
return (isset($this->groupBackends[$name])) ? return (isset($this->groupBackends[$name])) ? $this->groupBackends[$name] : null;
$this->groupBackends[$name] : null;
} }
/** /**
* Find a backend for a credential * Find a backend for the given credentials
* *
* @param Credential $credentials * @param Credential $credentials
* *
@ -313,9 +291,8 @@ class Manager
if ($authErrors >= count($this->userBackends)) { if ($authErrors >= count($this->userBackends)) {
Logger::fatal('AuthManager: No working backend found, unable to authenticate any user'); Logger::fatal('AuthManager: No working backend found, unable to authenticate any user');
throw new ConfigurationError( throw new ConfigurationError(
'No working backend found. Unable to authenticate any user.' 'No working backend found. Unable to authenticate any user.' .
. "\n" "\nPlease examine the logs for more information."
. 'Please examine the logs for more information.'
); );
} }
@ -334,24 +311,23 @@ class Manager
} }
/** /**
* Try to authenticate the current user with the Credential (@see Credential). * Try to authenticate a user with the given credentials
* *
* @param Credential $credentials The credentials to use for authentication * @param Credential $credentials The credentials to use for authentication
* @param Boolean $persist Whether to persist the authentication result * @param Boolean $persist Whether to persist the authentication result in the current session
* in the current session
* *
* @return Boolean true on success, otherwise false * @return Boolean Whether the authentication was successful or not
* @throws ConfigError * @throws ConfigurationError
*/ */
public function authenticate(Credential $credentials, $persist = true) public function authenticate(Credential $credentials, $persist = true)
{ {
$this->initBackends(); $this->initBackends();
if (count($this->userBackends) === 0) { if (count($this->userBackends) === 0) {
Logger::error('AuthManager: No authentication backend provided, your users will never be able to login.'); Logger::error('AuthManager: No authentication backend provided, your users will never be able to login.');
throw new ConfigError( throw new ConfigurationError(
'No authentication backend set - login will never succeed as icinga-web ' 'No authentication backend set - login will never succeed as icinga-web ' .
. 'doesn\'t know how to determine your user. ' . "\n" 'doesn\'t know how to determine your user. ' . "\n" .
. 'To fix this error, setup your authentication.ini with at least one valid authentication backend.' 'To fix this error, setup your authentication.ini with at least one valid authentication backend.'
); );
} }
@ -364,7 +340,7 @@ class Manager
$this->user = $userBackend->authenticate($credentials); $this->user = $userBackend->authenticate($credentials);
if ($this->user == null) { if ($this->user === null) {
Logger::info('AuthManager: Invalid credentials for user %s provided', $credentials->getUsername()); Logger::info('AuthManager: Invalid credentials for user %s provided', $credentials->getUsername());
return false; return false;
} }
@ -377,7 +353,6 @@ class Manager
if ($persist == true) { if ($persist == true) {
$this->persistCurrentUser(); $this->persistCurrentUser();
$this->session->write();
} }
Logger::info('AuthManager: User successfully logged in: %s', $credentials->getUsername()); Logger::info('AuthManager: User successfully logged in: %s', $credentials->getUsername());
@ -386,11 +361,13 @@ class Manager
} }
/** /**
* Writes the current user to the session (only usable when writeSession = true) * Writes the current user to the session
**/ **/
public function persistCurrentUser() public function persistCurrentUser()
{ {
$this->session->set('user', $this->user); $session = Session::getSession();
$session->set('user', $this->user);
$session->write();
} }
/** /**
@ -398,7 +375,7 @@ class Manager
**/ **/
public function authenticateFromSession() public function authenticateFromSession()
{ {
$this->user = $this->session->get('user', null); $this->user = Session::getSession()->get('user');
} }
/** /**
@ -458,12 +435,12 @@ class Manager
} }
/** /**
* Purges the current authorisation information and deletes the session * Purges the current authorization information and removes the user from the session
**/ **/
public function removeAuthorization() public function removeAuthorization()
{ {
$this->user = null; $this->user = null;
$this->session->purge(); $this->persistCurrentUser();
} }
/** /**
@ -477,7 +454,7 @@ class Manager
} }
/** /**
* Getter for groups belong authenticated user * Getter for groups belonged to authenticated user
* *
* @return array * @return array
* @see User::getGroups * @see User::getGroups
@ -486,14 +463,4 @@ class Manager
{ {
return $this->user->getGroups(); return $this->user->getGroups();
} }
/**
* Getter for session
*
* @return Session
*/
public function getSession()
{
return $this->session;
}
} }

View File

@ -1,131 +0,0 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga Web 2.
*
* Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2013 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 2013 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\Authentication;
/**
* Base class for handling sessions
*/
abstract class Session
{
/**
* Container for session values
*
* @var array
*/
private $sessionValues = array();
/**
* Read all values from the underlying session implementation
*/
abstract public function read();
/**
* Persists changes to the underlying session implementation
*/
abstract public function write();
/**
* Purge session
*/
abstract public function purge();
/**
* Setter for session values
*
* You have to persist values manually
*
* @see self::persist
* @param string $key Name of value
* @param mixed $value Value
*
* @returns PhpSession this
*/
public function set($key, $value)
{
$this->sessionValues[$key] = $value;
return $this;
}
/**
* Getter fpr session values
*
* Values are available after populate session with method read.
*
* @param string $key
* @param mixed $defaultValue
*
* @return mixed
* @see self::read
*/
public function get($key, $defaultValue = null)
{
return isset($this->sessionValues[$key]) ?
$this->sessionValues[$key] : $defaultValue;
}
/**
* Getter for all session values
*
* This are also dirty, unwritten values.
*
* @return array
*/
public function getAll()
{
return $this->sessionValues;
}
/**
* Put an array into session
*
* @param array $values
* @param bool $overwrite Overwrite existing values
*/
public function setAll(array $values, $overwrite = false)
{
if ($overwrite) {
$this->clear();
}
foreach ($values as $key => $value) {
if (isset($this->sessionValues[$key]) && !$overwrite) {
continue;
}
$this->sessionValues[$key] = $value;
}
}
/**
* Clear all values from the session cache
*/
public function clear()
{
$this->sessionValues = array();
}
}

View File

@ -4,7 +4,7 @@
* This file is part of Icinga Web 2. * This file is part of Icinga Web 2.
* *
* Icinga Web 2 - Head for multiple monitoring backends. * Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team * Copyright (C) 2014 Icinga Development Team
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -20,29 +20,20 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* *
* @copyright 2013 Icinga Development Team <info@icinga.org> * @copyright 2014 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org> * @author Icinga Development Team <info@icinga.org>
* *
*/ */
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Authentication; 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
*
* Standard PHP Session handling
* You have to call read() first in order to start the session. If
* no parameter is given to read, the session is closed immediately
* after reading the persisted variables, in order to avoid concurrent
* requests to be blocked. Otherwise, you can call write() (again with
* no parameter in order to auto-close it) to persist all values previously
* set with the set() method
*
*/ */
class PhpSession extends Session class PhpSession extends Session
{ {
@ -70,7 +61,7 @@ class PhpSession extends Session
/** /**
* Create a new PHPSession object using the provided options (if any) * Create a new PHPSession object using the provided options (if any)
* *
* @param array $options An optional array of ini options to set, * @param array $options An optional array of ini options to set
* *
* @throws ConfigurationError * @throws ConfigurationError
* @see http://php.net/manual/en/session.configuration.php * @see http://php.net/manual/en/session.configuration.php
@ -78,14 +69,16 @@ class PhpSession extends Session
public function __construct(array $options = null) public function __construct(array $options = null)
{ {
if ($options !== null) { if ($options !== null) {
$options = array_merge(PhpSession::$defaultCookieOptions, $options); $options = array_merge(self::$defaultCookieOptions, $options);
} else { } else {
$options = PhpSession::$defaultCookieOptions; $options = self::$defaultCookieOptions;
} }
if (array_key_exists('test_session_name', $options)) { if (array_key_exists('test_session_name', $options)) {
$this->sessionName = $options['test_session_name']; $this->sessionName = $options['test_session_name'];
unset($options['test_session_name']); unset($options['test_session_name']);
} }
foreach ($options as $sessionVar => $value) { foreach ($options as $sessionVar => $value) {
if (ini_set("session." . $sessionVar, $value) === false) { if (ini_set("session." . $sessionVar, $value) === false) {
Logger::warn( Logger::warn(
@ -95,9 +88,11 @@ class PhpSession extends Session
); );
} }
} }
if (!is_writable(session_save_path())) { if (!is_writable(session_save_path())) {
throw new ConfigurationError('Can\'t save session'); throw new ConfigurationError('Can\'t save session');
} }
$this->read(); $this->read();
} }
@ -139,7 +134,7 @@ class PhpSession extends Session
{ {
$this->open(); $this->open();
$_SESSION = array(); $_SESSION = array();
$this->setAll(array(), true); $this->clear();
session_destroy(); session_destroy();
$this->clearCookies(); $this->clearCookies();
session_write_close(); session_write_close();

View File

@ -0,0 +1,156 @@
<?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;
/**
* Base class for handling sessions
*/
abstract class Session
{
/**
* Container for session values
*
* @var array
*/
private $sessionValues = array();
/**
* Read all values from the underlying session implementation
*/
abstract public function read();
/**
* Persists changes to the underlying session implementation
*/
abstract public function write();
/**
* Purge session
*/
abstract public function purge();
/**
* Setter for session values
*
* Values need to be manually persisted with method write.
*
* @param string $key Name of value
* @param mixed $value Value to set
* @param string $namespace Namespace to use
*
* @return Session
* @see self::write
*/
public function set($key, $value, $namespace = null)
{
if ($namespace !== null) {
if (!isset($this->sessionValues[$namespace])) {
$this->sessionValues[$namespace] = array();
}
$this->sessionValues[$namespace][$key] = $value;
} else {
$this->sessionValues[$key] = $value;
}
return $this;
}
/**
* Getter for session values
*
* 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()
{
$this->sessionValues = array();
}
}

View File

@ -29,12 +29,11 @@
namespace Icinga; namespace Icinga;
use DateTimeZone; use \DateTimeZone;
use Exception; use \Exception;
use InvalidArgumentException; use \InvalidArgumentException;
use Icinga\User\Preferences; use Icinga\User\Preferences;
use Icinga\User\Message; use Icinga\User\Message;
use Icinga\Authentication\PhpSession;
use Icinga\Application\Config; use Icinga\Application\Config;
/** /**

View File

@ -29,7 +29,7 @@
namespace Icinga\User\Preferences; namespace Icinga\User\Preferences;
use Icinga\Authentication\Session; use Icinga\Session\Session;
use \SplObserver; use \SplObserver;
use \SplSubject; use \SplSubject;
use Icinga\User\Preferences; use Icinga\User\Preferences;

View File

@ -29,10 +29,10 @@
namespace Icinga\Web\Controller; namespace Icinga\Web\Controller;
use \Icinga\Application\Icinga;
use \Icinga\Authentication\Manager as AuthenticationManager;
use \Zend_Log; use \Zend_Log;
use \Icinga\User\Message; use Icinga\Web\Session;
use Icinga\User\Message;
use Icinga\Authentication\Manager as AuthenticationManager;
/** /**
* Base class for Configuration Controllers * Base class for Configuration Controllers
@ -55,7 +55,7 @@ class BaseConfigController extends ActionController
AuthenticationManager::getInstance()->getUser()->addMessage( AuthenticationManager::getInstance()->getUser()->addMessage(
new Message($msg, Zend_Log::INFO) new Message($msg, Zend_Log::INFO)
); );
AuthenticationManager::getInstance()->getSession()->write(); Session::getSession()->write();
} }
/** /**
@ -69,7 +69,7 @@ class BaseConfigController extends ActionController
AuthenticationManager::getInstance()->getUser()->addMessage( AuthenticationManager::getInstance()->getUser()->addMessage(
new Message($msg, Zend_Log::ERR) new Message($msg, Zend_Log::ERR)
); );
AuthenticationManager::getInstance()->getSession()->write(); Session::getSession()->write();
} }
/* /*

View File

@ -32,7 +32,7 @@ namespace Icinga\Web;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Icinga\Application\Platform; use Icinga\Application\Platform;
use Icinga\Application\Logger as Log; use Icinga\Application\Logger as Log;
use Icinga\Authentication\Manager as AuthManager; use Icinga\Web\Session;
/** /**
* // @TODO(eL): Use Notification not as Singleton but within request: * // @TODO(eL): Use Notification not as Singleton but within request:
@ -108,28 +108,31 @@ class Notification
); );
// Get, change, set - just to be on the safe side: // Get, change, set - just to be on the safe side:
$msgs = $this->session->messages; $session = Session::getSession();
$msgs = $session->messages;
$msgs[] = $mo; $msgs[] = $mo;
$this->session->messages = $msgs; $session->messages = $msgs;
} }
public function hasMessages() public function hasMessages()
{ {
return ! empty($this->session->messages); $session = Session::getSession();
return !empty($session->messages);
} }
public function getMessages() public function getMessages()
{ {
$msgs = $this->session->messages; $session = Session::getSession();
$this->session->messages = array(); $msgs = $session->messages;
$session->messages = array();
return $msgs; return $msgs;
} }
final private function __construct() final private function __construct()
{ {
$this->session = AuthManager::getInstance()->getSession(); $session = Session::getSession();
if (!is_array($this->session->get('messages'))) { if (!is_array($session->get('messages'))) {
$this->session->messages = array(); $session->messages = array();
} }
if (Platform::isCli()) { if (Platform::isCli()) {

View File

@ -0,0 +1,80 @@
<?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\Web;
use Icinga\Session\PhpSession;
use Icinga\Session\Session as BaseSession;
use Icinga\Exception\ProgrammingError;
/**
* Session container
*/
class Session
{
/**
* The current session
*
* @var BaseSession $session
*/
private static $session;
/**
* Create the session
*
* @param BaseSession $session
*
* @return BaseSession
*/
public static function create(BaseSession $session = null)
{
if ($session === null) {
self::$session = new PhpSession();
} else {
self::$session = $session;
}
return self::$session;
}
/**
* Return the current session
*
* @return BaseSession
*/
public static function getSession()
{
if (self::$session === null) {
throw new ProgrammingError('No session created yet');
}
return self::$session;
}
}

View File

@ -4,10 +4,11 @@ namespace Icinga\Web\Widget;
use \Zend_Log; use \Zend_Log;
use \Zend_Form; use \Zend_Form;
use \Icinga\User;
use \Icinga\User\Message;
use \Zend_View_Abstract; use \Zend_View_Abstract;
use \Icinga\Authentication\Manager as AuthenticationManager; use Icinga\User;
use Icinga\User\Message;
use Icinga\Web\Session;
use Icinga\Authentication\Manager as AuthenticationManager;
/** /**
* Displays a set of alert messages to the user. * Displays a set of alert messages to the user.
@ -28,7 +29,7 @@ class AlertMessageBox implements \Icinga\Web\Widget\Widget {
{ {
$messages = $this->user->getMessages(); $messages = $this->user->getMessages();
$this->user->clearMessages(); $this->user->clearMessages();
AuthenticationManager::getInstance()->getSession()->write(); Session::getSession()->write();
return $messages; return $messages;
} }

View File

@ -2,10 +2,6 @@
namespace Icinga\Module\Monitoring; namespace Icinga\Module\Monitoring;
use \Icinga\Application\Config;
use Icinga\Web\Session;
use Exception;
class Environment class Environment
{ {
protected static $envs = array( protected static $envs = array(

View File

@ -44,15 +44,17 @@ require_once BaseTestCase::$libDir . '/Authentication/Manager.php';
require_once BaseTestCase::$libDir . '/Authentication/Credential.php'; require_once BaseTestCase::$libDir . '/Authentication/Credential.php';
require_once BaseTestCase::$libDir . '/Exception/ConfigurationError.php'; require_once BaseTestCase::$libDir . '/Exception/ConfigurationError.php';
require_once BaseTestCase::$libDir . '/Exception/ProgrammingError.php'; require_once BaseTestCase::$libDir . '/Exception/ProgrammingError.php';
require_once BaseTestCase::$libDir . '/Web/Session.php';
require_once 'BackendMock.php'; require_once 'BackendMock.php';
require_once 'ErrorProneBackendMock.php'; require_once 'ErrorProneBackendMock.php';
require_once 'SessionMock.php'; require_once 'SessionMock.php';
// @codingStandardsIgnoreEnd // @codingStandardsIgnoreEnd
use \Zend_Config; use \Zend_Config;
use \Icinga\Authentication\Manager as AuthManager; use Icinga\Web\Session;
use \Icinga\Authentication\Credential; use Icinga\Authentication\Manager as AuthManager;
use \Icinga\Exception\ConfigurationError; use Icinga\Authentication\Credential;
use Icinga\Exception\ConfigurationError;
/** /**
* @backupStaticAttributes enabled * @backupStaticAttributes enabled
@ -83,11 +85,10 @@ class ManagerTest extends BaseTestCase
} }
$managerOptions = array( $managerOptions = array(
'sessionClass' => $session,
'writeSession' => $write,
'noDefaultConfig' => true 'noDefaultConfig' => true
); );
Session::create($session);
$manager = AuthManager::getInstance($managerConfig, $managerOptions); $manager = AuthManager::getInstance($managerConfig, $managerOptions);
if ($nobackend === false) { if ($nobackend === false) {
@ -124,11 +125,7 @@ class ManagerTest extends BaseTestCase
$this->assertInstanceOf('Icinga\User', $authMgr->getUser()); $this->assertInstanceOf('Icinga\User', $authMgr->getUser());
$this->assertSame('Username', $authMgr->getUser()->getUsername()); $this->assertSame('Username', $authMgr->getUser()->getUsername());
$this->assertInstanceOf( $session->isOpen = true;
'Tests\Icinga\Authentication\SessionMock',
$authMgr->getSession()
);
$authMgr->removeAuthorization(); $authMgr->removeAuthorization();
$this->assertNull($authMgr->getUser()); $this->assertNull($authMgr->getUser());

View File

@ -36,14 +36,14 @@ require_once realpath(__DIR__ . '/../../../../../library/Icinga/Test/BaseTestCas
use Icinga\Test\BaseTestCase; use Icinga\Test\BaseTestCase;
// @codingStandardsIgnoreStart // @codingStandardsIgnoreStart
require_once BaseTestCase::$libDir . '/Authentication/Session.php'; require_once BaseTestCase::$libDir . '/Session/Session.php';
require_once BaseTestCase::$libDir . '/Authentication/PhpSession.php'; require_once BaseTestCase::$libDir . '/Session/PhpSession.php';
require_once BaseTestCase::$libDir . '/Application/Logger.php'; require_once BaseTestCase::$libDir . '/Application/Logger.php';
require_once BaseTestCase::$libDir . '/Exception/ConfigurationError.php'; require_once BaseTestCase::$libDir . '/Exception/ConfigurationError.php';
require_once 'Zend/Log.php'; require_once 'Zend/Log.php';
// @codingStandardsIgnoreEnd // @codingStandardsIgnoreEnd
use Icinga\Authentication\PhpSession; use Icinga\Session\PhpSession;
class PhpSessionTest extends BaseTestCase class PhpSessionTest extends BaseTestCase
{ {

View File

@ -29,9 +29,9 @@
namespace Tests\Icinga\Authentication; namespace Tests\Icinga\Authentication;
require_once("../../library/Icinga/Authentication/Session.php"); require_once("../../library/Icinga/Session/Session.php");
use Icinga\Authentication\Session as Session; use Icinga\Session\Session;
class SessionMock extends Session class SessionMock extends Session
{ {