Skip to content

[Ldap] Fixed issue with legacy find() method not working as expected #19980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 41 additions & 29 deletions src/Symfony/Component/Ldap/Adapter/ExtLdap/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,90 +24,102 @@ class Collection implements CollectionInterface
private $search;
private $entries;

public function __construct(Connection $connection, Query $search, array $entries = array())
public function __construct(Connection $connection, Query $search)
{
$this->connection = $connection;
$this->search = $search;
$this->entries = array();
}

/**
* {@inheritdoc}
*/
public function toArray()
{
$this->initialize();
if (null === $this->entries) {
$this->entries = iterator_to_array($this->getIterator(), false);
}

return $this->entries;
}

public function count()
{
$this->initialize();
if (false !== $count = ldap_count_entries($this->connection->getResource(), $this->search->getResource())) {
return $count;
}

return count($this->entries);
throw new LdapException(sprintf('Error while retrieving entry count: %s', ldap_error($this->connection->getResource())));
}

public function getIterator()
{
return new ResultIterator($this->connection, $this->search);
$con = $this->connection->getResource();
$search = $this->search->getResource();
$current = ldap_first_entry($con, $search);

if (0 === $this->count()) {
return;
}

if (false === $current) {
throw new LdapException(sprintf('Could not rewind entries array: %s', ldap_error($con)));
}

yield $this->getSingleEntry($con, $current);

while (false !== $current = ldap_next_entry($con, $current)) {
yield $this->getSingleEntry($con, $current);
}
}

public function offsetExists($offset)
{
$this->initialize();
$this->toArray();

return isset($this->entries[$offset]);
}

public function offsetGet($offset)
{
$this->toArray();

return isset($this->entries[$offset]) ? $this->entries[$offset] : null;
}

public function offsetSet($offset, $value)
{
$this->initialize();
$this->toArray();

$this->entries[$offset] = $value;
}

public function offsetUnset($offset)
{
$this->initialize();
$this->toArray();

unset($this->entries[$offset]);
}

private function initialize()
private function getSingleEntry($con, $current)
{
if (null === $this->entries) {
return;
}
$attributes = ldap_get_attributes($con, $current);

$con = $this->connection->getResource();

$entries = ldap_get_entries($con, $this->search->getResource());

if (false === $entries) {
throw new LdapException(sprintf('Could not load entries: %s', ldap_error($con)));
if (false === $attributes) {
throw new LdapException(sprintf('Could not fetch attributes: %s', ldap_error($con)));
}

if (0 === $entries['count']) {
return array();
}
$attributes = $this->cleanupAttributes($attributes);

unset($entries['count']);
$dn = ldap_get_dn($con, $current);

$this->entries = array_map(function (array $entry) {
$dn = $entry['dn'];
$attributes = $this->cleanupAttributes($entry);
if (false === $dn) {
throw new LdapException(sprintf('Could not fetch DN: %s', ldap_error($con)));
}

return new Entry($dn, $attributes);
}, $entries);
return new Entry($dn, $attributes);
}

private function cleanupAttributes(array $entry = array())
private function cleanupAttributes(array $entry)
{
$attributes = array_diff_key($entry, array_flip(range(0, $entry['count'] - 1)) + array(
'count' => null,
Expand Down
81 changes: 0 additions & 81 deletions src/Symfony/Component/Ldap/Adapter/ExtLdap/ResultIterator.php

This file was deleted.

20 changes: 15 additions & 5 deletions src/Symfony/Component/Ldap/LdapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,34 @@ public function find($dn, $query, $filter = '*')

$query = $this->ldap->query($dn, $query, array('filter' => $filter));
$entries = $query->execute();
$result = array();
$result = array(
'count' => 0,
);

foreach ($entries as $entry) {
$resultEntry = array();

foreach ($entry->getAttributes() as $attribute => $values) {
$resultAttribute = $values;
$resultAttribute = array(
'count' => count($values),
);

foreach ($values as $val) {
$resultAttribute[] = $val;
}
$attributeName = strtolower($attribute);

$resultAttribute['count'] = count($values);
$resultEntry[] = $resultAttribute;
$resultEntry[$attribute] = $resultAttribute;
$resultEntry[$attributeName] = $resultAttribute;
$resultEntry[] = $attributeName;
}

$resultEntry['count'] = count($resultEntry) / 2;
$resultEntry['dn'] = $entry->getDn();
$result[] = $resultEntry;
}

$result['count'] = count($result);
$result['count'] = count($result) - 1;

return $result;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Ldap/Tests/Adapter/ExtLdap/AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,22 @@ public function testLdapQuery()
$this->assertEquals(array('Fabien Potencier'), $entry->getAttribute('cn'));
$this->assertEquals(array('fabpot@symfony.com', 'fabien@potencier.com'), $entry->getAttribute('mail'));
}

/**
* @group functional
*/
public function testLdapQueryIterator()
{
$ldap = new Adapter($this->getLdapConfig());

$ldap->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony');
$query = $ldap->createQuery('dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))', array());
$result = $query->execute();
$iterator = $result->getIterator();
$iterator->rewind();
$entry = $iterator->current();
$this->assertInstanceOf(Entry::class, $entry);
$this->assertEquals(array('Fabien Potencier'), $entry->getAttribute('cn'));
$this->assertEquals(array('fabpot@symfony.com', 'fabien@potencier.com'), $entry->getAttribute('mail'));
}
}
Loading