Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6804efe

Browse files
committed
Fixed issue with legacy client initialization
1 parent c7f1f78 commit 6804efe

File tree

2 files changed

+90
-8
lines changed

2 files changed

+90
-8
lines changed

src/Symfony/Component/Ldap/LdapClient.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Ldap;
1313

14+
@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);
15+
1416
/**
1517
* @author Grégoire Pineau <[email protected]>
1618
* @author Francis Besset <[email protected]>
@@ -24,14 +26,7 @@ final class LdapClient implements LdapClientInterface
2426

2527
public function __construct($host = null, $port = 389, $version = 3, $useSsl = false, $useStartTls = false, $optReferrals = false, LdapInterface $ldap = null)
2628
{
27-
$config = array(
28-
'host' => $host,
29-
'port' => $port,
30-
'version' => $version,
31-
'useSsl' => (bool) $useSsl,
32-
'useStartTls' => (bool) $useStartTls,
33-
'optReferrals' => (bool) $optReferrals,
34-
);
29+
$config = $this->normalizeConfig($host, $port, $version, $useSsl, $useStartTls, $optReferrals);
3530

3631
$this->ldap = null !== $ldap ? $ldap : Ldap::create('ext_ldap', $config);
3732
}
@@ -98,4 +93,25 @@ public function escape($subject, $ignore = '', $flags = 0)
9893
{
9994
return $this->ldap->escape($subject, $ignore, $flags);
10095
}
96+
97+
private function normalizeConfig($host, $port, $version, $useSsl, $useStartTls, $optReferrals)
98+
{
99+
if ((bool) $useSsl) {
100+
$encryption = 'ssl';
101+
} elseif ((bool) $useStartTls) {
102+
$encryption = 'tls';
103+
} else {
104+
$encryption = 'none';
105+
}
106+
107+
return array(
108+
'host' => $host,
109+
'port' => $port,
110+
'encryption' => $encryption,
111+
'options' => array(
112+
'protocol_version' => $version,
113+
'referrals' => (bool) $optReferrals,
114+
),
115+
);
116+
}
101117
}

src/Symfony/Component/Ldap/Tests/LdapClientTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,70 @@ public function testLdapFind()
177177
);
178178
$this->assertEquals($expected, $this->client->find('dc=foo,dc=com', 'bar', 'baz'));
179179
}
180+
181+
/**
182+
* @dataProvider provideConfig
183+
*/
184+
public function testLdapClientConfig($args, $expected)
185+
{
186+
$reflObj = new \ReflectionObject($this->client);
187+
$reflMethod = $reflObj->getMethod('normalizeConfig');
188+
$reflMethod->setAccessible(true);
189+
array_unshift($args, $this->client);
190+
$this->assertEquals($expected, call_user_func_array(array($reflMethod, 'invoke'), $args));
191+
}
192+
193+
public function provideConfig()
194+
{
195+
return array(
196+
array(
197+
array('localhost', 389, 3, true, false, false),
198+
array(
199+
'host' => 'localhost',
200+
'port' => 389,
201+
'encryption' => 'ssl',
202+
'options' => array(
203+
'protocol_version' => 3,
204+
'referrals' => false,
205+
),
206+
),
207+
),
208+
array(
209+
array('localhost', 389, 3, false, true, false),
210+
array(
211+
'host' => 'localhost',
212+
'port' => 389,
213+
'encryption' => 'tls',
214+
'options' => array(
215+
'protocol_version' => 3,
216+
'referrals' => false,
217+
),
218+
),
219+
),
220+
array(
221+
array('localhost', 389, 3, false, false, false),
222+
array(
223+
'host' => 'localhost',
224+
'port' => 389,
225+
'encryption' => 'none',
226+
'options' => array(
227+
'protocol_version' => 3,
228+
'referrals' => false,
229+
),
230+
),
231+
),
232+
array(
233+
array('localhost', 389, 3, false, false, false),
234+
array(
235+
'host' => 'localhost',
236+
'port' => 389,
237+
'encryption' => 'none',
238+
'options' => array(
239+
'protocol_version' => 3,
240+
'referrals' => false,
241+
),
242+
),
243+
),
244+
);
245+
}
180246
}

0 commit comments

Comments
 (0)