Added compatibility layer for previous version of the Security component
This commit is contained in:
parent
81cb79b10a
commit
34d3c853cb
48
src/Symfony/Component/Ldap/BaseLdapInterface.php
Normal file
48
src/Symfony/Component/Ldap/BaseLdapInterface.php
Normal file
@ -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);
|
||||||
|
}
|
@ -15,8 +15,6 @@ use Symfony\Component\Ldap\Adapter\AdapterInterface;
|
|||||||
use Symfony\Component\Ldap\Exception\DriverNotFoundException;
|
use Symfony\Component\Ldap\Exception\DriverNotFoundException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Grégoire Pineau <lyrixx@lyrixx.info>
|
|
||||||
* @author Francis Besset <francis.besset@gmail.com>
|
|
||||||
* @author Charles Sarrazin <charles@sarraz.in>
|
* @author Charles Sarrazin <charles@sarraz.in>
|
||||||
*/
|
*/
|
||||||
final class Ldap implements LdapInterface
|
final class Ldap implements LdapInterface
|
||||||
|
85
src/Symfony/Component/Ldap/LdapClient.php
Normal file
85
src/Symfony/Component/Ldap/LdapClient.php
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
36
src/Symfony/Component/Ldap/LdapClientInterface.php
Normal file
36
src/Symfony/Component/Ldap/LdapClientInterface.php
Normal file
@ -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 = '*');
|
||||||
|
}
|
@ -12,29 +12,17 @@
|
|||||||
namespace Symfony\Component\Ldap;
|
namespace Symfony\Component\Ldap;
|
||||||
|
|
||||||
use Symfony\Component\Ldap\Adapter\QueryInterface;
|
use Symfony\Component\Ldap\Adapter\QueryInterface;
|
||||||
use Symfony\Component\Ldap\Exception\ConnectionException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ldap interface.
|
* Ldap interface.
|
||||||
*
|
*
|
||||||
* @author Grégoire Pineau <lyrixx@lyrixx.info>
|
|
||||||
* @author Charles Sarrazin <charles@sarraz.in>
|
* @author Charles Sarrazin <charles@sarraz.in>
|
||||||
*/
|
*/
|
||||||
interface LdapInterface
|
interface LdapInterface extends BaseLdapInterface
|
||||||
{
|
{
|
||||||
const ESCAPE_FILTER = 0x01;
|
const ESCAPE_FILTER = 0x01;
|
||||||
const ESCAPE_DN = 0x02;
|
const ESCAPE_DN = 0x02;
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queries a ldap server for entries matching the given criteria.
|
* Queries a ldap server for entries matching the given criteria.
|
||||||
*
|
*
|
||||||
@ -45,15 +33,4 @@ interface LdapInterface
|
|||||||
* @return QueryInterface
|
* @return QueryInterface
|
||||||
*/
|
*/
|
||||||
public function query($dn, $query, array $options = array());
|
public function query($dn, $query, array $options = array());
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
}
|
}
|
||||||
|
170
src/Symfony/Component/Ldap/Tests/LdapClientTest.php
Normal file
170
src/Symfony/Component/Ldap/Tests/LdapClientTest.php
Normal file
@ -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'));
|
||||||
|
}
|
||||||
|
}
|
@ -18,7 +18,7 @@ use Symfony\Component\Ldap\Ldap;
|
|||||||
|
|
||||||
class LdapTest extends \PHPUnit_Framework_TestCase
|
class LdapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
/** @var AdapterInterface */
|
/** @var \PHPUnit_Framework_MockObject_MockObject */
|
||||||
private $adapter;
|
private $adapter;
|
||||||
|
|
||||||
/** @var Ldap */
|
/** @var Ldap */
|
||||||
|
Reference in New Issue
Block a user