Skip to content
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

[stable20] LDAP: when nesting is not enabled, the group filter can be applied right away #23570

Merged
merged 2 commits into from
Oct 21, 2020
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
6 changes: 1 addition & 5 deletions apps/user_ldap/lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,7 @@ public function __clone() {
$this->dontDestruct = true;
}

/**
* @param string $name
* @return bool|mixed
*/
public function __get($name) {
public function __get(string $name) {
if (!$this->configured) {
$this->readConfiguration();
}
Expand Down
7 changes: 6 additions & 1 deletion apps/user_ldap/lib/Group_LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ private function walkNestedGroups(string $dn, Closure $fetcher, array $list): ar
}

$seen = [];
while ($record = array_pop($list)) {
while ($record = array_shift($list)) {
$recordDN = $recordMode ? $record['dn'][0] : $record;
if ($recordDN === $dn || array_key_exists($recordDN, $seen)) {
// Prevent loops
Expand Down Expand Up @@ -822,6 +822,11 @@ private function getGroupsByMember(string $dn, array &$seen = null): array {
$filter .= '@*';
}

$nesting = (int)$this->access->connection->ldapNestedGroups;
if ($nesting === 0) {
$filter = $this->access->combineFilterWithAnd([$filter, $this->access->connection->ldapGroupFilter]);
}

$groups = $this->access->fetchListOfGroups($filter,
[strtolower($this->access->connection->ldapGroupMemberAssocAttr), $this->access->connection->ldapGroupDisplayName, 'dn']);
if (is_array($groups)) {
Expand Down
42 changes: 36 additions & 6 deletions apps/user_ldap/tests/Group_LDAPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -719,23 +719,36 @@ public function testGetUserGroupsMemberOfDisabled() {
$groupBackend->getUserGroups('userX');
}

public function testGetGroupsByMember() {
public function nestedGroupsProvider(): array {
return [
[ true ],
[ false ],
];
}

/**
* @dataProvider nestedGroupsProvider
*/
public function testGetGroupsByMember(bool $nestedGroups) {
$access = $this->getAccessMock();
$pluginManager = $this->getPluginManagerMock();

$groupFilter = '(&(objectclass=nextcloudGroup)(nextcloudEnabled=TRUE))';
$access->connection = $this->createMock(Connection::class);
$access->connection->expects($this->any())
->method('__get')
->willReturnCallback(function ($name) {
->willReturnCallback(function ($name) use ($nestedGroups, $groupFilter) {
switch ($name) {
case 'useMemberOfToDetectMembership':
return 0;
case 'ldapDynamicGroupMemberURL':
return '';
case 'ldapNestedGroups':
return false;
return $nestedGroups;
case 'ldapGroupMemberAssocAttr':
return 'member';
case 'ldapGroupFilter':
return $groupFilter;
}
return 1;
});
Expand All @@ -748,10 +761,15 @@ public function testGetGroupsByMember() {
$access->expects($this->exactly(2))
->method('username2dn')
->willReturn($dn);

$access->expects($this->any())
->method('readAttribute')
->willReturn([]);
$access->expects($this->any())
->method('combineFilterWithAnd')
->willReturnCallback(function (array $filterParts) {
// ⚠ returns a pseudo-filter only, not real LDAP Filter syntax
return implode('&', $filterParts);
});

$group1 = [
'cn' => 'group1',
Expand All @@ -766,9 +784,21 @@ public function testGetGroupsByMember() {
->method('nextcloudGroupNames')
->with([$group1, $group2])
->willReturn(['group1', 'group2']);
$access->expects($this->once())
$access->expects($nestedGroups ? $this->atLeastOnce() : $this->once())
->method('fetchListOfGroups')
->willReturn([$group1, $group2]);
->willReturnCallback(function ($filter, $attr, $limit, $offset) use ($nestedGroups, $groupFilter, $group1, $group2) {
static $firstRun = true;
if (!$nestedGroups) {
// When nested groups are enabled, groups cannot be filtered early as it would
// exclude intermediate groups. But we can, and should, when working with flat groups.
$this->assertTrue(strpos($filter, $groupFilter) !== false);
}
if ($firstRun) {
$firstRun = false;
return [$group1, $group2];
}
return [];
});
$access->expects($this->any())
->method('dn2groupname')
->willReturnCallback(function (string $dn) {
Expand Down