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_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
*

View File

@ -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 <info@icinga.org>
* @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>
*
@ -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;
}
}

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.
*
* 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 <info@icinga.org>
* @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\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();

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;
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;
/**

View File

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

View File

@ -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();
}
/*

View File

@ -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()) {

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,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();
}
}

View File

@ -2,10 +2,6 @@
namespace Icinga\Module\Monitoring;
use \Icinga\Application\Config;
use Icinga\Web\Session;
use Exception;
class Environment
{
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 . '/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());

View File

@ -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
{

View File

@ -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
{