-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added compatibility layer for previous version of the Security component
- Loading branch information
Showing
7 changed files
with
341 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Ldap; | ||
|
||
use Symfony\Component\Ldap\Exception\ConnectionException; | ||
|
||
/** | ||
* Base Ldap interface. | ||
* | ||
* This interface is here for reusability in the BC layer, | ||
* and will be merged in LdapInterface in Symfony 4.0. | ||
* | ||
* @author Charles Sarrazin <charles@sarraz.in> | ||
* | ||
* @internal | ||
*/ | ||
interface BaseLdapInterface | ||
{ | ||
/** | ||
* Return a connection bound to the ldap. | ||
* | ||
* @param string $dn A LDAP dn | ||
* @param string $password A password | ||
* | ||
* @throws ConnectionException If dn / password could not be bound. | ||
*/ | ||
public function bind($dn = null, $password = null); | ||
|
||
/** | ||
* Escape a string for use in an LDAP filter or DN. | ||
* | ||
* @param string $subject | ||
* @param string $ignore | ||
* @param int $flags | ||
* | ||
* @return string | ||
*/ | ||
public function escape($subject, $ignore = '', $flags = 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Ldap; | ||
|
||
/** | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
* @author Francis Besset <francis.besset@gmail.com> | ||
* @author Charles Sarrazin <charles@sarraz.in> | ||
* | ||
* @deprecated The LdapClient class will be removed in Symfony 4.0. You should use the Ldap class instead. | ||
*/ | ||
final class LdapClient implements LdapClientInterface | ||
{ | ||
private $ldap; | ||
|
||
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, | ||
); | ||
|
||
$this->ldap = null !== $ldap ? $ldap : Ldap::create('ext_ldap', $config); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function bind($dn = null, $password = null) | ||
{ | ||
$this->ldap->bind($dn, $password); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function find($dn, $query, $filter = '*') | ||
{ | ||
@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); | ||
|
||
$query = $this->ldap->query($dn, $query, array('filter' => $filter)); | ||
$entries = $query->execute(); | ||
$result = array(); | ||
|
||
foreach ($entries as $entry) { | ||
$resultEntry = array(); | ||
|
||
foreach ($entry->getAttributes() as $attribute => $values) { | ||
$resultAttribute = $values; | ||
|
||
$resultAttribute['count'] = count($values); | ||
$resultEntry[] = $resultAttribute; | ||
$resultEntry[$attribute] = $resultAttribute; | ||
} | ||
|
||
$resultEntry['count'] = count($resultEntry) / 2; | ||
$result[] = $resultEntry; | ||
} | ||
|
||
$result['count'] = count($result); | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function escape($subject, $ignore = '', $flags = 0) | ||
{ | ||
return $this->ldap->escape($subject, $ignore, $flags); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Ldap; | ||
|
||
/** | ||
* Ldap interface. | ||
* | ||
* This interface is used for the BC layer with branch 2.8 and 3.0. | ||
* | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
* @author Charles Sarrazin <charles@sarraz.in> | ||
* | ||
* @deprecated You should use LdapInterface instead | ||
*/ | ||
interface LdapClientInterface extends BaseLdapInterface | ||
{ | ||
/* | ||
* Find a username into ldap connection. | ||
* | ||
* @param string $dn | ||
* @param string $query | ||
* @param mixed $filter | ||
* | ||
* @return array|null | ||
*/ | ||
public function find($dn, $query, $filter = '*'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Ldap\Tests; | ||
|
||
use Symfony\Component\Ldap\Adapter\CollectionInterface; | ||
use Symfony\Component\Ldap\Adapter\QueryInterface; | ||
use Symfony\Component\Ldap\Entry; | ||
use Symfony\Component\Ldap\LdapClient; | ||
use Symfony\Component\Ldap\LdapInterface; | ||
|
||
/** | ||
* @group legacy | ||
*/ | ||
class LdapClientTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** @var LdapClient */ | ||
private $client; | ||
/** @var \PHPUnit_Framework_MockObject_MockObject */ | ||
private $ldap; | ||
|
||
protected function setUp() | ||
{ | ||
$this->ldap = $this->getMock(LdapInterface::class); | ||
|
||
$this->client = new LdapClient(null, 389, 3, false, false, false, $this->ldap); | ||
} | ||
|
||
public function testLdapBind() | ||
{ | ||
$this->ldap | ||
->expects($this->once()) | ||
->method('bind') | ||
->with('foo', 'bar') | ||
; | ||
$this->client->bind('foo', 'bar'); | ||
} | ||
|
||
public function testLdapEscape() | ||
{ | ||
$this->ldap | ||
->expects($this->once()) | ||
->method('escape') | ||
->with('foo', 'bar', 'baz') | ||
; | ||
$this->client->escape('foo', 'bar', 'baz'); | ||
} | ||
|
||
public function testLdapFind() | ||
{ | ||
$collection = $this->getMock(CollectionInterface::class); | ||
$collection | ||
->expects($this->once()) | ||
->method('getIterator') | ||
->will($this->returnValue(new \ArrayIterator(array( | ||
new Entry('cn=qux,dc=foo,dc=com', array( | ||
'dn' => array('cn=qux,dc=foo,dc=com'), | ||
'cn' => array('qux'), | ||
'dc' => array('com', 'foo'), | ||
'givenName' => array('Qux'), | ||
)), | ||
new Entry('cn=baz,dc=foo,dc=com', array( | ||
'dn' => array('cn=baz,dc=foo,dc=com'), | ||
'cn' => array('baz'), | ||
'dc' => array('com', 'foo'), | ||
'givenName' => array('Baz'), | ||
)), | ||
)))) | ||
; | ||
$query = $this->getMock(QueryInterface::class); | ||
$query | ||
->expects($this->once()) | ||
->method('execute') | ||
->will($this->returnValue($collection)) | ||
; | ||
$this->ldap | ||
->expects($this->once()) | ||
->method('query') | ||
->with('dc=foo,dc=com', 'bar', array('filter' => 'baz')) | ||
->willReturn($query) | ||
; | ||
|
||
$expected = array( | ||
'count' => 2, | ||
0 => array( | ||
'count' => 4, | ||
0 => array( | ||
'count' => 1, | ||
0 => 'cn=qux,dc=foo,dc=com', | ||
), | ||
'dn' => array( | ||
'count' => 1, | ||
0 => 'cn=qux,dc=foo,dc=com', | ||
), | ||
1 => array( | ||
'count' => 1, | ||
0 => 'qux', | ||
), | ||
'cn' => array( | ||
'count' => 1, | ||
0 => 'qux', | ||
), | ||
2 => array( | ||
'count' => 2, | ||
0 => 'com', | ||
1 => 'foo', | ||
), | ||
'dc' => array( | ||
'count' => 2, | ||
0 => 'com', | ||
1 => 'foo', | ||
), | ||
3 => array( | ||
'count' => 1, | ||
0 => 'Qux', | ||
), | ||
'givenName' => array( | ||
'count' => 1, | ||
0 => 'Qux', | ||
), | ||
), | ||
1 => array( | ||
'count' => 4, | ||
0 => array( | ||
'count' => 1, | ||
0 => 'cn=baz,dc=foo,dc=com', | ||
), | ||
'dn' => array( | ||
'count' => 1, | ||
0 => 'cn=baz,dc=foo,dc=com', | ||
), | ||
1 => array( | ||
'count' => 1, | ||
0 => 'baz', | ||
), | ||
'cn' => array( | ||
'count' => 1, | ||
0 => 'baz', | ||
), | ||
2 => array( | ||
'count' => 2, | ||
0 => 'com', | ||
1 => 'foo', | ||
), | ||
'dc' => array( | ||
'count' => 2, | ||
0 => 'com', | ||
1 => 'foo', | ||
), | ||
3 => array( | ||
'count' => 1, | ||
0 => 'Baz', | ||
), | ||
'givenName' => array( | ||
'count' => 1, | ||
0 => 'Baz', | ||
), | ||
), | ||
); | ||
$this->assertEquals($expected, $this->client->find('dc=foo,dc=com', 'bar', 'baz')); | ||
} | ||
} |
Oops, something went wrong.