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

Skip to content

Commit 34d3c85

Browse files
committed
Added compatibility layer for previous version of the Security component
1 parent 81cb79b commit 34d3c85

File tree

7 files changed

+341
-27
lines changed

7 files changed

+341
-27
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Ldap;
13+
14+
use Symfony\Component\Ldap\Exception\ConnectionException;
15+
16+
/**
17+
* Base Ldap interface.
18+
*
19+
* This interface is here for reusability in the BC layer,
20+
* and will be merged in LdapInterface in Symfony 4.0.
21+
*
22+
* @author Charles Sarrazin <[email protected]>
23+
*
24+
* @internal
25+
*/
26+
interface BaseLdapInterface
27+
{
28+
/**
29+
* Return a connection bound to the ldap.
30+
*
31+
* @param string $dn A LDAP dn
32+
* @param string $password A password
33+
*
34+
* @throws ConnectionException If dn / password could not be bound.
35+
*/
36+
public function bind($dn = null, $password = null);
37+
38+
/**
39+
* Escape a string for use in an LDAP filter or DN.
40+
*
41+
* @param string $subject
42+
* @param string $ignore
43+
* @param int $flags
44+
*
45+
* @return string
46+
*/
47+
public function escape($subject, $ignore = '', $flags = 0);
48+
}

src/Symfony/Component/Ldap/Ldap.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use Symfony\Component\Ldap\Exception\DriverNotFoundException;
1616

