From ac0b0415236e24d6890d3c55dcca18c0eed729bc Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 23 Jan 2014 12:09:48 +0100 Subject: [PATCH] Remove session handling from the authentication manager refs #5510 --- library/Icinga/Application/Web.php | 45 +++-- library/Icinga/Authentication/Manager.php | 145 +++++++--------- library/Icinga/Authentication/Session.php | 131 --------------- .../PhpSession.php | 27 ++- library/Icinga/Session/Session.php | 156 ++++++++++++++++++ library/Icinga/User.php | 7 +- .../Icinga/User/Preferences/SessionStore.php | 2 +- .../Web/Controller/BaseConfigController.php | 10 +- library/Icinga/Web/Notification.php | 21 ++- library/Icinga/Web/Session.php | 80 +++++++++ library/Icinga/Web/Widget/AlertMessageBox.php | 19 ++- .../library/Monitoring/Environment.php | 4 - .../Icinga/Authentication/ManagerTest.php | 17 +- .../Icinga/Authentication/PhpSessionTest.php | 6 +- .../Icinga/Authentication/SessionMock.php | 4 +- 15 files changed, 379 insertions(+), 295 deletions(-) delete mode 100644 library/Icinga/Authentication/Session.php rename library/Icinga/{Authentication => Session}/PhpSession.php (83%) create mode 100644 library/Icinga/Session/Session.php create mode 100644 library/Icinga/Web/Session.php diff --git a/library/Icinga/Application/Web.php b/library/Icinga/Application/Web.php index 6ea8ab359..ad4c58d3c 100644 --- a/library/Icinga/Application/Web.php +++ b/library/Icinga/Application/Web.php @@ -37,17 +37,19 @@ use \Zend_View_Helper_PaginationControl; use \Zend_Controller_Action_HelperBroker; use \Zend_Controller_Router_Route; use \Zend_Controller_Front; -use \Icinga\Application\Logger; -use \Icinga\Authentication\Manager as AuthenticationManager; -use \Icinga\Exception\ConfigurationError; -use \Icinga\User\Preferences; -use \Icinga\User\Preferences\LoadInterface; -use \Icinga\User; -use \Icinga\Web\Request; -use \Icinga\Web\View; -use \Icinga\User\Preferences\StoreFactory; -use \Icinga\User\Preferences\SessionStore; -use \Icinga\Util\DateTimeFactory; +use Icinga\Application\Logger; +use Icinga\Authentication\Manager as AuthenticationManager; +use Icinga\Exception\ConfigurationError; +use Icinga\User\Preferences; +use Icinga\User\Preferences\LoadInterface; +use Icinga\User; +use Icinga\Web\Request; +use Icinga\Web\View; +use Icinga\User\Preferences\StoreFactory; +use Icinga\User\Preferences\SessionStore; +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 @@ -81,6 +83,13 @@ class Web extends ApplicationBootstrap */ private $request; + /** + * Session object + * + * @var BaseSession + */ + private $session; + /** * User object * @@ -105,6 +114,7 @@ class Web extends ApplicationBootstrap return $this->setupConfig() ->setupErrorHandling() ->setupResourceFactory() + ->setupSession() ->setupUser() ->setupTimezone() ->setupRequest() @@ -239,7 +249,7 @@ class Web extends ApplicationBootstrap $user = $authenticationManager->getUser(); // 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 // stored in session @@ -323,6 +333,17 @@ class Web extends ApplicationBootstrap return $this; } + /** + * Initialize a session provider + * + * @return self + */ + private function setupSession() + { + $this->session = Session::create(); + return $this; + } + /** * Inject dependencies into request * diff --git a/library/Icinga/Authentication/Manager.php b/library/Icinga/Authentication/Manager.php index 0783ae051..45e8bd566 100644 --- a/library/Icinga/Authentication/Manager.php +++ b/library/Icinga/Authentication/Manager.php @@ -4,7 +4,7 @@ * This file is part of Icinga Web 2. * * 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 * 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 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - * @copyright 2013 Icinga Development Team + * @copyright 2014 Icinga Development Team * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team * @@ -30,32 +30,26 @@ namespace Icinga\Authentication; use \Exception; -use Icinga\Exception\ConfigurationError; use \Zend_Config; -use \Icinga\User; -use \Icinga\Data\ResourceFactory; -use \Icinga\Application\Logger; -use \Icinga\Application\Config as IcingaConfig; -use \Icinga\Authentication\Backend\DbUserBackend; -use \Icinga\Authentication\Backend\LdapUserBackend; -use \Icinga\Exception\ConfigurationError as ConfigError; +use Icinga\User; +use Icinga\Web\Session; +use Icinga\Data\ResourceFactory; +use Icinga\Application\Logger; +use Icinga\Exception\ConfigurationError; +use Icinga\Application\Config as IcingaConfig; +use Icinga\Authentication\Backend\DbUserBackend; +use Icinga\Authentication\Backend\LdapUserBackend; /** - * The authentication manager allows to identify users and - * to persist authentication information in a session. + * The authentication manager allows to identify users and + * to persist authentication information in a session. * - * Direct instantiation is not permitted, the AuthenticationManager - * must be created using the getInstance method. Subsequent getInstance - * calls return the same object and ignore any additional configuration + * Direct instantiation is not permitted, the AuthenticationManager + * must be created using the getInstance method. Subsequent getInstance + * 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) **/ class Manager { @@ -64,19 +58,19 @@ class Manager * * @var self */ - private static $instance = null; + private static $instance; /** * Instance of authenticated user * * @var User **/ - private $user = null; + private $user; /** * Array of user backends * - * @var UserBackend[] + * @var array **/ private $userBackends = array(); @@ -87,13 +81,6 @@ class Manager **/ private $groupBackends = array(); - /** - * Session - * - * @var Session - **/ - private $session = null; - /** * The configuration * @@ -117,19 +104,12 @@ class Manager * instead of the authentication.ini * @param array $options Additional options that affect the managers behaviour. * Supported values: - * * writeSession: Whether the session should be writable - * * sessionClass: Allows to provide a different session implementation) * * noDefaultConfig: Disable default configuration from authentication.ini **/ private function __construct(Zend_Config $config = null, array $options = array()) { if ($config === null && !(isset($options['noDefaultConfig']) && $options['noDefaultConfig'] == true)) { - $config = IcingaConfig::app('authentication'); - } - if (!isset($options['sessionClass'])) { - $this->session = new PhpSession(); - } else { - $this->session = $options['sessionClass']; + $config = IcingaConfig::app('authentication'); } $this->config = $config; } @@ -137,8 +117,8 @@ class Manager /** * Get a singleton instance of our self * - * @param Zend_Config $config - * @param array $options + * @param Zend_Config $config + * @param array $options * * @return self * @see Manager:__construct @@ -177,9 +157,9 @@ 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 * * @return null|UserBackend */ @@ -211,16 +191,16 @@ class Manager default: 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()); } return null; } /** - * Add a user backend to stack + * Add a user backend to the stack * - * @param UserBackend $userBackend + * @param UserBackend $userBackend */ public function addUserBackend(UserBackend $userBackend) { @@ -230,21 +210,20 @@ class Manager /** * Get a user backend by name * - * @param string $name + * @param string $name * * @return UserBackend|null */ public function getUserBackend($name) { $this->initBackends(); - return (isset($this->userBackends[$name])) ? - $this->userBackends[$name] : null; + return (isset($this->userBackends[$name])) ? $this->userBackends[$name] : null; } /** - * Add a group backend to stack + * Add a group backend to the stack * - * @param GroupBackend $groupBackend + * @param GroupBackend $groupBackend */ public function addGroupBackend(GroupBackend $groupBackend) { @@ -254,21 +233,20 @@ class Manager /** * Get a group backend by name * - * @param string $name + * @param string $name * * @return GroupBackend|null */ public function getGroupBackend($name) { $this->initBackends(); - return (isset($this->groupBackends[$name])) ? - $this->groupBackends[$name] : null; + return (isset($this->groupBackends[$name])) ? $this->groupBackends[$name] : null; } /** - * Find a backend for a credential + * Find a backend for the given credentials * - * @param Credential $credentials + * @param Credential $credentials * * @return UserBackend|null * @throws ConfigurationError @@ -313,9 +291,8 @@ class Manager if ($authErrors >= count($this->userBackends)) { Logger::fatal('AuthManager: No working backend found, unable to authenticate any user'); throw new ConfigurationError( - 'No working backend found. Unable to authenticate any user.' - . "\n" - . 'Please examine the logs for more information.' + 'No working backend found. Unable to authenticate any user.' . + "\nPlease 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 Boolean $persist Whether to persist the authentication result - * in the current session + * @param Credential $credentials The credentials to use for authentication + * @param Boolean $persist Whether to persist the authentication result in the current session * - * @return Boolean true on success, otherwise false - * @throws ConfigError + * @return Boolean Whether the authentication was successful or not + * @throws ConfigurationError */ public function authenticate(Credential $credentials, $persist = true) { $this->initBackends(); if (count($this->userBackends) === 0) { Logger::error('AuthManager: No authentication backend provided, your users will never be able to login.'); - throw new ConfigError( - 'No authentication backend set - login will never succeed as icinga-web ' - . 'doesn\'t know how to determine your user. ' . "\n" - . 'To fix this error, setup your authentication.ini with at least one valid authentication backend.' + throw new ConfigurationError( + 'No authentication backend set - login will never succeed as icinga-web ' . + 'doesn\'t know how to determine your user. ' . "\n" . + '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); - if ($this->user == null) { + if ($this->user === null) { Logger::info('AuthManager: Invalid credentials for user %s provided', $credentials->getUsername()); return false; } @@ -377,7 +353,6 @@ class Manager if ($persist == true) { $this->persistCurrentUser(); - $this->session->write(); } 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() { - $this->session->set('user', $this->user); + $session = Session::getSession(); + $session->set('user', $this->user); + $session->write(); } /** @@ -398,13 +375,13 @@ class Manager **/ public function authenticateFromSession() { - $this->user = $this->session->get('user', null); + $this->user = Session::getSession()->get('user'); } /** * Returns true when the user is currently authenticated * - * @param Boolean $ignoreSession Set to true to prevent authentication by session + * @param Boolean $ignoreSession Set to true to prevent authentication by session * * @return bool */ @@ -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() { $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 * @see User::getGroups @@ -486,14 +463,4 @@ class Manager { return $this->user->getGroups(); } - - /** - * Getter for session - * - * @return Session - */ - public function getSession() - { - return $this->session; - } } diff --git a/library/Icinga/Authentication/Session.php b/library/Icinga/Authentication/Session.php deleted file mode 100644 index 8959549a6..000000000 --- a/library/Icinga/Authentication/Session.php +++ /dev/null @@ -1,131 +0,0 @@ - - * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 - * @author Icinga Development Team - * - */ -// {{{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(); - } -} diff --git a/library/Icinga/Authentication/PhpSession.php b/library/Icinga/Session/PhpSession.php similarity index 83% rename from library/Icinga/Authentication/PhpSession.php rename to library/Icinga/Session/PhpSession.php index fea5b3663..c6492f0c5 100644 --- a/library/Icinga/Authentication/PhpSession.php +++ b/library/Icinga/Session/PhpSession.php @@ -4,7 +4,7 @@ * This file is part of Icinga Web 2. * * 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 * 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 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - * @copyright 2013 Icinga Development Team + * @copyright 2014 Icinga Development Team * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team * */ // {{{ICINGA_LICENSE_HEADER}}} -namespace Icinga\Authentication; +namespace Icinga\Session; use Icinga\Application\Logger; use \Icinga\Exception\ConfigurationError; /** * 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 { @@ -70,7 +61,7 @@ class PhpSession extends Session /** * 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 * @see http://php.net/manual/en/session.configuration.php @@ -78,14 +69,16 @@ class PhpSession extends Session public function __construct(array $options = null) { if ($options !== null) { - $options = array_merge(PhpSession::$defaultCookieOptions, $options); + $options = array_merge(self::$defaultCookieOptions, $options); } else { - $options = PhpSession::$defaultCookieOptions; + $options = self::$defaultCookieOptions; } + if (array_key_exists('test_session_name', $options)) { $this->sessionName = $options['test_session_name']; unset($options['test_session_name']); } + foreach ($options as $sessionVar => $value) { if (ini_set("session." . $sessionVar, $value) === false) { Logger::warn( @@ -95,9 +88,11 @@ class PhpSession extends Session ); } } + if (!is_writable(session_save_path())) { throw new ConfigurationError('Can\'t save session'); } + $this->read(); } @@ -139,7 +134,7 @@ class PhpSession extends Session { $this->open(); $_SESSION = array(); - $this->setAll(array(), true); + $this->clear(); session_destroy(); $this->clearCookies(); session_write_close(); diff --git a/library/Icinga/Session/Session.php b/library/Icinga/Session/Session.php new file mode 100644 index 000000000..3f96e5bd4 --- /dev/null +++ b/library/Icinga/Session/Session.php @@ -0,0 +1,156 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + * + */ +// {{{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(); + } +} diff --git a/library/Icinga/User.php b/library/Icinga/User.php index 3c278db2c..ba0c98f5f 100644 --- a/library/Icinga/User.php +++ b/library/Icinga/User.php @@ -29,12 +29,11 @@ namespace Icinga; -use DateTimeZone; -use Exception; -use InvalidArgumentException; +use \DateTimeZone; +use \Exception; +use \InvalidArgumentException; use Icinga\User\Preferences; use Icinga\User\Message; -use Icinga\Authentication\PhpSession; use Icinga\Application\Config; /** diff --git a/library/Icinga/User/Preferences/SessionStore.php b/library/Icinga/User/Preferences/SessionStore.php index c9080a9ee..5c939c9db 100644 --- a/library/Icinga/User/Preferences/SessionStore.php +++ b/library/Icinga/User/Preferences/SessionStore.php @@ -29,7 +29,7 @@ namespace Icinga\User\Preferences; -use Icinga\Authentication\Session; +use Icinga\Session\Session; use \SplObserver; use \SplSubject; use Icinga\User\Preferences; diff --git a/library/Icinga/Web/Controller/BaseConfigController.php b/library/Icinga/Web/Controller/BaseConfigController.php index 5ca93e9aa..0a38f423d 100644 --- a/library/Icinga/Web/Controller/BaseConfigController.php +++ b/library/Icinga/Web/Controller/BaseConfigController.php @@ -29,10 +29,10 @@ namespace Icinga\Web\Controller; -use \Icinga\Application\Icinga; -use \Icinga\Authentication\Manager as AuthenticationManager; 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 @@ -55,7 +55,7 @@ class BaseConfigController extends ActionController AuthenticationManager::getInstance()->getUser()->addMessage( 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( new Message($msg, Zend_Log::ERR) ); - AuthenticationManager::getInstance()->getSession()->write(); + Session::getSession()->write(); } /* diff --git a/library/Icinga/Web/Notification.php b/library/Icinga/Web/Notification.php index 47192f6cb..0d4762b11 100644 --- a/library/Icinga/Web/Notification.php +++ b/library/Icinga/Web/Notification.php @@ -32,7 +32,7 @@ namespace Icinga\Web; use Icinga\Exception\ProgrammingError; use Icinga\Application\Platform; 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: @@ -108,28 +108,31 @@ class Notification ); // Get, change, set - just to be on the safe side: - $msgs = $this->session->messages; + $session = Session::getSession(); + $msgs = $session->messages; $msgs[] = $mo; - $this->session->messages = $msgs; + $session->messages = $msgs; } public function hasMessages() { - return ! empty($this->session->messages); + $session = Session::getSession(); + return !empty($session->messages); } public function getMessages() { - $msgs = $this->session->messages; - $this->session->messages = array(); + $session = Session::getSession(); + $msgs = $session->messages; + $session->messages = array(); return $msgs; } final private function __construct() { - $this->session = AuthManager::getInstance()->getSession(); - if (!is_array($this->session->get('messages'))) { - $this->session->messages = array(); + $session = Session::getSession(); + if (!is_array($session->get('messages'))) { + $session->messages = array(); } if (Platform::isCli()) { diff --git a/library/Icinga/Web/Session.php b/library/Icinga/Web/Session.php new file mode 100644 index 000000000..b441f24d8 --- /dev/null +++ b/library/Icinga/Web/Session.php @@ -0,0 +1,80 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + * + */ +// {{{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; + } +} diff --git a/library/Icinga/Web/Widget/AlertMessageBox.php b/library/Icinga/Web/Widget/AlertMessageBox.php index 9bf90facf..111e0cbec 100644 --- a/library/Icinga/Web/Widget/AlertMessageBox.php +++ b/library/Icinga/Web/Widget/AlertMessageBox.php @@ -4,17 +4,18 @@ namespace Icinga\Web\Widget; use \Zend_Log; use \Zend_Form; -use \Icinga\User; -use \Icinga\User\Message; 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. * - * The messages are fetched automatically from the current AuthenticationManager, - * but this is done lazily when render() is called, to ensure that messages will - * always be displayed before they are cleared. + * The messages are fetched automatically from the current AuthenticationManager, + * but this is done lazily when render() is called, to ensure that messages will + * always be displayed before they are cleared. */ class AlertMessageBox implements \Icinga\Web\Widget\Widget { @@ -28,7 +29,7 @@ class AlertMessageBox implements \Icinga\Web\Widget\Widget { { $messages = $this->user->getMessages(); $this->user->clearMessages(); - AuthenticationManager::getInstance()->getSession()->write(); + Session::getSession()->write(); return $messages; } @@ -77,7 +78,7 @@ class AlertMessageBox implements \Icinga\Web\Widget\Widget { * in this AlertMessageBox. Defaults to false */ public function __construct($showUserMessages = false) { - if ($showUserMessages) { + if ($showUserMessages) { $this->user = AuthenticationManager::getInstance()->getUser(); } } diff --git a/modules/monitoring/library/Monitoring/Environment.php b/modules/monitoring/library/Monitoring/Environment.php index a218ccb78..a1e41d732 100644 --- a/modules/monitoring/library/Monitoring/Environment.php +++ b/modules/monitoring/library/Monitoring/Environment.php @@ -2,10 +2,6 @@ namespace Icinga\Module\Monitoring; -use \Icinga\Application\Config; -use Icinga\Web\Session; -use Exception; - class Environment { protected static $envs = array( diff --git a/test/php/library/Icinga/Authentication/ManagerTest.php b/test/php/library/Icinga/Authentication/ManagerTest.php index 18e58ff9f..4afd882f1 100644 --- a/test/php/library/Icinga/Authentication/ManagerTest.php +++ b/test/php/library/Icinga/Authentication/ManagerTest.php @@ -44,15 +44,17 @@ require_once BaseTestCase::$libDir . '/Authentication/Manager.php'; require_once BaseTestCase::$libDir . '/Authentication/Credential.php'; require_once BaseTestCase::$libDir . '/Exception/ConfigurationError.php'; require_once BaseTestCase::$libDir . '/Exception/ProgrammingError.php'; +require_once BaseTestCase::$libDir . '/Web/Session.php'; require_once 'BackendMock.php'; require_once 'ErrorProneBackendMock.php'; require_once 'SessionMock.php'; // @codingStandardsIgnoreEnd use \Zend_Config; -use \Icinga\Authentication\Manager as AuthManager; -use \Icinga\Authentication\Credential; -use \Icinga\Exception\ConfigurationError; +use Icinga\Web\Session; +use Icinga\Authentication\Manager as AuthManager; +use Icinga\Authentication\Credential; +use Icinga\Exception\ConfigurationError; /** * @backupStaticAttributes enabled @@ -83,11 +85,10 @@ class ManagerTest extends BaseTestCase } $managerOptions = array( - 'sessionClass' => $session, - 'writeSession' => $write, 'noDefaultConfig' => true ); + Session::create($session); $manager = AuthManager::getInstance($managerConfig, $managerOptions); if ($nobackend === false) { @@ -124,11 +125,7 @@ class ManagerTest extends BaseTestCase $this->assertInstanceOf('Icinga\User', $authMgr->getUser()); $this->assertSame('Username', $authMgr->getUser()->getUsername()); - $this->assertInstanceOf( - 'Tests\Icinga\Authentication\SessionMock', - $authMgr->getSession() - ); - + $session->isOpen = true; $authMgr->removeAuthorization(); $this->assertNull($authMgr->getUser()); diff --git a/test/php/library/Icinga/Authentication/PhpSessionTest.php b/test/php/library/Icinga/Authentication/PhpSessionTest.php index cf7e2fee9..9a9c9f721 100644 --- a/test/php/library/Icinga/Authentication/PhpSessionTest.php +++ b/test/php/library/Icinga/Authentication/PhpSessionTest.php @@ -36,14 +36,14 @@ require_once realpath(__DIR__ . '/../../../../../library/Icinga/Test/BaseTestCas use Icinga\Test\BaseTestCase; // @codingStandardsIgnoreStart -require_once BaseTestCase::$libDir . '/Authentication/Session.php'; -require_once BaseTestCase::$libDir . '/Authentication/PhpSession.php'; +require_once BaseTestCase::$libDir . '/Session/Session.php'; +require_once BaseTestCase::$libDir . '/Session/PhpSession.php'; require_once BaseTestCase::$libDir . '/Application/Logger.php'; require_once BaseTestCase::$libDir . '/Exception/ConfigurationError.php'; require_once 'Zend/Log.php'; // @codingStandardsIgnoreEnd -use Icinga\Authentication\PhpSession; +use Icinga\Session\PhpSession; class PhpSessionTest extends BaseTestCase { diff --git a/test/php/library/Icinga/Authentication/SessionMock.php b/test/php/library/Icinga/Authentication/SessionMock.php index 697269739..b63e80025 100644 --- a/test/php/library/Icinga/Authentication/SessionMock.php +++ b/test/php/library/Icinga/Authentication/SessionMock.php @@ -29,9 +29,9 @@ 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 {