8000 [Ldap] Allow search scoping · symfony/symfony@83c7915 · GitHub
[go: up one dir, main page]

Skip to content

Commit 83c7915

Browse files
xuntofabpot
authored andcommitted
[Ldap] Allow search scoping
1 parent c3b9690 commit 83c7915

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

src/Symfony/Component/Ldap/Adapter/AbstractQuery.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ public function __construct(ConnectionInterface $connection, $dn, $query, array
3434
'timeout' => 0,
3535
'deref' => static::DEREF_NEVER,
3636
'attrsOnly' => 0,
37+
'scope' => static::SCOPE_SUB,
3738
));
3839
$resolver->setAllowedValues('deref', array(static::DEREF_ALWAYS, static::DEREF_NEVER, static::DEREF_FINDING, static::DEREF_SEARCHING));
40+
$resolver->setAllowedValues('scope', array(static::SCOPE_BASE, static::SCOPE_ONE, static::SCOPE_SUB));
41+
3942
$resolver->setNormalizer('filter', function (Options $options, $value) {
4043
return is_array($value) ? $value : array($value);
4144
});

src/Symfony/Component/Ldap/Adapter/ExtLdap/Query.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,21 @@ public function execute()
6060

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

63-
$this->search = @ldap_search(
63+
switch ($this->options['scope']) {
64+
case static::SCOPE_BASE:
65+
$func = 'ldap_read';
66+
break;
67+
case static::SCOPE_ONE:
68+
$func = 'ldap_list';
69+
break;
70+
case static::SCOPE_SUB:
71+
$func = 'ldap_search';
72+
break;
73+
default:
74+
throw new LdapException(sprintf('Could not search in scope %s', $this->options['scopen']));
75+
}
76+
77+
$this->search = @$func(
6478
$con,
6579
$this->dn,
6680
$this->query,

src/Symfony/Component/Ldap/Adapter/QueryInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ interface QueryInterface
2323
const DEREF_FINDING = 0x02;
2424
const DEREF_ALWAYS = 0x03;
2525

26+
const SCOPE_BASE = 'base';
27+
const SCOPE_ONE = 'one';
28+
const SCOPE_SUB = 'sub';
29+
2630
/**
2731
* Executes a query and returns the list of Ldap entries.
2832
*

src/Symfony/Component/Ldap/Tests/Adapter/ExtLdap/AdapterTest.php

Lines changed: 34 additions & 0 deletions
D7C8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Ldap\Adapter\ExtLdap\Adapter;
1515
use Symfony E864 \Component\Ldap\Adapter\ExtLdap\Collection;
16+
use Symfony\Component\Ldap\Adapter\ExtLdap\Query;
1617
use Symfony\Component\Ldap\Entry;
1718
use Symfony\Component\Ldap\LdapInterface;
1819

@@ -65,4 +66,37 @@ public function testLdapQueryIterator()
6566
$this->assertEquals(array('Fabien Potencier'), $entry->getAttribute('cn'));
6667
$this->assertEquals(array('fabpot@symfony.com', 'fabien@potencier.com'), $entry->getAttribute('mail'));
6768
}
69+
70+
public function testLdapQueryScopeBase()
71+
{
72+
$ldap = new Adapter($this->getLdapConfig());
73+
74+
$ldap->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony');
75+
76+
$query = $ldap->createQuery('cn=Fabien Potencier,dc=symfony,dc=com', '(objectclass=*)', array(
77+
'scope' => Query::SCOPE_BASE,
78+
));
79+
$result = $query->execute();
80+
81+
$entry = $result[0];
82+
$this->assertEquals($result->count(), 1);
83+
$this->assertEquals(array('Fabien Potencier'), $entry->getAttribute('cn'));
84+
}
85+
86+
public function testLdapQueryScopeOneLevel()
87+
{
88+
$ldap = new Adapter($this->getLdapConfig());
89+
90+
$ldap->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony');
91+
92+
$one_level_result = $ldap->createQuery('ou=Components,dc=symfony,dc=com', '(objectclass=*)', array(
93+
'scope' => Query::SCOPE_ONE,
94+
))->execute();
95+
96+
$subtree_count = $ldap->createQuery('ou=Components,dc=symfony,dc=com', '(objectclass=*)')->execute()->count();
97+
98+
$this->assertNotEquals($one_level_result->count(), $subtree_count);
99+
$this->assertEquals($one_level_result->count(), 1);
100+
$this->assertEquals($one_level_result[0]->getAttribute('ou'), array('Ldap'));
101+
}
68102
}

src/Symfony/Component/Ldap/Tests/Fixtures/data/fixtures.ldif

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ ou: Maintainers
1212
ou: Founder
1313
givenName: Fabien Potencier
1414
description: Founder and project lead @Symfony
15+
16+
dn: ou=Components,dc=symfony,dc=com
17+
objectclass: organizationalunit
18+
ou: Components
19+
20+
dn: ou=Ldap,ou=Components,dc=symfony,dc=com
21+
objectclass: organizationalunit
22+
ou: Ldap
23+
24+
dn: ou=Ldap scoping,ou=Ldap,ou=Components,dc=symfony,dc=com
25+
objectclass: organizationalunit
26+
ou: Ldap scoping

0 commit comments

Comments
 (0)
0