Skip to content

Commit

Permalink
Added compatibility layer for previous version of the Security component
Browse files Browse the repository at this point in the history
  • Loading branch information
csarrazi committed Feb 11, 2016
1 parent 81cb79b commit 34d3c85
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 27 deletions.
48 changes: 48 additions & 0 deletions src/Symfony/Component/Ldap/BaseLdapInterface.php
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);
}
2 changes: 0 additions & 2 deletions src/Symfony/Component/Ldap/Ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
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>
*/
final class Ldap implements LdapInterface
Expand Down
85 changes: 85 additions & 0 deletions src/Symfony/Component/Ldap/LdapClient.php
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);
}
}
36 changes: 36 additions & 0 deletions src/Symfony/Component/Ldap/LdapClientInterface.php
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 = '*');
}
25 changes: 1 addition & 24 deletions src/Symfony/Component/Ldap/LdapInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,17 @@
namespace Symfony\Component\Ldap;

use Symfony\Component\Ldap\Adapter\QueryInterface;
use Symfony\Component\Ldap\Exception\ConnectionException;

/**
* Ldap interface.
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
* @author Charles Sarrazin <charles@sarraz.in>
*/
interface LdapInterface
interface LdapInterface extends BaseLdapInterface
{
const ESCAPE_FILTER = 0x01;
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.
*
Expand All @@ -45,15 +33,4 @@ public function bind($dn = null, $password = null);
* @return QueryInterface
*/
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 changes: 170 additions & 0 deletions src/Symfony/Component/Ldap/Tests/LdapClientTest.php
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'));
}
}

0 comments on commit 34d3c85

Please sign in to comment.