Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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 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
3 changes: 3 additions & 0 deletions 3 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_SUB,
));
$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_ONE, static::SCOPE_SUB));

$resolver->setNormalizer('filter', function (Options $options, $value) {
return is_array($value) ? $value : array($value);
});
Expand Down
16 changes: 15 additions & 1 deletion 16 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_ONE:
$func = 'ldap_list';
break;
case static::SCOPE_SUB:
$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 4 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_ONE = 'one';
const SCOPE_SUB = 'sub';

/**
* Executes a query and returns the list of Ldap entries.
*
Expand Down
34 changes: 34 additions & 0 deletions 34 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_ONE,
))->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 12 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.