consolidate validity check for users in group class

This commit is contained in:
Arthur Schiwon 2013-08-26 17:46:31 +02:00
parent 64c0e5d807
commit 1f5a55ddff
2 changed files with 29 additions and 28 deletions

View File

@ -62,7 +62,6 @@ class Group {
return $this->users; return $this->users;
} }
$users = array();
$userIds = array(); $userIds = array();
foreach ($this->backends as $backend) { foreach ($this->backends as $backend) {
$diff = array_diff( $diff = array_diff(
@ -74,14 +73,8 @@ class Group {
} }
} }
foreach ($userIds as $userId) { $this->users = $this->getVerifiedUsers($userIds);
$user = $this->userManager->get($userId); return $this->users;
if(!is_null($user)) {
$users[] = $user;
}
}
$this->users = $users;
return $users;
} }
/** /**
@ -116,7 +109,7 @@ class Group {
if ($backend->implementsActions(OC_GROUP_BACKEND_ADD_TO_GROUP)) { if ($backend->implementsActions(OC_GROUP_BACKEND_ADD_TO_GROUP)) {
$backend->addToGroup($user->getUID(), $this->gid); $backend->addToGroup($user->getUID(), $this->gid);
if ($this->users) { if ($this->users) {
$this->users[] = $user; $this->users[$user->getUID()] = $user;
} }
if ($this->emitter) { if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user));
@ -175,12 +168,7 @@ class Group {
if (!is_null($offset)) { if (!is_null($offset)) {
$offset -= count($userIds); $offset -= count($userIds);
} }
foreach ($userIds as $userId) { $users += $this->getVerifiedUsers($userIds);
$user = $this->userManager->get($userId);
if(!is_null($user)) {
$users[$userId] = $user;
}
}
if (!is_null($limit) and $limit <= 0) { if (!is_null($limit) and $limit <= 0) {
return array_values($users); return array_values($users);
} }
@ -197,7 +185,6 @@ class Group {
* @return \OC\User\User[] * @return \OC\User\User[]
*/ */
public function searchDisplayName($search, $limit = null, $offset = null) { public function searchDisplayName($search, $limit = null, $offset = null) {
$users = array();
foreach ($this->backends as $backend) { foreach ($this->backends as $backend) {
if ($backend->implementsActions(OC_GROUP_BACKEND_GET_DISPLAYNAME)) { if ($backend->implementsActions(OC_GROUP_BACKEND_GET_DISPLAYNAME)) {
$userIds = array_keys($backend->displayNamesInGroup($this->gid, $search, $limit, $offset)); $userIds = array_keys($backend->displayNamesInGroup($this->gid, $search, $limit, $offset));
@ -210,12 +197,7 @@ class Group {
if (!is_null($offset)) { if (!is_null($offset)) {
$offset -= count($userIds); $offset -= count($userIds);
} }
foreach ($userIds as $userId) { $users = $this->getVerifiedUsers($userIds);
$user = $this->userManager->get($userId);
if(!is_null($user)) {
$users[$userId] = $user;
}
}
if (!is_null($limit) and $limit <= 0) { if (!is_null($limit) and $limit <= 0) {
return array_values($users); return array_values($users);
} }
@ -244,4 +226,23 @@ class Group {
} }
return $result; return $result;
} }
/**
* @brief returns all the Users from an array that really exists
* @param $userIds an array containing user IDs
* @return an Array with the userId as Key and \OC\User\User as value
*/
private function getVerifiedUsers($userIds) {
if(!is_array($userIds)) {
return array();
}
$users = array();
foreach ($userIds as $userId) {
$user = $this->userManager->get($userId);
if(!is_null($user)) {
$users[$userId] = $user;
}
}
return $users;
}
} }

View File

@ -43,8 +43,8 @@ class Group extends \PHPUnit_Framework_TestCase {
$users = $group->getUsers(); $users = $group->getUsers();
$this->assertEquals(2, count($users)); $this->assertEquals(2, count($users));
$user1 = $users[0]; $user1 = $users['user1'];
$user2 = $users[1]; $user2 = $users['user2'];
$this->assertEquals('user1', $user1->getUID()); $this->assertEquals('user1', $user1->getUID());
$this->assertEquals('user2', $user2->getUID()); $this->assertEquals('user2', $user2->getUID());
} }
@ -68,9 +68,9 @@ class Group extends \PHPUnit_Framework_TestCase {
$users = $group->getUsers(); $users = $group->getUsers();
$this->assertEquals(3, count($users)); $this->assertEquals(3, count($users));
$user1 = $users[0]; $user1 = $users['user1'];
$user2 = $users[1]; $user2 = $users['user2'];
$user3 = $users[2]; $user3 = $users['user3'];
$this->assertEquals('user1', $user1->getUID()); $this->assertEquals('user1', $user1->getUID());
$this->assertEquals('user2', $user2->getUID()); $this->assertEquals('user2', $user2->getUID());
$this->assertEquals('user3', $user3->getUID()); $this->assertEquals('user3', $user3->getUID());