Merge branch 'bugfix/document-and-rename-ldap-connection-class-8954'

fixes #8954
fixes #8955
This commit is contained in:
Johannes Meyer 2015-06-24 09:26:18 +02:00
commit 31064a0dc5
14 changed files with 572 additions and 329 deletions

View File

@ -7,7 +7,7 @@ use Exception;
use Icinga\Web\Form; use Icinga\Web\Form;
use Icinga\Data\ConfigObject; use Icinga\Data\ConfigObject;
use Icinga\Data\ResourceFactory; use Icinga\Data\ResourceFactory;
use Icinga\Protocol\Ldap\Connection; use Icinga\Protocol\Ldap\LdapConnection;
/** /**
* Form class for adding/modifying ldap resources * Form class for adding/modifying ldap resources
@ -27,7 +27,7 @@ class LdapResourceForm extends Form
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
$defaultPort = ! array_key_exists('encryption', $formData) || $formData['encryption'] !== Connection::LDAPS $defaultPort = ! array_key_exists('encryption', $formData) || $formData['encryption'] !== LdapConnection::LDAPS
? 389 ? 389
: 636; : 636;
@ -75,9 +75,9 @@ class LdapResourceForm extends Form
. ' none for unencrypted communication' . ' none for unencrypted communication'
), ),
'multiOptions' => array( 'multiOptions' => array(
'none' => $this->translate('None', 'resource.ldap.encryption'), 'none' => $this->translate('None', 'resource.ldap.encryption'),
Connection::STARTTLS => 'STARTTLS', LdapConnection::STARTTLS => 'STARTTLS',
Connection::LDAPS => 'LDAPS' LdapConnection::LDAPS => 'LDAPS'
) )
) )
); );
@ -156,13 +156,8 @@ class LdapResourceForm extends Form
{ {
try { try {
$resource = ResourceFactory::createResource(new ConfigObject($form->getValues())); $resource = ResourceFactory::createResource(new ConfigObject($form->getValues()));
if (false === $resource->testCredentials( $resource->connect();
$form->getElement('bind_dn')->getValue(), $resource->bind();
$form->getElement('bind_pw')->getValue()
)
) {
throw new Exception(); // TODO: Get the exact error message
}
} catch (Exception $e) { } catch (Exception $e) {
$msg = $form->translate('Connectivity validation failed, connection to the given resource not possible.'); $msg = $form->translate('Connectivity validation failed, connection to the given resource not possible.');
if (($error = $e->getMessage())) { if (($error = $e->getMessage())) {

View File

@ -8,7 +8,7 @@ use Icinga\Authentication\User\UserBackend;
use Icinga\Authentication\UserGroup\LdapUserGroupBackend; use Icinga\Authentication\UserGroup\LdapUserGroupBackend;
use Icinga\Data\ConfigObject; use Icinga\Data\ConfigObject;
use Icinga\Data\ResourceFactory; use Icinga\Data\ResourceFactory;
use Icinga\Protocol\Ldap\Connection; use Icinga\Protocol\Ldap\LdapConnection;
use Icinga\Web\Form; use Icinga\Web\Form;
use Icinga\Web\Notification; use Icinga\Web\Notification;
@ -286,11 +286,11 @@ class LdapUserGroupBackendForm extends Form
/** /**
* Return the names of all configured LDAP user backends * Return the names of all configured LDAP user backends
* *
* @param Connection $resource * @param LdapConnection $resource
* *
* @return array * @return array
*/ */
protected function getLdapUserBackendNames(Connection $resource) protected function getLdapUserBackendNames(LdapConnection $resource)
{ {
$names = array(); $names = array();
foreach (Config::app('authentication') as $name => $config) { foreach (Config::app('authentication') as $name => $config) {

View File

@ -9,7 +9,7 @@ use Icinga\Exception\AuthenticationException;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Icinga\Repository\LdapRepository; use Icinga\Repository\LdapRepository;
use Icinga\Repository\RepositoryQuery; use Icinga\Repository\RepositoryQuery;
use Icinga\Protocol\Ldap\Exception as LdapException; use Icinga\Protocol\Ldap\LdapException;
use Icinga\Protocol\Ldap\Expression; use Icinga\Protocol\Ldap\Expression;
use Icinga\User; use Icinga\User;
@ -325,7 +325,7 @@ class LdapUserBackend extends LdapRepository implements UserBackendInterface
throw new AuthenticationException('Connection not possible.', $e); throw new AuthenticationException('Connection not possible.', $e);
} }
if ($result === null) { if ($result === false) {
throw new AuthenticationException( throw new AuthenticationException(
'No objects with objectClass "%s" in DN "%s" found. (Filter: %s)', 'No objects with objectClass "%s" in DN "%s" found. (Filter: %s)',
$this->userClass, $this->userClass,

View File

@ -8,7 +8,7 @@ use Icinga\Util\ConfigAwareFactory;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
use Icinga\Data\Db\DbConnection; use Icinga\Data\Db\DbConnection;
use Icinga\Protocol\Livestatus\Connection as LivestatusConnection; use Icinga\Protocol\Livestatus\Connection as LivestatusConnection;
use Icinga\Protocol\Ldap\Connection as LdapConnection; use Icinga\Protocol\Ldap\LdapConnection;
use Icinga\Protocol\File\FileReader; use Icinga\Protocol\File\FileReader;
/** /**

View File

@ -9,7 +9,7 @@ use Icinga\Protocol\Dns;
class Discovery { class Discovery {
/** /**
* @var Connection * @var LdapConnection
*/ */
private $connection; private $connection;
@ -21,9 +21,9 @@ class Discovery {
private $discovered = false; private $discovered = false;
/** /**
* @param Connection $conn The ldap connection to use for the discovery * @param LdapConnection $conn The ldap connection to use for the discovery
*/ */
public function __construct(Connection $conn) public function __construct(LdapConnection $conn)
{ {
$this->connection = $conn; $this->connection = $conn;
} }
@ -147,7 +147,7 @@ class Discovery {
*/ */
public static function discover($host, $port) public static function discover($host, $port)
{ {
$conn = new Connection(new ConfigObject(array( $conn = new LdapConnection(new ConfigObject(array(
'hostname' => $host, 'hostname' => $host,
'port' => $port 'port' => $port
))); )));

View File

@ -6,9 +6,9 @@ namespace Icinga\Protocol\Ldap;
use Icinga\Exception\IcingaException; use Icinga\Exception\IcingaException;
/** /**
* Class Exception * Class LdapException
* @package Icinga\Protocol\Ldap * @package Icinga\Protocol\Ldap
*/ */
class Exception extends IcingaException class LdapException extends IcingaException
{ {
} }

View File

@ -10,7 +10,7 @@ use Icinga\Exception\NotImplementedError;
/** /**
* LDAP query class * LDAP query class
*/ */
class Query extends SimpleQuery class LdapQuery extends SimpleQuery
{ {
/** /**
* This query's filters * This query's filters
@ -156,7 +156,7 @@ class Query extends SimpleQuery
{ {
$result = $this->fetchAll(); $result = $this->fetchAll();
$sorted = array(); $sorted = array();
$quotedDn = preg_quote($this->ds->getDN(), '/'); $quotedDn = preg_quote($this->ds->getDn(), '/');
foreach ($result as $key => & $item) { foreach ($result as $key => & $item) {
$new_key = LdapUtils::implodeDN( $new_key = LdapUtils::implodeDN(
array_reverse( array_reverse(
@ -186,7 +186,7 @@ class Query extends SimpleQuery
* *
* @return string|false The distinguished name or false in case it's not possible to fetch a result * @return string|false The distinguished name or false in case it's not possible to fetch a result
* *
* @throws Exception In case the query returns multiple results * @throws LdapException In case the query returns multiple results
* (i.e. it's not possible to fetch a unique DN) * (i.e. it's not possible to fetch a unique DN)
*/ */
public function fetchDn() public function fetchDn()
@ -199,12 +199,12 @@ class Query extends SimpleQuery
* *
* @return string * @return string
* *
* @throws Exception In case the objectClass filter does not exist * @throws LdapException In case the objectClass filter does not exist
*/ */
protected function renderFilter() protected function renderFilter()
{ {
if (! isset($this->filters['objectClass'])) { if (! isset($this->filters['objectClass'])) {
throw new Exception('Object class is mandatory'); throw new LdapException('Object class is mandatory');
} }
$parts = array(); $parts = array();

View File

@ -14,7 +14,7 @@ namespace Icinga\Protocol\Ldap;
class Node extends Root class Node extends Root
{ {
/** /**
* @var Connection * @var LdapConnection
*/ */
protected $connection; protected $connection;

View File

@ -22,7 +22,7 @@ class Root
protected $rdn; protected $rdn;
/** /**
* @var Connection * @var LdapConnection
*/ */
protected $connection; protected $connection;
@ -37,9 +37,9 @@ class Root
protected $props = array(); protected $props = array();
/** /**
* @param Connection $connection * @param LdapConnection $connection
*/ */
protected function __construct(Connection $connection) protected function __construct(LdapConnection $connection)
{ {
$this->connection = $connection; $this->connection = $connection;
} }
@ -53,10 +53,10 @@ class Root
} }
/** /**
* @param Connection $connection * @param LdapConnection $connection
* @return Root * @return Root
*/ */
public static function forConnection(Connection $connection) public static function forConnection(LdapConnection $connection)
{ {
$root = new Root($connection); $root = new Root($connection);
return $root; return $root;
@ -177,17 +177,17 @@ class Root
} }
/** /**
* @param Connection $connection * @param LdapConnection $connection
* @return $this * @return $this
*/ */
public function setConnection(Connection $connection) public function setConnection(LdapConnection $connection)
{ {
$this->connection = $connection; $this->connection = $connection;
return $this; return $this;
} }
/** /**
* @return Connection * @return LdapConnection
*/ */
public function getConnection() public function getConnection()
{ {
@ -215,7 +215,7 @@ class Root
*/ */
public function getDN() public function getDN()
{ {
return $this->connection->getDN(); return $this->connection->getDn();
} }
/** /**

View File

@ -3,7 +3,7 @@
namespace Icinga\Repository; namespace Icinga\Repository;
use Icinga\Protocol\Ldap\Connection; use Icinga\Protocol\Ldap\LdapConnection;
/** /**
* Abstract base class for concrete LDAP repository implementations * Abstract base class for concrete LDAP repository implementations
@ -18,7 +18,7 @@ abstract class LdapRepository extends Repository
/** /**
* The datasource being used * The datasource being used
* *
* @var Connection * @var LdapConnection
*/ */
protected $ds; protected $ds;
@ -40,9 +40,9 @@ abstract class LdapRepository extends Repository
/** /**
* Create a new LDAP repository object * Create a new LDAP repository object
* *
* @param Connection $ds The data source to use * @param LdapConnection $ds The data source to use
*/ */
public function __construct(Connection $ds) public function __construct(LdapConnection $ds)
{ {
parent::__construct($ds); parent::__construct($ds);
} }

View File

@ -26,7 +26,7 @@ class LdapResourceFormTest extends BaseTestCase
public function testValidLdapResourceIsValid() public function testValidLdapResourceIsValid()
{ {
$this->setUpResourceFactoryMock( $this->setUpResourceFactoryMock(
Mockery::mock()->shouldReceive('testCredentials')->once()->andReturn(true)->getMock() Mockery::mock()->shouldReceive('connect')->once()->shouldReceive('bind')->once()->getMock()
); );
// Passing array(null) is required to make Mockery call the constructor... // Passing array(null) is required to make Mockery call the constructor...
@ -49,7 +49,7 @@ class LdapResourceFormTest extends BaseTestCase
public function testInvalidLdapResourceIsNotValid() public function testInvalidLdapResourceIsNotValid()
{ {
$this->setUpResourceFactoryMock( $this->setUpResourceFactoryMock(
Mockery::mock()->shouldReceive('testCredentials')->once()->andThrow('\Exception')->getMock() Mockery::mock()->shouldReceive('connect')->once()->shouldReceive('bind')->andThrow('\Exception')->getMock()
); );
// Passing array(null) is required to make Mockery call the constructor... // Passing array(null) is required to make Mockery call the constructor...

View File

@ -739,7 +739,6 @@ inkey' => 'blarg'
); );
$rendered = $writer->render(); $rendered = $writer->render();
var_dump($rendered);
$this->assertEquals( $this->assertEquals(
count(explode("\n", $rendered)), count(explode("\n", $rendered)),
4, 4,

View File

@ -5,7 +5,7 @@ namespace Tests\Icinga\Protocol\Ldap;
use Icinga\Data\ConfigObject; use Icinga\Data\ConfigObject;
use Icinga\Test\BaseTestCase; use Icinga\Test\BaseTestCase;
use Icinga\Protocol\Ldap\Connection; use Icinga\Protocol\Ldap\LdapConnection;
class QueryTest extends BaseTestCase class QueryTest extends BaseTestCase
{ {
@ -20,7 +20,7 @@ class QueryTest extends BaseTestCase
) )
); );
$connection = new Connection($config); $connection = new LdapConnection($config);
return $connection->select(); return $connection->select();
} }