Change all UserBackends to a lazy connection-behavior

refs #5506
This commit is contained in:
Matthias Jentsch 2014-01-22 11:38:47 +01:00
parent ad56868af5
commit 85dbd86e5b
8 changed files with 50 additions and 18 deletions

View File

@ -145,6 +145,7 @@ class DbBackendForm extends BaseBackendForm
)
)
);
$dbBackend->connect();
if ($dbBackend->getUserCount() < 1) {
$this->addErrorMessage("No users found under the specified database backend");
return false;

View File

@ -35,7 +35,6 @@ use \Zend_Config;
use \Zend_Db;
use \Zend_Db_Adapter_Abstract;
use \Icinga\Data\ResourceFactory;
use \Icinga\Data\Db\Connection as DbConnection;
use \Icinga\User;
use \Icinga\Authentication\UserBackend;
use \Icinga\Authentication\Credential;
@ -118,7 +117,7 @@ class DbUserBackend implements UserBackend
* instance of Zend_Db_Adapter_Abstract
* 'name' => The name of this authentication backend
*
* @throws Exception When the connection to the resource is not possible.
* @throws ConfigurationError When the given resource does not exist.
*/
public function __construct(Zend_Config $config)
{
@ -132,9 +131,6 @@ class DbUserBackend implements UserBackend
$resource = ResourceFactory::createResource(ResourceFactory::getResourceConfig($config->resource));
$this->db = $resource->getConnection();
}
// test the connection
$this->db->getConnection();
}
/**
@ -355,4 +351,14 @@ class DbUserBackend implements UserBackend
$query = $this->db->select()->from($this->userTable, 'COUNT(*) as count')->query();
return $query->fetch()->count;
}
/**
* Try to connect to the underlying database.
*
* @throws ConfigurationError When the backend is not reachable with the given configuration.
*/
public function connect()
{
$this->db->getConnection();
}
}

View File

@ -69,12 +69,12 @@ class LdapUserBackend implements UserBackend
/**
* Create a new LdapUserBackend
*
* @param Zend_Config $config The configuration for this authentication backend.
* 'resource' => The name of the resource to use, or an actual
* @param Zend_Config $config The configuration for this authentication backend.
* 'resource' => The name of the resource to use, or an actual
* instance of \Icinga\Protocol\Ldap\Connection.
* 'name' => The name of this authentication backend.
* 'name' => The name of this authentication backend.
*
* @throws \Exception When the connection to the resource is not possible.
* @throws ConfigurationError When the given resource does not exist.
*/
public function __construct(Zend_Config $config)
{
@ -83,7 +83,6 @@ class LdapUserBackend implements UserBackend
}
$this->config = $config;
$this->name = $config->name;
if ($config->resource instanceof LdapConnection) {
$this->connection = $config->resource;
} else {
@ -91,8 +90,6 @@ class LdapUserBackend implements UserBackend
ResourceFactory::getResourceConfig($config->resource)
);
}
// will throw an exception, when the connection is not possible.
$this->connection->connect();
}
/**
@ -190,4 +187,15 @@ class LdapUserBackend implements UserBackend
)
);
}
/**
*
* Establish the connection to this authentication backend
*
* @throws \Exception When the connection to the resource is not possible.
*/
public function connect()
{
$this->connection->connect();
}
}

View File

@ -34,13 +34,10 @@ use Icinga\Exception\ConfigurationError;
use \Zend_Config;
use \Icinga\User;
use \Icinga\Data\ResourceFactory;
use \Icinga\Data\Db\Connection as DbConnection;
use \Icinga\Application\Logger;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Protocol\Ldap\Connection as LdapConnection;
use \Icinga\Authentication\Backend\DbUserBackend;
use \Icinga\Authentication\Backend\LdapUserBackend;
use \Icinga\Exception\ProgrammingError;
use \Icinga\Exception\ConfigurationError as ConfigError;
@ -159,11 +156,11 @@ class Manager
$backendConfig->name = $name;
}
$backend = $this->createBackend($backendConfig);
if ($backend instanceof UserBackend) {
$backend->connect();
$this->userBackends[$backend->getName()] = $backend;
} elseif ($backend instanceof GroupBackend) {
$backend->connect();
$this->groupBackends[$backend->getName()] = $backend;
}
}

View File

@ -69,4 +69,11 @@ interface UserBackend
* @return int
*/
public function getUserCount();
/**
* Establish the connection to this authentication backend
*
* @throws ConfigurationError When the backend is not reachable with the given configuration.
*/
public function connect();
}

View File

@ -121,4 +121,9 @@ class BackendMock implements UserBackend
{
$this->allowedCredentials = $credentials;
}
public function connect()
{
}
}

View File

@ -131,6 +131,7 @@ class DbUserBackendTest extends BaseTestCase
{
$this->setupDbProvider($db);
$backend = new DbUserBackend($this->createDbBackendConfig($db));
$backend->connect();
$this->runBackendAuthentication($backend);
$this->runBackendUsername($backend);
}
@ -144,6 +145,7 @@ class DbUserBackendTest extends BaseTestCase
{
$this->setupDbProvider($db);
$backend = new DbUserBackend($this->createDbBackendConfig($db));
$backend->connect();
$this->runBackendAuthentication($backend);
$this->runBackendUsername($backend);
}
@ -281,7 +283,7 @@ class DbUserBackendTest extends BaseTestCase
$testName = 'test-name-123123';
$backend = new DbUserBackend($this->createDbBackendConfig($db, $testName));
$backend->connect();
$this->assertSame($testName, $backend->getName());
}
@ -293,6 +295,7 @@ class DbUserBackendTest extends BaseTestCase
$this->setupDbProvider($db);
$testName = 'test-name-123123';
$backend = new DbUserBackend($this->createDbBackendConfig($db, $testName));
$backend->connect();
$this->assertGreaterThan(0, $backend->getUserCount());
}
@ -305,6 +308,7 @@ class DbUserBackendTest extends BaseTestCase
$this->setupDbProvider($db);
$testName = 'test-name-123123';
$backend = new DbUserBackend($this->createDbBackendConfig($db, $testName));
$backend->connect();
$this->assertGreaterThan(0, $backend->getUserCount());
}

View File

@ -125,4 +125,8 @@ class ErrorProneBackendMock implements UserBackend
throw new Exception('getUserCount error: No users in this error prone backend');
}
public function connect()
{
}
}