Merge pull request #15606 from owncloud/fix-group-list-and-count-primary-group-only

Fixes returns of group memberships and counting if all members have the ...
This commit is contained in:
Thomas Müller 2015-05-19 14:02:12 +02:00
commit 12aef31115
2 changed files with 78 additions and 9 deletions

View File

@ -292,12 +292,13 @@ class GROUP_LDAP extends BackendUtility implements \OCP\GroupInterface {
public function getUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { public function getUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) {
try { try {
$filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search);
return $this->access->fetchListOfUsers( $users = $this->access->fetchListOfUsers(
$filter, $filter,
array($this->access->connection->ldapUserDisplayName, 'dn'), array($this->access->connection->ldapUserDisplayName, 'dn'),
$limit, $limit,
$offset $offset
); );
return $this->access->ownCloudUserNames($users);
} catch (\Exception $e) { } catch (\Exception $e) {
return array(); return array();
} }
@ -476,8 +477,9 @@ class GROUP_LDAP extends BackendUtility implements \OCP\GroupInterface {
return array(); return array();
} }
$primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset);
$members = array_keys($this->_groupMembers($groupDN)); $members = array_keys($this->_groupMembers($groupDN));
if(!$members) { if(!$members && empty($primaryUsers)) {
//in case users could not be retrieved, return empty result set //in case users could not be retrieved, return empty result set
$this->access->connection->writeToCache($cacheKey, array()); $this->access->connection->writeToCache($cacheKey, array());
return array(); return array();
@ -514,13 +516,11 @@ class GROUP_LDAP extends BackendUtility implements \OCP\GroupInterface {
} }
} }
$groupUsers = array_unique(array_merge($groupUsers, $primaryUsers));
natsort($groupUsers); natsort($groupUsers);
$this->access->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers); $this->access->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers);
$groupUsers = array_slice($groupUsers, $offset, $limit); $groupUsers = array_slice($groupUsers, $offset, $limit);
//and get users that have the group as primary
$primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset);
$groupUsers = array_unique(array_merge($groupUsers, $primaryUsers));
$this->access->connection->writeToCache($cacheKey, $groupUsers); $this->access->connection->writeToCache($cacheKey, $groupUsers);
@ -551,16 +551,15 @@ class GROUP_LDAP extends BackendUtility implements \OCP\GroupInterface {
} }
$members = array_keys($this->_groupMembers($groupDN)); $members = array_keys($this->_groupMembers($groupDN));
if(!$members) { $primaryUserCount = $this->countUsersInPrimaryGroup($groupDN, '');
if(!$members && $primaryUserCount === 0) {
//in case users could not be retrieved, return empty result set //in case users could not be retrieved, return empty result set
$this->access->connection->writeToCache($cacheKey, false); $this->access->connection->writeToCache($cacheKey, false);
return false; return false;
} }
if(empty($search)) { if(empty($search)) {
$primaryUsers = $this->countUsersInPrimaryGroup($groupDN, ''); $groupUsers = count($members) + $primaryUserCount;
$groupUsers = count($members) + $primaryUsers;
$this->access->connection->writeToCache($cacheKey, $groupUsers); $this->access->connection->writeToCache($cacheKey, $groupUsers);
return $groupUsers; return $groupUsers;
} }

View File

@ -313,4 +313,74 @@ class Test_Group_Ldap extends \Test\TestCase {
$this->assertSame(2, count($groups)); $this->assertSame(2, count($groups));
} }
/**
* tests that a user listing is complete, if all it's members have the group
* as their primary.
*/
public function testUsersInGroupPrimaryMembersOnly() {
$access = $this->getAccessMock();
$this->enableGroups($access);
$access->connection->expects($this->any())
->method('getFromCache')
->will($this->returnValue(null));
$access->expects($this->any())
->method('readAttribute')
->will($this->returnCallback(function($dn, $attr) {
if($attr === 'primaryGroupToken') {
return array(1337);
}
return array();
}));
$access->expects($this->any())
->method('groupname2dn')
->will($this->returnValue('cn=foobar,dc=foo,dc=bar'));
$access->expects($this->once())
->method('ownCloudUserNames')
->will($this->returnValue(array('lisa', 'bart', 'kira', 'brad')));
$groupBackend = new GroupLDAP($access);
$users = $groupBackend->usersInGroup('foobar');
$this->assertSame(4, count($users));
}
/**
* tests that a user counting is complete, if all it's members have the group
* as their primary.
*/
public function testCountUsersInGroupPrimaryMembersOnly() {
$access = $this->getAccessMock();
$this->enableGroups($access);
$access->connection->expects($this->any())
->method('getFromCache')
->will($this->returnValue(null));
$access->expects($this->any())
->method('readAttribute')
->will($this->returnCallback(function($dn, $attr) {
if($attr === 'primaryGroupToken') {
return array(1337);
}
return array();
}));
$access->expects($this->any())
->method('groupname2dn')
->will($this->returnValue('cn=foobar,dc=foo,dc=bar'));
$access->expects($this->once())
->method('countUsers')
->will($this->returnValue(4));
$groupBackend = new GroupLDAP($access);
$users = $groupBackend->countUsersInGroup('foobar');
$this->assertSame(4, $users);
}
} }