8000 [Ldap] Fixed issue with legacy client initialisation by csarrazi · Pull Request #18944 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Ldap] Fixed issue with legacy client initialisation #18944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/Symfony/Component/Ldap/LdapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Ldap;

@trigger_error('The '.__NAMESPACE__.'\LdapClient class is deprecated since version 3.1 and will be removed in 4.0. Use the Ldap class directly instead.', E_USER_DEPRECATED);

/**
* @author Grégoire Pineau <lyrixx@lyrixx.info>
* @author Francis Besset <francis.besset@gmail.com>
Expand All @@ -24,14 +26,7 @@ final class LdapClient implements LdapClientInterface

public function __construct($host = null, $port = 389, $version = 3, $useSsl = false, $useStartTls = false, $optReferrals = false, LdapInterface $ldap = null)
{
$config = array(
'host' => $host,
'port' => $port,
'version' => $version,
'useSsl' => (bool) $useSsl,
'useStartTls' => (bool) $useStartTls,
'optReferrals' => (bool) $optReferrals,
);
$config = $this->normalizeConfig($host, $port, $version, $useSsl, $useStartTls, $optReferrals);

$this->ldap = null !== $ldap ? $ldap : Ldap::create('ext_ldap', $config);
}
Expand Down Expand Up @@ -98,4 +93,25 @@ public function escape($subject, $ignore = '', $flags = 0)
{
return $this->ldap->escape($subject, $ignore, $flags);
}

private function normalizeConfig($host, $port, $version, $useSsl, $useStartTls, $optReferrals)
{
if ((bool) $useSsl) {
$encryption = 'ssl';
} elseif ((bool) $useStartTls) {
$encryption = 'tls';
} else {
$encryption = 'none';
}

return array(
'host' => $host,
'port' => $port,
'encryption' => $encryption,
'options' => array(
'protocol_version' => $version,
'referrals' => (bool) $optReferrals,
),
);
}
}
66 changes: 66 additions & 0 deletions src/Symfony/Component/Ldap/Tests/LdapClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,70 @@ public function testLdapFind()
);
$this->assertEquals($expected, $this->client->find('dc=foo,dc=com', 'bar', 'baz'));
}

/**
* @dataProvider provideConfig
*/
public function testLdapClientConfig($args, $expected)
{
$reflObj = new \ReflectionObject($this->client);
$reflMethod = $reflObj->getMethod('normalizeConfig');
$reflMethod->setAccessible(true);
array_unshift($args, $this->client);
$this->assertEquals($expected, call_user_func_array(array($reflMethod, 'invoke'), $args));
}

public function provideConfig()
{
return array(
array(
array('localhost', 389, 3, true, false, false),
array(
'host' => 'localhost',
'port' => 389,
'encryption' => 'ssl',
'options' => array(
'protocol_version' => 3,
'referrals' => false,
),
),
),
array(
array('localhost', 389, 3, false, true, false),
array(
'host' => 'localhost',
'port' => 389,
'encryption' => 'tls',
'options' => array(
'protocol_version' => 3,
'referrals' => false,
),
),
),
array(
array('localhost', 389, 3, false, false, false),
array(
'host' => 'localhost',
'port' => 389,
'encryption' => 'none',
'options' => array(
'protocol_version' => 3,
'referrals' => false,
),
),
),
array(
array('localhost', 389, 3, false, false, false),
array(
'host' => 'localhost',
'port' => 389,
'encryption' => 'none',
'options' => array(
'protocol_version' => 3,
'referrals' => false,
),
),
),
);
}
}
0