Skip to content
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
13 changes: 12 additions & 1 deletion apps/provisioning_api/lib/Controller/AUserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use OC\Accounts\AccountManager;
use OC\User\Backend;
use OC\User\NoUserException;
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
Expand Down Expand Up @@ -79,7 +80,9 @@ public function __construct(string $appName,
*
* @param string $userId
* @return array
* @throws NotFoundException
* @throws OCSException
* @throws OCSNotFoundException
*/
protected function getUserData(string $userId): array {
$currentLoggedInUser = $this->userSession->getUser();
Expand Down Expand Up @@ -111,9 +114,17 @@ protected function getUserData(string $userId): array {
$gids[] = $group->getGID();
}

try {
# might be thrown by LDAP due to handling of users disappears
# from the external source (reasons unknown to us)
# cf. https://github.com/nextcloud/server/issues/12991
$data['storageLocation'] = $targetUserObject->getHome();
} catch (NoUserException $e) {
throw new OCSNotFoundException($e->getMessage(), $e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why throw the OCS not found exception? This is mainly osmething to throw when the middleware will handle it. The NoUserException seems perfectly valid here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

// Find the data
$data['id'] = $targetUserObject->getUID();
$data['storageLocation'] = $targetUserObject->getHome();
$data['lastLogin'] = $targetUserObject->getLastLogin() * 1000;
$data['backend'] = $targetUserObject->getBackendClassName();
$data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
Expand Down
24 changes: 14 additions & 10 deletions apps/provisioning_api/lib/Controller/GroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,20 @@ public function getGroupUsersDetails(string $groupId, string $search = '', int $
// Extract required number
$usersDetails = [];
foreach ($users as $user) {
/** @var IUser $user */
$userId = (string) $user->getUID();
$userData = $this->getUserData($userId);
// Do not insert empty entry
if(!empty($userData)) {
$usersDetails[$userId] = $userData;
} else {
// Logged user does not have permissions to see this user
// only showing its id
$usersDetails[$userId] = ['id' => $userId];
try {
/** @var IUser $user */
$userId = (string)$user->getUID();
$userData = $this->getUserData($userId);
// Do not insert empty entry
if (!empty($userData)) {
$usersDetails[$userId] = $userData;
} else {
// Logged user does not have permissions to see this user
// only showing its id
$usersDetails[$userId] = ['id' => $userId];
}
} catch(OCSNotFoundException $e) {
// continue if a users ceased to exist.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe at least log something?
What's the use case of having a list of users where the user does not exists here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user is not added to the list when this Exception pops up. Or do you mean something else?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, I don't really like having blank catches :p

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not an error condition, but a valid scenario.

}
}
return new DataResponse(['users' => $usersDetails]);
Expand Down
84 changes: 76 additions & 8 deletions apps/provisioning_api/tests/Controller/GroupsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,39 @@
use OC\Accounts\AccountManager;
use OC\Group\Manager;
use OC\SubAdmin;
use OC\User\NoUserException;
use OCA\Provisioning_API\Controller\GroupsController;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\UserInterface;

class GroupsControllerTest extends \Test\TestCase {

/** @var IRequest|PHPUnit_Framework_MockObject_MockObject */
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
protected $request;
/** @var IUserManager|PHPUnit_Framework_MockObject_MockObject */
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
protected $userManager;
/** @var IConfig|PHPUnit_Framework_MockObject_MockObject */
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
protected $config;
/** @var Manager|PHPUnit_Framework_MockObject_MockObject */
/** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
protected $groupManager;
/** @var IUserSession|PHPUnit_Framework_MockObject_MockObject */
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
protected $userSession;
/** @var AccountManager|PHPUnit_Framework_MockObject_MockObject */
/** @var AccountManager|\PHPUnit_Framework_MockObject_MockObject */
protected $accountManager;
/** @var ILogger|PHPUnit_Framework_MockObject_MockObject */
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
protected $logger;
/** @var SubAdmin|\PHPUnit_Framework_MockObject_MockObject */
protected $subAdminManager;

/** @var GroupsController|PHPUnit_Framework_MockObject_MockObject */
/** @var GroupsController|\PHPUnit_Framework_MockObject_MockObject */
protected $api;


protected function setUp() {
parent::setUp();

Expand Down Expand Up @@ -126,6 +131,10 @@ private function createUser($uid) {
$user
->method('getUID')
->willReturn($uid);
$backendMock = $this->createMock(UserInterface::class);
$user
->method('getBackend')
->willReturn($backendMock);
return $user;
}

Expand Down Expand Up @@ -164,6 +173,19 @@ private function asSubAdminOfGroup($group) {
}));
}

private function useAccountManager() {
$this->accountManager->expects($this->any())
->method('getUser')
->willReturnCallback(function(IUser $user) {
return [
AccountManager::PROPERTY_PHONE => ['value' => '0800-call-' . $user->getUID()],
AccountManager::PROPERTY_ADDRESS => ['value' => 'Holzweg 99, 0601 Herrera, Panama'],
AccountManager::PROPERTY_WEBSITE => ['value' => 'https://' . $user->getUid() . '.pa'],
AccountManager::PROPERTY_TWITTER => ['value' => '@' . $user->getUID()],
];
});
}

public function dataGetGroups() {
return [
[null, 0, 0],
Expand Down Expand Up @@ -454,4 +476,50 @@ public function testDeleteGroup() {

$this->api->deleteGroup('ExistingGroup');
}

public function testGetGroupUsersDetails() {
$gid = 'ncg1';

$this->asAdmin();
$this->useAccountManager();

$users = [
'ncu1' => $this->createUser('ncu1'), # regular
'ncu2' => $this->createUser('ncu2'), # the zombie
];
$users['ncu2']->expects($this->atLeastOnce())
->method('getHome')
->willThrowException(new NoUserException());

$this->userManager->expects($this->any())
->method('get')
->willReturnCallback(function(string $uid) use ($users) {
return isset($users[$uid]) ? $users[$uid] : null;
});

$group = $this->createGroup($gid);
$group->expects($this->once())
->method('searchUsers')
->with('', null, 0)
->willReturn(array_values($users));

$this->groupManager
->method('get')
->with($gid)
->willReturn($group);
$this->groupManager->expects($this->any())
->method('getUserGroups')
->willReturn([$group]);

/** @var \PHPUnit_Framework_MockObject_MockObject */
$this->subAdminManager->expects($this->any())
->method('isSubAdminOfGroup')
->willReturn(false);
$this->subAdminManager->expects($this->any())
->method('getSubAdminsGroups')
->willReturn([]);


$this->api->getGroupUsersDetails($gid);
}
}