Skip to content

[Ldap] Allow search scoping #20310

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[Ldap] Allow search scoping
  • Loading branch information
xunto committed Nov 2, 2016
commit 9f83a2b91b901b1a89264baeb7566f9a798ba49c
3 changes: 3 additions & 0 deletions src/Symfony/Component/Ldap/Adapter/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ public function __construct(ConnectionInterface $connection, $dn, $query, array
'timeout' => 0,
'deref' => static::DEREF_NEVER,
'attrsOnly' => 0,
'scope' => static::SCOPE_SUBTREE,
));
$resolver->setAllowedValues('deref', array(static::DEREF_ALWAYS, static::DEREF_NEVER, static::DEREF_FINDING, static::DEREF_SEARCHING));
$resolver->setAllowedValues('scope', array(static::SCOPE_BASE, static::SCOPE_ONELEVEL, static::SCOPE_SUBTREE));

$resolver->setNormalizer('filter', function (Options $options, $value) {
return is_array($value) ? $value : array($value);
});
Expand Down
16 changes: 15 additions & 1 deletion src/Symfony/Component/Ldap/Adapter/ExtLdap/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,21 @@ public function execute()

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

$this->search = @ldap_search(
switch ($this->options['scope']) {
case static::SCOPE_BASE:
$func = 'ldap_read';
break;
case static::SCOPE_ONELEVEL:
$func = 'ldap_list';
break;
case static::SCOPE_SUBTREE:
$func = 'ldap_search';
break;
default:
throw new LdapException(sprintf('Could not search in scope %s', $this->options['scopen']));
}

$this->search = @$func(
$con,
$this->dn,
$this->query,
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Ldap/Adapter/QueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ interface QueryInterface
const DEREF_FINDING = 0x02;
const DEREF_ALWAYS = 0x03;

const SCOPE_BASE = 'base';
const SCOPE_ONELEVEL = 'one_level';
const SCOPE_SUBTREE = 'subtree';

/**
* Executes a query and returns the list of Ldap entries.
*
Expand Down
34 changes: 34 additions & 0 deletions src/Symfony/Component/Ldap/Tests/Adapter/ExtLdap/AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Ldap\Adapter\ExtLdap\Adapter;
use Symfony\Component\Ldap\Adapter\ExtLdap\Collection;
use Symfony\Component\Ldap\Adapter\ExtLdap\Query;
use Symfony\Component\Ldap\Entry;
use Symfony\Component\Ldap\LdapInterface;

Expand Down Expand Up @@ -65,4 +66,37 @@ public function testLdapQueryIterator()
$this->assertEquals(array('Fabien Potencier'), $entry->getAttribute('cn'));
$this->assertEquals(array('fabpot@symfony.com', 'fabien@potencier.com'), $entry->getAttribute('mail'));
}

public function testLdapQueryScopeBase()
{
$ldap = new Adapter($this->getLdapConfig());

$ldap->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony');

$query = $ldap->createQuery('cn=Fabien Potencier,dc=symfony,dc=com', '(objectclass=*)', array(
'scope' => Query::SCOPE_BASE,
));
$result = $query->execute();

$entry = $result[0];
$this->assertEquals($result->count(), 1);
$this->assertEquals(array('Fabien Potencier'), $entry->getAttribute('cn'));
}

public function testLdapQueryScopeOneLevel()
{
$ldap = new Adapter($this->getLdapConfig());

$ldap->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony');

$one_level_result = $ldap->createQuery('ou=Components,dc=symfony,dc=com', '(objectclass=*)', array(
'scope' => Query::SCOPE_ONELEVEL,
))->execute();

$subtree_count = $ldap->createQuery('ou=Components,dc=symfony,dc=com', '(objectclass=*)')->execute()->count();

$this->assertNotEquals($one_level_result->count(), $subtree_count);
$this->assertEquals($one_level_result->count(), 1);
$this->assertEquals($one_level_result[0]->getAttribute('ou'), array('Ldap'));
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/Ldap/Tests/Fixtures/data/fixtures.ldif
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ ou: Maintainers
ou: Founder
givenName: Fabien Potencier
description: Founder and project lead @Symfony

dn: ou=Components,dc=symfony,dc=com
objectclass: organizationalunit
ou: Components

dn: ou=Ldap,ou=Components,dc=symfony,dc=com
objectclass: organizationalunit
ou: Ldap

dn: ou=Ldap scoping,ou=Ldap,ou=Components,dc=symfony,dc=com
objectclass: organizationalunit
ou: Ldap scoping