Change setup behavior of the AuthenticationManager to a lazy approach

The authentication manager should only try to set up all backends when
necessary. Remove the backend setup from the constructor and call it only when it is actually needed.

fixes #5506
This commit is contained in:
Matthias Jentsch 2014-01-22 12:50:17 +01:00
parent 85dbd86e5b
commit 226eb4eded

View File

@ -94,6 +94,20 @@ class Manager
**/ **/
private $session = null; private $session = null;
/**
* The configuration
*
* @var Zend_Config
*/
private $config = null;
/**
* If the backends are already created.
*
* @var Boolean
*/
private $initialized = false;
/** /**
* Creates a new authentication manager using the provided config (or the * Creates a new authentication manager using the provided config (or the
* configuration provided in the authentication.ini if no config is given) * configuration provided in the authentication.ini if no config is given)
@ -112,16 +126,12 @@ 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 ($config !== null) {
$this->setupBackends($config);
}
if (!isset($options['sessionClass'])) { if (!isset($options['sessionClass'])) {
$this->session = new PhpSession(); $this->session = new PhpSession();
} else { } else {
$this->session = $options['sessionClass']; $this->session = $options['sessionClass'];
} }
$this->config = $config;
} }
/** /**
@ -226,6 +236,7 @@ class Manager
*/ */
public function getUserBackend($name) public function getUserBackend($name)
{ {
$this->initBackends();
return (isset($this->userBackends[$name])) ? return (isset($this->userBackends[$name])) ?
$this->userBackends[$name] : null; $this->userBackends[$name] : null;
} }
@ -249,6 +260,7 @@ class Manager
*/ */
public function getGroupBackend($name) public function getGroupBackend($name)
{ {
$this->initBackends();
return (isset($this->groupBackends[$name])) ? return (isset($this->groupBackends[$name])) ?
$this->groupBackends[$name] : null; $this->groupBackends[$name] : null;
} }
@ -263,8 +275,9 @@ class Manager
*/ */
private function getBackendForCredential(Credential $credentials) private function getBackendForCredential(Credential $credentials)
{ {
$authErrors = 0; $this->initBackends();
$authErrors = 0;
foreach ($this->userBackends as $userBackend) { foreach ($this->userBackends as $userBackend) {
$flag = false; $flag = false;
@ -309,6 +322,17 @@ class Manager
return null; return null;
} }
/**
* Ensures that all backends are initialized
*/
private function initBackends()
{
if (!$this->initialized) {
$this->setupBackends($this->config);
$this->initialized = true;
}
}
/** /**
* Try to authenticate the current user with the Credential (@see Credential). * Try to authenticate the current user with the Credential (@see Credential).
* *
@ -321,6 +345,7 @@ class Manager
*/ */
public function authenticate(Credential $credentials, $persist = true) public function authenticate(Credential $credentials, $persist = true)
{ {
$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 ConfigError(