From 6804efeeee569906bc17628acc31e06aa26331cb Mon Sep 17 00:00:00 2001 From: Charles Sarrazin Date: Wed, 1 Jun 2016 21:47:08 +0200 Subject: [PATCH] Fixed issue with legacy client initialization --- src/Symfony/Component/Ldap/LdapClient.php | 32 ++++++--- .../Component/Ldap/Tests/LdapClientTest.php | 66 +++++++++++++++++++ 2 files changed, 90 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Ldap/LdapClient.php b/src/Symfony/Component/Ldap/LdapClient.php index bb9c1cbd4c..2bd32f6ac1 100644 --- a/src/Symfony/Component/Ldap/LdapClient.php +++ b/src/Symfony/Component/Ldap/LdapClient.php @@ -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 * @author Francis Besset @@ -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); } @@ -98,4 +93,25 @@ final class LdapClient implements LdapClientInterface { 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, + ), + ); + } } diff --git a/src/Symfony/Component/Ldap/Tests/LdapClientTest.php b/src/Symfony/Component/Ldap/Tests/LdapClientTest.php index 985c09e9c3..60cdf27a12 100644 --- a/src/Symfony/Component/Ldap/Tests/LdapClientTest.php +++ b/src/Symfony/Component/Ldap/Tests/LdapClientTest.php @@ -177,4 +177,70 @@ class LdapClientTest extends \PHPUnit_Framework_TestCase ); $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, + ), + ), + ), + ); + } }