1717
/**
18-
* @author Grégoire Pineau <[email protected]>
19-
* @author Francis Besset <[email protected]>
2018
* @author Charles Sarrazin <[email protected]>
2119
*/
2220
final class Ldap implements LdapInterface
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Ldap;
13+
14+
/**
15+
* @author Grégoire Pineau <[email protected]>
16+
* @author Francis Besset <[email protected]>
17+
* @author Charles Sarrazin <[email protected]>
18+
*
19+
* @deprecated The LdapClient class will be removed in Symfony 4.0. You should use the Ldap class instead.
20+
*/
21+
final class LdapClient implements LdapClientInterface
22+
{
23+
private $ldap;
24+
25+
public function __construct($host = null, $port = 389, $version = 3, $useSsl = false, $useStartTls = false, $optReferrals = false, LdapInterface $ldap = null)
26+
{
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+
);
35+
36+
$this->ldap = null !== $ldap ? $ldap : Ldap::create('ext_ldap', $config);
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function bind($dn = null, $password = null)
43+
{
44+
$this->ldap->bind($dn, $password);
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function find($dn, $query, $filter = '*')
51+
{
52+
@trigger_error('The "find" method is deprecated since version 3.1 and will be removed in 4.0. Use the "query" method instead.', E_USER_DEPRECATED);
53+
54+
$query = $this->ldap->query($dn, $query, array('filter' => $filter));
55+
$entries = $query->execute();
56+
$result = array();
57+
58+
foreach ($entries as $entry) {
59+
$resultEntry = array();
60+
61+
foreach ($entry->getAttributes() as $attribute => $values) {
62+
$resultAttribute = $values;
63+
64+
$resultAttribute['count'] = count($values);
65+
$resultEntry[] = $resultAttribute;
66+
$resultEntry[$attribute] = $resultAttribute;
67+
}
68+
69+
$resultEntry['count'] = count($resultEntry) / 2;
70+
$result[] = $resultEntry;
71+
}
72+
73+
$result['count'] = count($result);
74+
75+
return $result;
76+
}
77+
78+
/**
79+
* {@inheritdoc}
80+
*/
81+
public function escape($subject, $ignore = '', $flags = 0)
82+
{
83+
return $this->ldap->escape($subject, $ignore, $flags);
84+
}
85+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Ldap;
13+
14+
/**
15+
* Ldap interface.
16+
*
17+
* This interface is used for the BC layer with branch 2.8 and 3.0.
18+
*
19+
* @author Grégoire Pineau <[email protected]>
20+
* @author Charles Sarrazin <[email protected]>
21+
*
22+
* @deprecated You should use LdapInterface instead
23+
*/
24+
interface LdapClientInterface extends BaseLdapInterface
25+
{
26+
/*
27+
* Find a username into ldap connection.
28+
*
29+
* @param string $dn
30+
* @param string $query
31+
* @param mixed $filter
32+
*
33+
* @return array|null
34+
*/
35+
public function find($dn, $query, $filter = '*');
36+
}

src/Symfony/Component/Ldap/LdapInterface.php

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,17 @@
1212
namespace Symfony\Component\Ldap;
1313

1414
use Symfony\Component\Ldap\Adapter\QueryInterface;
15-
use Symfony\Component\Ldap\Exception\ConnectionException;
1615

1716
/**
1817
* Ldap interface.
1918
*
20-
* @author Grégoire Pineau <[email protected]>
2119
* @author Charles Sarrazin <[email protected]>
2220
*/
23-
interface LdapInterface
21+
interface LdapInterface extends BaseLdapInterface
2422
{
2523
const ESCAPE_FILTER = 0x01;
2624
const ESCAPE_DN = 0x02;
2725

28-
/**
29-
* Return a connection bound to the ldap.
30-
*
31-
* @param string $dn A LDAP dn
32-
* @param string $password A password
33-
*
34-
* @throws ConnectionException If dn / password could not be bound.
35-
*/
36-
public function bind($dn = null, $password = null);
37-
3826
/**
3927
* Queries a ldap server for entries matching the given criteria.
4028
*
@@ -45,15 +33,4 @@ public function bind($dn = null, $password = null);
4533
* @return QueryInterface
4634
*/
4735
public function query($dn, $query, array $options = array());
48-
49-
/**
50-
* Escape a string for use in an LDAP filter or DN.
51-
*
52-
* @param string $subject
53-
* @param string $ignore
54-
* @param int $flags
55-
*
56-
* @return string
57-
*/
58-
public function escape($subject, $ignore = '', $flags = 0);
5936
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Ldap\Tests;
13+
14+
use Symfony\Component\Ldap\Adapter\CollectionInterface;
15+
use Symfony\Component\Ldap\Adapter\QueryInterface;
16+
use Symfony\Component\Ldap\Entry;
17+
use Symfony\Component\Ldap\LdapClient;
18+
use Symfony\Component\Ldap\LdapInterface;
19+
20+
/**
21+
* @group legacy
22+
*/
23+
class LdapClientTest extends \PHPUnit_Framework_TestCase
24+
{
25+
/** @var LdapClient */
26+
private $client;
27+
/** @var \PHPUnit_Framework_MockObject_MockObject */
28+
private $ldap;
29+
30+
protected function setUp()
31+
{
32+
$this->ldap = $this->getMock(LdapInterface::class);
33+
34+
$this->client = new LdapClient(null, 389, 3, false, false, false, $this->ldap);
35+
}
36+
37+
public function testLdapBind()
38+
{
39+
$this->ldap
40+
->expects($this->once())
41+
->method('bind')
42+
->with('foo', 'bar')
43+
;
44+
$this->client->bind('foo', 'bar');
45+
}
46+
47+
public function testLdapEscape()
48+
{
49+
$this->ldap
50+
->expects($this->once())
51+
->method('escape')
52+
->with('foo', 'bar', 'baz')
53+
;
54+
$this->client->escape('foo', 'bar', 'baz');
55+
}
56+
57+
public function testLdapFind()
58+
{
59+
$collection = $this->getMock(CollectionInterface::class);
60+
$collection
61+
->expects($this->once())
62+
->method('getIterator')
63+
->will($this->returnValue(new \ArrayIterator(array(
64+
new Entry('cn=qux,dc=foo,dc=com', array(
65+
'dn' => array('cn=qux,dc=foo,dc=com'),
66+
'cn' => array('qux'),
67+
'dc' => array('com', 'foo'),
68+
'givenName' => array('Qux'),
69+
)),
70+
new Entry('cn=baz,dc=foo,dc=com', array(
71+
'dn' => array('cn=baz,dc=foo,dc=com'),
72+
'cn' => array('baz'),
73+
'dc' => array('com', 'foo'),
74+
'givenName' => array('Baz'),
75+
)),
76+
))))
77+
;
78+
$query = $this->getMock(QueryInterface::class);
79+
$query
80+
->expects($this->once())
81+
->method('execute')
82+
->will($this->returnValue($collection))
83+
;
84+
$this->ldap
85+
->expects($this->once())
86+
->method('query')
87+
->with('dc=foo,dc=com', 'bar', array('filter' => 'baz'))
88+
->willReturn($query)
89+
;
90+
91+
$expected = array(
92+
'count' => 2,
93+
0 => array(
94+
'count' => 4,
95+
0 => array(
96+
'count' => 1,
97+
0 => 'cn=qux,dc=foo,dc=com',
98+
),
99+
'dn' => array(
100+
'count' => 1,
101+
0 => 'cn=qux,dc=foo,dc=com',
102+
),
103+
1 => array(
104+
'count' => 1,
105+
0 => 'qux',
106+
),
107+
'cn' => array(
108+
'count' => 1,
109+
0 => 'qux',
110+
),
111+
2 => array(
112+
'count' => 2,
113+
0 => 'com',
114+
1 => 'foo',
115+
),
116+
'dc' => array(
117+
'count' => 2,
118+
0 => 'com',
119+
1 => 'foo',
120+
),
121+
3 => array(
122+
'count' => 1,
123+
0 => 'Qux',
124+
),
125+
'givenName' => array(
126+
'count' => 1,
127+
0 => 'Qux',
128+
),
129+
),
130+
1 => array(
131+
'count' => 4,
132+
0 => array(
133+
'count' => 1,
134+
0 => 'cn=baz,dc=foo,dc=com',
135+
),
136+
'dn' => array(
137+
'count' => 1,
138+
0 => 'cn=baz,dc=foo,dc=com',
139+
),
140+
1 => array(
141+
'count' => 1,
142+
0 => 'baz',
143+
),
144+
'cn' => array(
145+
'count' => 1,
146+
0 => 'baz',
147+
),
148+
2 => array(
149+
'count' => 2,
150+
0 => 'com',
151+
1 => 'foo',
152+
),
153+
'dc' => array(
154+
'count' => 2,
155+
0 => 'com',
156+
1 => 'foo',
157+
),
158+
3 => array(
159+
'count' => 1,
160+
0 => 'Baz',
161+
),
162+
'givenName' => array(
163+
'count' => 1,
164+
0 => 'Baz',
165+
),
166+
),
167+
);
168+
$this->assertEquals($expected, $this->client->find('dc=foo,dc=com', 'bar', 'baz'));
169+
}
170+
}

0 commit comments

Comments
 (0)