Skip to content

Commit

Permalink
Merge pull request #733 from DirectoryTree/custom-protocols
Browse files Browse the repository at this point in the history
Add ability to set custom LDAP protocol
  • Loading branch information
stevebauman authored Jul 26, 2024
2 parents 2ccba8b + 8333376 commit 4113aa0
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/Configuration/DomainConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '',

Expand Down
6 changes: 5 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/HandlesConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/LdapInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions src/Testing/LdapFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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__)
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/Configuration/DomainConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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,
Expand All @@ -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'));
Expand All @@ -92,6 +95,7 @@ public function test_get_all()
'timeout' => 5,
'version' => 3,
'port' => 389,
'protocol' => null,
'base_dn' => '',
'username' => '',
'password' => '',
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/FakeDirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions tests/Unit/LdapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 4113aa0

Please sign in to comment.