diff --git a/src/Configuration/DomainConfiguration.php b/src/Configuration/DomainConfiguration.php index bc37898f..425fbd9e 100644 --- a/src/Configuration/DomainConfiguration.php +++ b/src/Configuration/DomainConfiguration.php @@ -29,6 +29,9 @@ class DomainConfiguration // The port to use for connecting to your hosts. 'port' => LdapInterface::PORT, + // The protocol to use for connecting to your hosts (ldap:// or ldaps://). + 'protocol' => null, + // The base distinguished name of your domain. 'base_dn' => '', diff --git a/src/Connection.php b/src/Connection.php index fa6696f2..e04393a2 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -135,7 +135,11 @@ public function initialize(): void { $this->configure(); - $this->ldap->connect($this->host, $this->configuration->get('port')); + $this->ldap->connect( + $this->host, + $this->configuration->get('port'), + $this->configuration->get('protocol') + ); } /** diff --git a/src/HandlesConnection.php b/src/HandlesConnection.php index ca42d30a..e47904d8 100644 --- a/src/HandlesConnection.php +++ b/src/HandlesConnection.php @@ -15,6 +15,11 @@ trait HandlesConnection */ protected ?string $host = null; + /** + * The LDAP protocol to use (ldap:// or ldaps://). + */ + protected ?string $protocol = null; + /** * The LDAP connection resource. * @@ -141,7 +146,11 @@ public function getConnection(): ?Connection */ public function getProtocol(): string { - return $this->isUsingSSL() ? LdapInterface::PROTOCOL_SSL : LdapInterface::PROTOCOL; + return $this->protocol ?: ( + $this->isUsingSSL() + ? LdapInterface::PROTOCOL_SSL + : LdapInterface::PROTOCOL + ); } /** diff --git a/src/Ldap.php b/src/Ldap.php index c0fe3acf..77221435 100644 --- a/src/Ldap.php +++ b/src/Ldap.php @@ -158,10 +158,10 @@ public function startTLS(): bool /** * {@inheritdoc} */ - public function connect(string|array $hosts = [], int $port = 389): bool + public function connect(string|array $hosts = [], int $port = 389, ?string $protocol = null): bool { $this->bound = false; - + $this->protocol = $protocol; $this->host = $this->makeConnectionUris($hosts, $port); $this->connection = $this->executeFailableOperation(function () { @@ -182,9 +182,10 @@ public function close(): bool $result = @ldap_close($this->connection); } - $this->connection = null; $this->bound = false; $this->host = null; + $this->protocol = null; + $this->connection = null; return $result; } diff --git a/src/LdapInterface.php b/src/LdapInterface.php index 8f1afbc1..3a18f295 100644 --- a/src/LdapInterface.php +++ b/src/LdapInterface.php @@ -248,7 +248,7 @@ public function startTLS(): bool; * * @see http://php.net/manual/en/function.ldap-start-tls.php */ - public function connect(string|array $hosts = [], int $port = 389): bool; + public function connect(string|array $hosts = [], int $port = 389, ?string $protocol = null): bool; /** * Closes the current connection. diff --git a/src/Testing/LdapFake.php b/src/Testing/LdapFake.php index f05b734c..046221b5 100644 --- a/src/Testing/LdapFake.php +++ b/src/Testing/LdapFake.php @@ -336,10 +336,10 @@ public function startTLS(): bool /** * {@inheritdoc} */ - public function connect(string|array $hosts = [], int $port = 389): bool + public function connect(string|array $hosts = [], int $port = 389, ?string $protocol = null): bool { $this->bound = false; - + $this->protocol = $protocol; $this->host = $this->makeConnectionUris($hosts, $port); return $this->connection = $this->hasExpectations(__FUNCTION__) @@ -352,9 +352,10 @@ public function connect(string|array $hosts = [], int $port = 389): bool */ public function close(): bool { - $this->connection = null; $this->bound = false; $this->host = null; + $this->protocol = null; + $this->connection = null; return $this->hasExpectations(__FUNCTION__) ? $this->resolveExpectation(__FUNCTION__) diff --git a/tests/Unit/Configuration/DomainConfigurationTest.php b/tests/Unit/Configuration/DomainConfigurationTest.php index 56cd0b11..e7d55e5b 100644 --- a/tests/Unit/Configuration/DomainConfigurationTest.php +++ b/tests/Unit/Configuration/DomainConfigurationTest.php @@ -36,6 +36,7 @@ public function test_default_options() $config = new DomainConfiguration(); $this->assertEquals(389, $config->get('port')); + $this->assertNull($config->get('protocol')); $this->assertEmpty($config->get('hosts')); $this->assertEquals(0, $config->get('follow_referrals')); $this->assertEmpty($config->get('username')); @@ -50,6 +51,7 @@ public function test_all_options() { $config = new DomainConfiguration([ 'port' => 500, + 'protocol' => 'foo://', 'base_dn' => 'dc=corp,dc=org', 'hosts' => ['dc1', 'dc2'], 'follow_referrals' => false, @@ -67,6 +69,7 @@ public function test_all_options() ]); $this->assertEquals(500, $config->get('port')); + $this->assertEquals('foo://', $config->get('protocol')); $this->assertEquals('dc=corp,dc=org', $config->get('base_dn')); $this->assertEquals(['dc1', 'dc2'], $config->get('hosts')); $this->assertEquals('username', $config->get('username')); @@ -92,6 +95,7 @@ public function test_get_all() 'timeout' => 5, 'version' => 3, 'port' => 389, + 'protocol' => null, 'base_dn' => '', 'username' => '', 'password' => '', diff --git a/tests/Unit/FakeDirectoryTest.php b/tests/Unit/FakeDirectoryTest.php index 62505617..b9d67c3e 100644 --- a/tests/Unit/FakeDirectoryTest.php +++ b/tests/Unit/FakeDirectoryTest.php @@ -50,6 +50,7 @@ public function test_fake_connection_uses_real_connections_config() 'username' => 'user', 'password' => 'pass', 'port' => 389, + 'protocol' => null, 'use_tls' => true, 'use_ssl' => false, 'use_sasl' => false, diff --git a/tests/Unit/LdapTest.php b/tests/Unit/LdapTest.php index 84273a43..1a9c5079 100644 --- a/tests/Unit/LdapTest.php +++ b/tests/Unit/LdapTest.php @@ -3,6 +3,7 @@ namespace LdapRecord\Tests\Unit; use LdapRecord\Ldap; +use LdapRecord\LdapInterface; use LdapRecord\Testing\LdapFake; use LdapRecord\Tests\TestCase; use Mockery as m; @@ -17,6 +18,7 @@ public function test_construct_defaults() $this->assertFalse($ldap->isUsingSSL()); $this->assertFalse($ldap->isBound()); $this->assertNull($ldap->getConnection()); + $this->assertEquals($ldap->getProtocol(), LdapInterface::PROTOCOL); } public function test_host_arrays_are_properly_processed() @@ -28,13 +30,13 @@ public function test_host_arrays_are_properly_processed() $this->assertEquals('ldap://dc01:500 ldap://dc02:500', $ldap->getHost()); } - public function test_host_strings_are_properly_processed() + public function test_host_strings_are_properly_created() { $ldap = new LdapFake(); - $ldap->connect('dc01', $port = 500); + $ldap->connect('dc01', $port = 500, 'foo://'); - $this->assertEquals('ldap://dc01:500', $ldap->getHost()); + $this->assertEquals('foo://dc01:500', $ldap->getHost()); } public function test_get_default_protocol()