From 0fdeefe47c82b18eb6adf1bd66ec2471b4d76c25 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Thu, 11 Aug 2016 09:45:15 +0200 Subject: Add ProvisioningAPI middleware The provisioning API has 3 access levels: * Admin * SubAdmin * User This middleware adds a check for the SubAdmin part. --- apps/provisioning_api/lib/AppInfo/Application.php | 28 ++++++++++ .../Middleware/Exceptions/NotSubAdminException.php | 11 ++++ .../lib/Middleware/ProvisioningApiMiddleware.php | 64 ++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 apps/provisioning_api/lib/AppInfo/Application.php create mode 100644 apps/provisioning_api/lib/Middleware/Exceptions/NotSubAdminException.php create mode 100644 apps/provisioning_api/lib/Middleware/ProvisioningApiMiddleware.php (limited to 'apps') diff --git a/apps/provisioning_api/lib/AppInfo/Application.php b/apps/provisioning_api/lib/AppInfo/Application.php new file mode 100644 index 00000000000..2d6a82e2ff9 --- /dev/null +++ b/apps/provisioning_api/lib/AppInfo/Application.php @@ -0,0 +1,28 @@ +getContainer(); + $server = $container->getServer(); + + $container->registerService('ProvisioningApiMiddleware', function(SimpleContainer $c) use ($server) { + $user = $server->getUserManager()->get($c['UserId']); + $isAdmin = $user !== null ? $server->getGroupManager()->isAdmin($user->getUID()) : false; + $isSubAdmin = $user !== null ? $server->getGroupManager()->getSubAdmin()->isSubAdmin($user) : false; + return new ProvisioningApiMiddleware( + $c['ControllerMethodReflector'], + $isAdmin, + $isSubAdmin + ); + }); + $container->registerMiddleWare('ProvisioningApiMiddleware'); + } +} diff --git a/apps/provisioning_api/lib/Middleware/Exceptions/NotSubAdminException.php b/apps/provisioning_api/lib/Middleware/Exceptions/NotSubAdminException.php new file mode 100644 index 00000000000..007ea04db46 --- /dev/null +++ b/apps/provisioning_api/lib/Middleware/Exceptions/NotSubAdminException.php @@ -0,0 +1,11 @@ +reflector = $reflector; + $this->isAdmin = $isAdmin; + $this->isSubAdmin = $isSubAdmin; + } + + /** + * @param \OCP\AppFramework\Controller $controller + * @param string $methodName + * + * @throws NotSubAdminException + */ + public function beforeController($controller, $methodName) { + if (!$this->isAdmin && !$this->reflector->hasAnnotation('NoSubAdminRequired') && !$this->isSubAdmin) { + throw new NotSubAdminException(); + } + } + + /** + * @param \OCP\AppFramework\Controller $controller + * @param string $methodName + * @param \Exception $exception + * @throws \Exception + * @return Response + */ + public function afterException($controller, $methodName, \Exception $exception) { + if ($exception instanceof NotSubAdminException) { + throw new OCSException($exception->getMessage(), \OCP\API::RESPOND_UNAUTHORISED); + } + + throw $exception; + } +} \ No newline at end of file -- cgit v1.2.3 From 432e7c93c6dad564abbaec1e3d374f73653d7ba6 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Thu, 11 Aug 2016 09:46:25 +0200 Subject: Move Groups over to OCSController * Take advantage of the AppFramework * Fix tests --- apps/provisioning_api/appinfo/routes.php | 27 +- .../lib/Controller/GroupsController.php | 190 +++++++++ apps/provisioning_api/lib/Groups.php | 182 -------- .../tests/Controller/GroupsControllerTest.php | 378 +++++++++++++++++ apps/provisioning_api/tests/GroupsTest.php | 459 --------------------- 5 files changed, 580 insertions(+), 656 deletions(-) create mode 100644 apps/provisioning_api/lib/Controller/GroupsController.php delete mode 100644 apps/provisioning_api/lib/Groups.php create mode 100644 apps/provisioning_api/tests/Controller/GroupsControllerTest.php delete mode 100644 apps/provisioning_api/tests/GroupsTest.php (limited to 'apps') diff --git a/apps/provisioning_api/appinfo/routes.php b/apps/provisioning_api/appinfo/routes.php index 907e16ebf3c..dbc0321efb2 100644 --- a/apps/provisioning_api/appinfo/routes.php +++ b/apps/provisioning_api/appinfo/routes.php @@ -26,13 +26,22 @@ * */ -namespace OCA\Provisioning_API\AppInfo; - use OCA\Provisioning_API\Apps; -use OCA\Provisioning_API\Groups; use OCA\Provisioning_API\Users; use OCP\API; +$app = new \OCA\Provisioning_API\AppInfo\Application(); +$app->registerRoutes($this, [ + 'ocs' => [ + // Groups + ['root' => '/cloud', 'name' => 'Groups#getGroups', 'url' => '/groups', 'verb' => 'GET'], + ['root' => '/cloud', 'name' => 'Groups#getGroup', 'url' => '/groups/{groupId}', 'verb' => 'GET'], + ['root' => '/cloud', 'name' => 'Groups#addGroup', 'url' => '/groups', 'verb' => 'POST'], + ['root' => '/cloud', 'name' => 'Groups#deleteGroup', 'url' => '/groups/{groupId}', 'verb' => 'DELETE'], + ['root' => '/cloud', 'name' => 'Groups#getSubAdminsOfGroup', 'url' => '/groups/{groupId}/subadmins', 'verb' => 'GET'], + ], +]); + // Users $users = new Users( \OC::$server->getUserManager(), @@ -55,18 +64,6 @@ API::register('post', '/cloud/users/{userid}/subadmins', [$users, 'addSubAdmin'] API::register('delete', '/cloud/users/{userid}/subadmins', [$users, 'removeSubAdmin'], 'provisioning_api', API::ADMIN_AUTH); API::register('get', '/cloud/users/{userid}/subadmins', [$users, 'getUserSubAdminGroups'], 'provisioning_api', API::ADMIN_AUTH); -// Groups -$groups = new Groups( - \OC::$server->getGroupManager(), - \OC::$server->getUserSession(), - \OC::$server->getRequest() -); -API::register('get', '/cloud/groups', [$groups, 'getGroups'], 'provisioning_api', API::SUBADMIN_AUTH); -API::register('post', '/cloud/groups', [$groups, 'addGroup'], 'provisioning_api', API::SUBADMIN_AUTH); -API::register('get', '/cloud/groups/{groupid}', [$groups, 'getGroup'], 'provisioning_api', API::SUBADMIN_AUTH); -API::register('delete', '/cloud/groups/{groupid}', [$groups, 'deleteGroup'], 'provisioning_api', API::ADMIN_AUTH); -API::register('get', '/cloud/groups/{groupid}/subadmins', [$groups, 'getSubAdminsOfGroup'], 'provisioning_api', API::ADMIN_AUTH); - // Apps $apps = new Apps( \OC::$server->getAppManager(), diff --git a/apps/provisioning_api/lib/Controller/GroupsController.php b/apps/provisioning_api/lib/Controller/GroupsController.php new file mode 100644 index 00000000000..d36d0de8997 --- /dev/null +++ b/apps/provisioning_api/lib/Controller/GroupsController.php @@ -0,0 +1,190 @@ + + * @author Morris Jobke + * @author Roeland Jago Douma + * @author Tom Needham + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Provisioning_API\Controller; + +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSException; +use OCP\AppFramework\OCSController; +use OCP\IGroup; +use OCP\IGroupManager; +use OCP\IRequest; +use OCP\IUserSession; +use OCP\IUser; + + +class GroupsController extends OCSController { + + /** @var IGroupManager */ + private $groupManager; + + /** @var IUserSession */ + private $userSession; + + /** + * @param string $appName + * @param IRequest $request + * @param IGroupManager $groupManager + * @param IUserSession $userSession + */ + public function __construct( + $appName, + IRequest $request, + IGroupManager $groupManager, + IUserSession $userSession) { + parent::__construct($appName, $request); + + $this->groupManager = $groupManager; + $this->userSession = $userSession; + } + + /** + * returns a list of groups + * + * @NoAdminRequired + * + * @param string $search + * @param int $limit + * @param int $offset + * @return DataResponse + */ + public function getGroups($search = '', $limit = null, $offset = null) { + if ($limit !== null) { + $limit = (int)$limit; + } + if ($offset !== null) { + $offset = (int)$offset; + } + + $groups = $this->groupManager->search($search, $limit, $offset); + $groups = array_map(function($group) { + /** @var IGroup $group */ + return $group->getGID(); + }, $groups); + + return new DataResponse(['groups' => $groups]); + } + + /** + * returns an array of users in the group specified + * + * @NoAdminRequired + * + * @param string $groupId + * @return DataResponse + * @throws OCSException + */ + public function getGroup($groupId) { + $user = $this->userSession->getUser(); + + // Check the group exists + if(!$this->groupManager->groupExists($groupId)) { + throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND); + } + + $isSubadminOfGroup = false; + $group = $this->groupManager->get($groupId); + if ($group !== null) { + $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminofGroup($user, $group); + } + + // Check subadmin has access to this group + if($this->groupManager->isAdmin($user->getUID()) + || $isSubadminOfGroup) { + $users = $this->groupManager->get($groupId)->getUsers(); + $users = array_map(function($user) { + /** @var IUser $user */ + return $user->getUID(); + }, $users); + $users = array_values($users); + return new DataResponse(['users' => $users]); + } else { + throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED); + } + } + + /** + * creates a new group + * + * @NoAdminRequired + * + * @param string $groupid + * @return DataResponse + * @throws OCSException + */ + public function addGroup($groupid) { + // Validate name + if(empty($groupid)){ + \OCP\Util::writeLog('provisioning_api', 'Group name not supplied', \OCP\Util::ERROR); + throw new OCSException('Invalid group name', 101); + } + // Check if it exists + if($this->groupManager->groupExists($groupid)){ + throw new OCSException('', 102); + } + $this->groupManager->createGroup($groupid); + return new DataResponse(); + } + + /** + * @param string $groupId + * @return DataResponse + * @throws OCSException + */ + public function deleteGroup($groupId) { + // Check it exists + if(!$this->groupManager->groupExists($groupId)){ + throw new OCSException('', 101); + } else if($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()){ + // Cannot delete admin group + throw new OCSException('', 102); + } + + return new DataResponse(null, 100); + } + + /** + * @param string $groupId + * @return DataResponse + * @throws OCSException + */ + public function getSubAdminsOfGroup($groupId) { + // Check group exists + $targetGroup = $this->groupManager->get($groupId); + if($targetGroup === null) { + throw new OCSException('Group does not exist', 101); + } + + $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup); + // New class returns IUser[] so convert back + $uids = []; + foreach ($subadmins as $user) { + $uids[] = $user->getUID(); + } + + return new DataResponse($uids); + } + +} diff --git a/apps/provisioning_api/lib/Groups.php b/apps/provisioning_api/lib/Groups.php deleted file mode 100644 index 18302595ae9..00000000000 --- a/apps/provisioning_api/lib/Groups.php +++ /dev/null @@ -1,182 +0,0 @@ - - * @author Morris Jobke - * @author Roeland Jago Douma - * @author Tom Needham - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Provisioning_API; - -use OCP\IGroup; -use OCP\IUser; - -class Groups{ - - /** @var \OCP\IGroupManager */ - private $groupManager; - - /** @var \OCP\IUserSession */ - private $userSession; - - /** @var \OCP\IRequest */ - private $request; - - /** - * @param \OCP\IGroupManager $groupManager - * @param \OCP\IUserSession $userSession - * @param \OCP\IRequest $request - */ - public function __construct(\OCP\IGroupManager $groupManager, - \OCP\IUserSession $userSession, - \OCP\IRequest $request) { - $this->groupManager = $groupManager; - $this->userSession = $userSession; - $this->request = $request; - } - - /** - * returns a list of groups - * - * @param array $parameters - * @return \OC\OCS\Result - */ - public function getGroups($parameters) { - $search = $this->request->getParam('search', ''); - $limit = $this->request->getParam('limit'); - $offset = $this->request->getParam('offset'); - - if ($limit !== null) { - $limit = (int)$limit; - } - if ($offset !== null) { - $offset = (int)$offset; - } - - $groups = $this->groupManager->search($search, $limit, $offset); - $groups = array_map(function($group) { - /** @var IGroup $group */ - return $group->getGID(); - }, $groups); - - return new \OC\OCS\Result(['groups' => $groups]); - } - - /** - * returns an array of users in the group specified - * - * @param array $parameters - * @return \OC\OCS\Result - */ - public function getGroup($parameters) { - // Check if user is logged in - $user = $this->userSession->getUser(); - if ($user === null) { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED); - } - - $groupId = $parameters['groupid']; - - // Check the group exists - if(!$this->groupManager->groupExists($groupId)) { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The requested group could not be found'); - } - - $isSubadminOfGroup = false; - $group = $this->groupManager->get($groupId); - if ($group !== null) { - $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminofGroup($user, $group); - } - - // Check subadmin has access to this group - if($this->groupManager->isAdmin($user->getUID()) - || $isSubadminOfGroup) { - $users = $this->groupManager->get($groupId)->getUsers(); - $users = array_map(function($user) { - /** @var IUser $user */ - return $user->getUID(); - }, $users); - $users = array_values($users); - return new \OC\OCS\Result(['users' => $users]); - } else { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED, 'User does not have access to specified group'); - } - } - - /** - * creates a new group - * - * @param array $parameters - * @return \OC\OCS\Result - */ - public function addGroup($parameters) { - // Validate name - $groupId = $this->request->getParam('groupid', ''); - if(empty($groupId)){ - \OCP\Util::writeLog('provisioning_api', 'Group name not supplied', \OCP\Util::ERROR); - return new \OC\OCS\Result(null, 101, 'Invalid group name'); - } - // Check if it exists - if($this->groupManager->groupExists($groupId)){ - return new \OC\OCS\Result(null, 102); - } - $this->groupManager->createGroup($groupId); - return new \OC\OCS\Result(null, 100); - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function deleteGroup($parameters) { - // Check it exists - if(!$this->groupManager->groupExists($parameters['groupid'])){ - return new \OC\OCS\Result(null, 101); - } else if($parameters['groupid'] === 'admin' || !$this->groupManager->get($parameters['groupid'])->delete()){ - // Cannot delete admin group - return new \OC\OCS\Result(null, 102); - } else { - return new \OC\OCS\Result(null, 100); - } - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function getSubAdminsOfGroup($parameters) { - $group = $parameters['groupid']; - // Check group exists - $targetGroup = $this->groupManager->get($group); - if($targetGroup === null) { - return new \OC\OCS\Result(null, 101, 'Group does not exist'); - } - - $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup); - // New class returns IUser[] so convert back - $uids = []; - foreach ($subadmins as $user) { - $uids[] = $user->getUID(); - } - - return new \OC\OCS\Result($uids); - } - -} diff --git a/apps/provisioning_api/tests/Controller/GroupsControllerTest.php b/apps/provisioning_api/tests/Controller/GroupsControllerTest.php new file mode 100644 index 00000000000..25059e85425 --- /dev/null +++ b/apps/provisioning_api/tests/Controller/GroupsControllerTest.php @@ -0,0 +1,378 @@ + + * @author Lukas Reschke + * @author Morris Jobke + * @author Roeland Jago Douma + * @author Tom Needham + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Provisioning_API\Tests\Controller; + +use OCA\Provisioning_API\Controller\GroupsController; +use OCP\IGroupManager; +use OCP\IUserSession; + +class GroupsControllerTest extends \Test\TestCase { + /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $groupManager; + /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */ + protected $userSession; + /** @var \OC\SubAdmin|\PHPUnit_Framework_MockObject_MockObject */ + protected $subAdminManager; + /** @var GroupsController */ + protected $api; + + protected function setUp() { + parent::setUp(); + + $this->subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor() + ->getMock(); + + $this->groupManager = $this->getMockBuilder('OC\Group\Manager') + ->disableOriginalConstructor() + ->getMock(); + $this->groupManager + ->method('getSubAdmin') + ->willReturn($this->subAdminManager); + + $this->userSession = $this->getMockBuilder('OCP\IUserSession') + ->disableOriginalConstructor() + ->getMock(); + $request = $this->getMockBuilder('OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->api = new GroupsController( + 'provisioning_api', + $request, + $this->groupManager, + $this->userSession + ); + } + + /** + * @param string $gid + * @return \OCP\IGroup|\PHPUnit_Framework_MockObject_MockObject + */ + private function createGroup($gid) { + $group = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $group + ->method('getGID') + ->willReturn($gid); + return $group; + } + + /** + * @param string $uid + * @return \OCP\IUser|\PHPUnit_Framework_MockObject_MockObject + */ + private function createUser($uid) { + $user = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $user + ->method('getUID') + ->willReturn($uid); + return $user; + } + + private function asUser() { + $user = $this->createUser('user'); + $this->userSession + ->method('getUser') + ->willReturn($user); + } + + private function asAdmin() { + $user = $this->createUser('admin'); + $this->userSession + ->method('getUser') + ->willReturn($user); + + $this->groupManager + ->method('isAdmin') + ->with('admin') + ->willReturn(true); + } + + private function asSubAdminOfGroup($group) { + $user = $this->createUser('subAdmin'); + $this->userSession + ->method('getUser') + ->willReturn($user); + + $this->subAdminManager + ->method('isSubAdminOfGroup') + ->will($this->returnCallback(function($_user, $_group) use ($user, $group) { + if ($_user === $user && $_group === $group) { + return true; + } + return false; + })); + } + + public function dataGetGroups() { + return [ + [null, null, null], + ['foo', null, null], + [null, 1, null], + [null, null, 2], + ['foo', 1, 2], + ]; + } + + /** + * @dataProvider dataGetGroups + * + * @param string|null $search + * @param int|null $limit + * @param int|null $offset + */ + public function testGetGroups($search, $limit, $offset) { + $groups = [$this->createGroup('group1'), $this->createGroup('group2')]; + + $search = $search === null ? '' : $search; + + $this->groupManager + ->expects($this->once()) + ->method('search') + ->with($search, $limit, $offset) + ->willReturn($groups); + + $result = $this->api->getGroups($search, $limit, $offset); + $this->assertEquals(['groups' => ['group1', 'group2']], $result->getData()); + } + + public function testGetGroupAsSubadmin() { + $group = $this->createGroup('group'); + $this->asSubAdminOfGroup($group); + + $this->groupManager + ->method('get') + ->with('group') + ->willReturn($group); + $this->groupManager + ->method('groupExists') + ->with('group') + ->willReturn(true); + $group + ->method('getUsers') + ->willReturn([ + $this->createUser('user1'), + $this->createUser('user2') + ]); + + $result = $this->api->getGroup('group'); + + $this->assertEquals(['users' => ['user1', 'user2']], $result->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 997 + */ + public function testGetGroupAsIrrelevantSubadmin() { + $group = $this->createGroup('group'); + $otherGroup = $this->createGroup('otherGroup'); + $this->asSubAdminOfGroup($otherGroup); + + $this->groupManager + ->method('get') + ->with('group') + ->willReturn($group); + $this->groupManager + ->method('groupExists') + ->with('group') + ->willReturn(true); + + $this->api->getGroup('group'); + } + + public function testGetGroupAsAdmin() { + $group = $this->createGroup('group'); + $this->asAdmin(); + + $this->groupManager + ->method('get') + ->with('group') + ->willReturn($group); + $this->groupManager + ->method('groupExists') + ->with('group') + ->willReturn(true); + $group + ->method('getUsers') + ->willReturn([ + $this->createUser('user1'), + $this->createUser('user2') + ]); + + $result = $this->api->getGroup('group'); + + $this->assertEquals(['users' => ['user1', 'user2']], $result->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 998 + * @expectedExceptionMessage The requested group could not be found + */ + public function testGetGroupNonExisting() { + $this->asUser(); + + $this->api->getGroup($this->getUniqueID()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + * @expectedExceptionMessage Group does not exist + */ + public function testGetSubAdminsOfGroupsNotExists() { + $this->api->getSubAdminsOfGroup('NonExistingGroup'); + } + + public function testGetSubAdminsOfGroup() { + $group = $this->createGroup('GroupWithSubAdmins'); + $this->groupManager + ->method('get') + ->with('GroupWithSubAdmins') + ->willReturn($group); + + $this->subAdminManager + ->expects($this->once()) + ->method('getGroupsSubAdmins') + ->with($group) + ->willReturn([ + $this->createUser('SubAdmin1'), + $this->createUser('SubAdmin2'), + ]); + + $result = $this->api->getSubAdminsOfGroup('GroupWithSubAdmins'); + $this->assertEquals(['SubAdmin1', 'SubAdmin2'], $result->getData()); + } + + public function testGetSubAdminsOfGroupEmptyList() { + $group = $this->createGroup('GroupWithOutSubAdmins'); + $this->groupManager + ->method('get') + ->with('GroupWithOutSubAdmins') + ->willReturn($group); + + $this->subAdminManager + ->expects($this->once()) + ->method('getGroupsSubAdmins') + ->with($group) + ->willReturn([ + ]); + + $result = $this->api->getSubAdminsOfGroup('GroupWithOutSubAdmins'); + $this->assertEquals([], $result->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + * @expectedExceptionMessage Invalid group name + */ + public function testAddGroupEmptyGroup() { + $this->api->addGroup(''); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 102 + */ + public function testAddGroupExistingGroup() { + $this->groupManager + ->method('groupExists') + ->with('ExistingGroup') + ->willReturn(true); + + $this->api->addGroup('ExistingGroup'); + } + + public function testAddGroup() { + $this->groupManager + ->method('groupExists') + ->with('NewGroup') + ->willReturn(false); + + $this->groupManager + ->expects($this->once()) + ->method('createGroup') + ->with('NewGroup'); + + $this->api->addGroup('NewGroup'); + } + + public function testAddGroupWithSpecialChar() { + $this->groupManager + ->method('groupExists') + ->with('Iñtërnâtiônàlizætiøn') + ->willReturn(false); + + $this->groupManager + ->expects($this->once()) + ->method('createGroup') + ->with('Iñtërnâtiônàlizætiøn'); + + $this->api->addGroup('Iñtërnâtiônàlizætiøn'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + */ + public function testDeleteGroupNonExisting() { + $this->api->deleteGroup('NonExistingGroup'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 102 + */ + public function testDeleteAdminGroup() { + $this->groupManager + ->method('groupExists') + ->with('admin') + ->willReturn('true'); + + $this->api->deleteGroup('admin'); + } + + public function testDeleteGroup() { + $this->groupManager + ->method('groupExists') + ->with('ExistingGroup') + ->willReturn('true'); + + $group = $this->createGroup('ExistingGroup'); + $this->groupManager + ->method('get') + ->with('ExistingGroup') + ->willReturn($group); + $group + ->expects($this->once()) + ->method('delete') + ->willReturn(true); + + $this->api->deleteGroup('ExistingGroup'); + } +} diff --git a/apps/provisioning_api/tests/GroupsTest.php b/apps/provisioning_api/tests/GroupsTest.php deleted file mode 100644 index 2fa19c4f8b4..00000000000 --- a/apps/provisioning_api/tests/GroupsTest.php +++ /dev/null @@ -1,459 +0,0 @@ - - * @author Lukas Reschke - * @author Morris Jobke - * @author Roeland Jago Douma - * @author Tom Needham - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Provisioning_API\Tests; - -use OCA\Provisioning_API\Groups; -use OCP\API; -use OCP\IGroupManager; -use OCP\IUserSession; -use OCP\IRequest; - -class GroupsTest extends \Test\TestCase { - /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ - protected $groupManager; - /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */ - protected $userSession; - /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */ - protected $request; - /** @var \OC\SubAdmin|\PHPUnit_Framework_MockObject_MockObject */ - protected $subAdminManager; - /** @var Groups */ - protected $api; - - protected function setUp() { - parent::setUp(); - - $this->subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor() - ->getMock(); - - $this->groupManager = $this->getMockBuilder('OC\Group\Manager') - ->disableOriginalConstructor() - ->getMock(); - $this->groupManager - ->method('getSubAdmin') - ->willReturn($this->subAdminManager); - - $this->userSession = $this->getMockBuilder('OCP\IUserSession') - ->disableOriginalConstructor() - ->getMock(); - $this->request = $this->getMockBuilder('OCP\IRequest') - ->disableOriginalConstructor() - ->getMock(); - $this->api = new Groups( - $this->groupManager, - $this->userSession, - $this->request - ); - } - - /** - * @param string $gid - * @return \OCP\IGroup|\PHPUnit_Framework_MockObject_MockObject - */ - private function createGroup($gid) { - $group = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $group - ->method('getGID') - ->willReturn($gid); - return $group; - } - - /** - * @param string $uid - * @return \OCP\IUser|\PHPUnit_Framework_MockObject_MockObject - */ - private function createUser($uid) { - $user = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $user - ->method('getUID') - ->willReturn($uid); - return $user; - } - - private function asUser() { - $user = $this->createUser('user'); - $this->userSession - ->method('getUser') - ->willReturn($user); - } - - private function asAdmin() { - $user = $this->createUser('admin'); - $this->userSession - ->method('getUser') - ->willReturn($user); - - $this->groupManager - ->method('isAdmin') - ->with('admin') - ->willReturn(true); - } - - private function asSubAdminOfGroup($group) { - $user = $this->createUser('subAdmin'); - $this->userSession - ->method('getUser') - ->willReturn($user); - - $this->subAdminManager - ->method('isSubAdminOfGroup') - ->will($this->returnCallback(function($_user, $_group) use ($user, $group) { - if ($_user === $user && $_group === $group) { - return true; - } - return false; - })); - } - - public function dataGetGroups() { - return [ - [null, null, null], - ['foo', null, null], - [null, 1, null], - [null, null, 2], - ['foo', 1, 2], - ]; - } - - /** - * @dataProvider dataGetGroups - * - * @param string|null $search - * @param int|null $limit - * @param int|null $offset - */ - public function testGetGroups($search, $limit, $offset) { - $this->request - ->expects($this->exactly(3)) - ->method('getParam') - ->will($this->returnValueMap([ - ['search', '', $search], - ['limit', null, $limit], - ['offset', null, $offset], - ])); - - $groups = [$this->createGroup('group1'), $this->createGroup('group2')]; - - $search = $search === null ? '' : $search; - - $this->groupManager - ->expects($this->once()) - ->method('search') - ->with($search, $limit, $offset) - ->willReturn($groups); - - $result = $this->api->getGroups([]); - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertTrue($result->succeeded()); - $this->assertEquals(['group1', 'group2'], $result->getData()['groups']); - } - - public function testGetGroupAsUser() { - $result = $this->api->getGroup([]); - - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertFalse($result->succeeded()); - $this->assertEquals(API::RESPOND_UNAUTHORISED, $result->getStatusCode()); - - } - - public function testGetGroupAsSubadmin() { - $group = $this->createGroup('group'); - $this->asSubAdminOfGroup($group); - - $this->groupManager - ->method('get') - ->with('group') - ->willReturn($group); - $this->groupManager - ->method('groupExists') - ->with('group') - ->willReturn(true); - $group - ->method('getUsers') - ->willReturn([ - $this->createUser('user1'), - $this->createUser('user2') - ]); - - $result = $this->api->getGroup([ - 'groupid' => 'group', - ]); - - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertTrue($result->succeeded()); - $this->assertEquals(1, sizeof($result->getData()), 'Asserting the result data array only has the "users" key'); - $this->assertArrayHasKey('users', $result->getData()); - $this->assertEquals(['user1', 'user2'], $result->getData()['users']); - } - - public function testGetGroupAsIrrelevantSubadmin() { - $group = $this->createGroup('group'); - $otherGroup = $this->createGroup('otherGroup'); - $this->asSubAdminOfGroup($otherGroup); - - $this->groupManager - ->method('get') - ->with('group') - ->willReturn($group); - $this->groupManager - ->method('groupExists') - ->with('group') - ->willReturn(true); - - $result = $this->api->getGroup([ - 'groupid' => 'group', - ]); - - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertFalse($result->succeeded()); - $this->assertEquals(API::RESPOND_UNAUTHORISED, $result->getStatusCode()); - } - - public function testGetGroupAsAdmin() { - $group = $this->createGroup('group'); - $this->asAdmin(); - - $this->groupManager - ->method('get') - ->with('group') - ->willReturn($group); - $this->groupManager - ->method('groupExists') - ->with('group') - ->willReturn(true); - $group - ->method('getUsers') - ->willReturn([ - $this->createUser('user1'), - $this->createUser('user2') - ]); - - $result = $this->api->getGroup([ - 'groupid' => 'group', - ]); - - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertTrue($result->succeeded()); - $this->assertEquals(1, sizeof($result->getData()), 'Asserting the result data array only has the "users" key'); - $this->assertArrayHasKey('users', $result->getData()); - $this->assertEquals(['user1', 'user2'], $result->getData()['users']); - } - - public function testGetGroupNonExisting() { - $this->asUser(); - - $result = $this->api->getGroup([ - 'groupid' => $this->getUniqueID() - ]); - - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertFalse($result->succeeded()); - $this->assertEquals(API::RESPOND_NOT_FOUND, $result->getStatusCode()); - $this->assertEquals('The requested group could not be found', $result->getMeta()['message']); - } - - public function testGetSubAdminsOfGroupsNotExists() { - $result = $this->api->getSubAdminsOfGroup([ - 'groupid' => 'NonExistingGroup', - ]); - - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertFalse($result->succeeded()); - $this->assertEquals(101, $result->getStatusCode()); - $this->assertEquals('Group does not exist', $result->getMeta()['message']); - } - - public function testGetSubAdminsOfGroup() { - $group = $this->createGroup('GroupWithSubAdmins'); - $this->groupManager - ->method('get') - ->with('GroupWithSubAdmins') - ->willReturn($group); - - $this->subAdminManager - ->expects($this->once()) - ->method('getGroupsSubAdmins') - ->with($group) - ->willReturn([ - $this->createUser('SubAdmin1'), - $this->createUser('SubAdmin2'), - ]); - - $result = $this->api->getSubAdminsOfGroup([ - 'groupid' => 'GroupWithSubAdmins', - ]); - - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertTrue($result->succeeded()); - $this->assertEquals(['SubAdmin1', 'SubAdmin2'], $result->getData()); - } - - public function testGetSubAdminsOfGroupEmptyList() { - $group = $this->createGroup('GroupWithOutSubAdmins'); - $this->groupManager - ->method('get') - ->with('GroupWithOutSubAdmins') - ->willReturn($group); - - $this->subAdminManager - ->expects($this->once()) - ->method('getGroupsSubAdmins') - ->with($group) - ->willReturn([ - ]); - - $result = $this->api->getSubAdminsOfGroup([ - 'groupid' => 'GroupWithOutSubAdmins', - ]); - - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertTrue($result->succeeded()); - $this->assertEquals([], $result->getData()); - } - - public function testAddGroupEmptyGroup() { - $this->request - ->method('getParam') - ->with('groupid') - ->willReturn(''); - - $result = $this->api->addGroup([]); - - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertFalse($result->succeeded()); - $this->assertEquals(101, $result->getStatusCode()); - $this->assertEquals('Invalid group name', $result->getMeta()['message']); - } - - public function testAddGroupExistingGroup() { - $this->request - ->method('getParam') - ->with('groupid') - ->willReturn('ExistingGroup'); - - $this->groupManager - ->method('groupExists') - ->with('ExistingGroup') - ->willReturn(true); - - $result = $this->api->addGroup([]); - - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertFalse($result->succeeded()); - $this->assertEquals(102, $result->getStatusCode()); - } - - public function testAddGroup() { - $this->request - ->method('getParam') - ->with('groupid') - ->willReturn('NewGroup'); - - $this->groupManager - ->method('groupExists') - ->with('NewGroup') - ->willReturn(false); - - $this->groupManager - ->expects($this->once()) - ->method('createGroup') - ->with('NewGroup'); - - $result = $this->api->addGroup([]); - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertTrue($result->succeeded()); - } - - public function testAddGroupWithSpecialChar() { - $this->request - ->method('getParam') - ->with('groupid') - ->willReturn('Iñtërnâtiônàlizætiøn'); - - $this->groupManager - ->method('groupExists') - ->with('Iñtërnâtiônàlizætiøn') - ->willReturn(false); - - $this->groupManager - ->expects($this->once()) - ->method('createGroup') - ->with('Iñtërnâtiônàlizætiøn'); - - $result = $this->api->addGroup([]); - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertTrue($result->succeeded()); - } - - public function testDeleteGroupNonExisting() { - $result = $this->api->deleteGroup([ - 'groupid' => 'NonExistingGroup' - ]); - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertFalse($result->succeeded()); - $this->assertEquals(101, $result->getStatusCode()); - } - - public function testDeleteAdminGroup() { - $this->groupManager - ->method('groupExists') - ->with('admin') - ->willReturn('true'); - - $result = $this->api->deleteGroup([ - 'groupid' => 'admin' - ]); - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertFalse($result->succeeded()); - $this->assertEquals(102, $result->getStatusCode()); - } - - public function testDeleteGroup() { - $this->groupManager - ->method('groupExists') - ->with('ExistingGroup') - ->willReturn('true'); - - $group = $this->createGroup('ExistingGroup'); - $this->groupManager - ->method('get') - ->with('ExistingGroup') - ->willReturn($group); - $group - ->expects($this->once()) - ->method('delete') - ->willReturn(true); - - $result = $this->api->deleteGroup([ - 'groupid' => 'ExistingGroup', - ]); - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertTrue($result->succeeded()); - } -} -- cgit v1.2.3 From 8f4adebab7cf3133bb33b8081fbdf2c6e2e8e549 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Thu, 11 Aug 2016 19:49:45 +0200 Subject: Move Users to OCSController --- apps/provisioning_api/appinfo/routes.php | 38 +- .../lib/Controller/UsersController.php | 632 +++++ apps/provisioning_api/lib/Users.php | 631 ----- .../tests/Controller/UsersControllerTest.php | 2296 ++++++++++++++++++ apps/provisioning_api/tests/UsersTest.php | 2441 -------------------- 5 files changed, 2944 insertions(+), 3094 deletions(-) create mode 100644 apps/provisioning_api/lib/Controller/UsersController.php delete mode 100644 apps/provisioning_api/lib/Users.php create mode 100644 apps/provisioning_api/tests/Controller/UsersControllerTest.php delete mode 100644 apps/provisioning_api/tests/UsersTest.php (limited to 'apps') diff --git a/apps/provisioning_api/appinfo/routes.php b/apps/provisioning_api/appinfo/routes.php index dbc0321efb2..473ae2ff716 100644 --- a/apps/provisioning_api/appinfo/routes.php +++ b/apps/provisioning_api/appinfo/routes.php @@ -39,31 +39,25 @@ $app->registerRoutes($this, [ ['root' => '/cloud', 'name' => 'Groups#addGroup', 'url' => '/groups', 'verb' => 'POST'], ['root' => '/cloud', 'name' => 'Groups#deleteGroup', 'url' => '/groups/{groupId}', 'verb' => 'DELETE'], ['root' => '/cloud', 'name' => 'Groups#getSubAdminsOfGroup', 'url' => '/groups/{groupId}/subadmins', 'verb' => 'GET'], + + //Users + ['root' => '/cloud', 'name' => 'Users#getUsers', 'url' => '/users', 'verb' => 'GET'], + ['root' => '/cloud', 'name' => 'Users#addUser', 'url' => '/users', 'verb' => 'POST'], + ['root' => '/cloud', 'name' => 'Users#getUser', 'url' => '/users/{userId}', 'verb' => 'GET'], + ['root' => '/cloud', 'name' => 'Users#editUser', 'url' => '/users/{userId}', 'verb' => 'PUT'], + ['root' => '/cloud', 'name' => 'Users#deleteUser', 'url' => '/users/{userId}', 'verb' => 'DELETE'], + ['root' => '/cloud', 'name' => 'Users#enableUser', 'url' => '/users/{userId}/enable', 'verb' => 'PUT'], + ['root' => '/cloud', 'name' => 'Users#disableUser', 'url' => '/users/{userId}/disable', 'verb' => 'PUT'], + ['root' => '/cloud', 'name' => 'Users#getUsersGroups', 'url' => '/users/{userId}/groups', 'verb' => 'GET'], + ['root' => '/cloud', 'name' => 'Users#addToGroup', 'url' => '/users/{userId}/groups', 'verb' => 'POST'], + ['root' => '/cloud', 'name' => 'Users#removeFromGroup', 'url' => '/users/{userId}/groups', 'verb' => 'DELETE'], + ['root' => '/cloud', 'name' => 'Users#getUserSubAdminGroups', 'url' => '/users/{userId}/subadmins', 'verb' => 'GET'], + ['root' => '/cloud', 'name' => 'Users#addSubAdmin', 'url' => '/users/{userId}/subadmins', 'verb' => 'POST'], + ['root' => '/cloud', 'name' => 'Users#removeSubAdmin', 'url' => '/users/{userId}/subadmins', 'verb' => 'DELETE'], + ], ]); -// Users -$users = new Users( - \OC::$server->getUserManager(), - \OC::$server->getConfig(), - \OC::$server->getGroupManager(), - \OC::$server->getUserSession(), - \OC::$server->getLogger() -); -API::register('get', '/cloud/users', [$users, 'getUsers'], 'provisioning_api', API::SUBADMIN_AUTH); -API::register('post', '/cloud/users', [$users, 'addUser'], 'provisioning_api', API::SUBADMIN_AUTH); -API::register('get', '/cloud/users/{userid}', [$users, 'getUser'], 'provisioning_api', API::USER_AUTH); -API::register('put', '/cloud/users/{userid}', [$users, 'editUser'], 'provisioning_api', API::USER_AUTH); -API::register('delete', '/cloud/users/{userid}', [$users, 'deleteUser'], 'provisioning_api', API::SUBADMIN_AUTH); -API::register('put', '/cloud/users/{userid}/enable', [$users, 'enableUser'], 'provisioning_api', API::SUBADMIN_AUTH); -API::register('put', '/cloud/users/{userid}/disable', [$users, 'disableUser'], 'provisioning_api', API::SUBADMIN_AUTH); -API::register('get', '/cloud/users/{userid}/groups', [$users, 'getUsersGroups'], 'provisioning_api', API::USER_AUTH); -API::register('post', '/cloud/users/{userid}/groups', [$users, 'addToGroup'], 'provisioning_api', API::SUBADMIN_AUTH); -API::register('delete', '/cloud/users/{userid}/groups', [$users, 'removeFromGroup'], 'provisioning_api', API::SUBADMIN_AUTH); -API::register('post', '/cloud/users/{userid}/subadmins', [$users, 'addSubAdmin'], 'provisioning_api', API::ADMIN_AUTH); -API::register('delete', '/cloud/users/{userid}/subadmins', [$users, 'removeSubAdmin'], 'provisioning_api', API::ADMIN_AUTH); -API::register('get', '/cloud/users/{userid}/subadmins', [$users, 'getUserSubAdminGroups'], 'provisioning_api', API::ADMIN_AUTH); - // Apps $apps = new Apps( \OC::$server->getAppManager(), diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php new file mode 100644 index 00000000000..29d449e97d6 --- /dev/null +++ b/apps/provisioning_api/lib/Controller/UsersController.php @@ -0,0 +1,632 @@ + + * @author Joas Schilling + * @author Lukas Reschke + * @author michag86 + * @author Morris Jobke + * @author Roeland Jago Douma + * @author Thomas Müller + * @author Tom Needham + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Provisioning_API\Controller; + +use \OC_Helper; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSException; +use OCP\AppFramework\OCS\OCSForbiddenException; +use OCP\AppFramework\OCS\OCSNotFoundException; +use OCP\AppFramework\OCSController; +use OCP\Files\NotFoundException; +use OCP\IConfig; +use OCP\IGroupManager; +use OCP\ILogger; +use OCP\IRequest; +use OCP\IUserManager; +use OCP\IUserSession; + +class UsersController extends OCSController { + + /** @var IUserManager */ + private $userManager; + /** @var IConfig */ + private $config; + /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface + private $groupManager; + /** @var IUserSession */ + private $userSession; + /** @var ILogger */ + private $logger; + + /** + * @param string $appName + * @param IRequest $request + * @param IUserManager $userManager + * @param IConfig $config + * @param IGroupManager $groupManager + * @param IUserSession $userSession + * @param ILogger $logger + */ + public function __construct($appName, + IRequest $request, + IUserManager $userManager, + IConfig $config, + IGroupManager $groupManager, + IUserSession $userSession, + ILogger $logger) { + parent::__construct($appName, $request); + + $this->userManager = $userManager; + $this->config = $config; + $this->groupManager = $groupManager; + $this->userSession = $userSession; + $this->logger = $logger; + } + + /** + * @NoAdminRequired + * + * returns a list of users + * + * @param string $search + * @param int $limit + * @param int $offset + * @return DataResponse + */ + public function getUsers($search = '', $limit = null, $offset = null) { + $user = $this->userSession->getUser(); + + // Admin? Or SubAdmin? + $uid = $user->getUID(); + $subAdminManager = $this->groupManager->getSubAdmin(); + if($this->groupManager->isAdmin($uid)){ + $users = $this->userManager->search($search, $limit, $offset); + } else if ($subAdminManager->isSubAdmin($user)) { + $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); + foreach ($subAdminOfGroups as $key => $group) { + $subAdminOfGroups[$key] = $group->getGID(); + } + + if($offset === null) { + $offset = 0; + } + + $users = []; + foreach ($subAdminOfGroups as $group) { + $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search)); + } + + $users = array_slice($users, $offset, $limit); + } + + $users = array_keys($users); + + return new DataResponse([ + 'users' => $users + ]); + } + + /** + * @NoAdminRequired + * + * @param string $userid + * @param string $password + * @param array $groups + * @return DataResponse + * @throws OCSException + */ + public function addUser($userid, $password, $groups = null) { + $user = $this->userSession->getUser(); + $isAdmin = $this->groupManager->isAdmin($user->getUID()); + $subAdminManager = $this->groupManager->getSubAdmin(); + + if($this->userManager->userExists($userid)) { + $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); + throw new OCSException('User already exists', 102); + } + + if(is_array($groups)) { + foreach ($groups as $group) { + if(!$this->groupManager->groupExists($group)) { + throw new OCSException('group '.$group.' does not exist', 104); + } + if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) { + throw new OCSException('insufficient privileges for group '. $group, 105); + } + } + } else { + if(!$isAdmin) { + throw new OCSException('no group specified (required for subadmins)', 106); + } + } + + try { + $newUser = $this->userManager->createUser($userid, $password); + $this->logger->info('Successful addUser call with userid: '.$userid, ['app' => 'ocs_api']); + + if (is_array($groups)) { + foreach ($groups as $group) { + $this->groupManager->get($group)->addUser($newUser); + $this->logger->info('Added userid '.$userid.' to group '.$group, ['app' => 'ocs_api']); + } + } + return new DataResponse(); + } catch (\Exception $e) { + $this->logger->error('Failed addUser attempt with exception: '.$e->getMessage(), ['app' => 'ocs_api']); + throw new OCSException('Bad request', 101); + } + } + + /** + * @NoAdminRequired + * @NoSubAdminRequired + * + * gets user info + * + * @param string $userId + * @return DataResponse + * @throws OCSException + */ + public function getUser($userId) { + $currentLoggedInUser = $this->userSession->getUser(); + + $data = []; + + // Check if the target user exists + $targetUserObject = $this->userManager->get($userId); + if($targetUserObject === null) { + throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND); + } + + // Admin? Or SubAdmin? + if($this->groupManager->isAdmin($currentLoggedInUser->getUID()) + || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { + $data['enabled'] = $this->config->getUserValue($userId, 'core', 'enabled', 'true'); + } else { + // Check they are looking up themselves + if($currentLoggedInUser->getUID() !== $userId) { + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); + } + } + + // Find the data + $data['quota'] = $this->fillStorageInfo($userId); + $data['email'] = $targetUserObject->getEMailAddress(); + $data['displayname'] = $targetUserObject->getDisplayName(); + + return new DataResponse($data); + } + + /** + * @NoAdminRequired + * @NoSubAdminRequired + * + * edit users + * + * @param string $userId + * @param string $key + * @param string $value + * @return DataResponse + * @throws OCSException + * @throws OCSForbiddenException + */ + public function editUser($userId, $key, $value) { + $currentLoggedInUser = $this->userSession->getUser(); + + $targetUser = $this->userManager->get($userId); + if($targetUser === null) { + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); + } + + $permittedFields = []; + if($userId === $currentLoggedInUser->getUID()) { + // Editing self (display, email) + $permittedFields[] = 'display'; + $permittedFields[] = 'email'; + $permittedFields[] = 'password'; + // If admin they can edit their own quota + if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { + $permittedFields[] = 'quota'; + } + } else { + // Check if admin / subadmin + $subAdminManager = $this->groupManager->getSubAdmin(); + if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) + || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { + // They have permissions over the user + $permittedFields[] = 'display'; + $permittedFields[] = 'quota'; + $permittedFields[] = 'password'; + $permittedFields[] = 'email'; + } else { + // No rights + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); + } + } + // Check if permitted to edit this field + if(!in_array($key, $permittedFields)) { + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); + } + // Process the edit + switch($key) { + case 'display': + $targetUser->setDisplayName($value); + break; + case 'quota': + $quota = $value; + if($quota !== 'none' and $quota !== 'default') { + if (is_numeric($quota)) { + $quota = floatval($quota); + } else { + $quota = \OCP\Util::computerFileSize($quota); + } + if ($quota === false) { + throw new OCSException('Invalid quota value '.$value, 103); + } + if($quota === 0) { + $quota = 'default'; + }else if($quota === -1) { + $quota = 'none'; + } else { + $quota = \OCP\Util::humanFileSize($quota); + } + } + $targetUser->setQuota($quota); + break; + case 'password': + $targetUser->setPassword($value); + break; + case 'email': + if(filter_var($value, FILTER_VALIDATE_EMAIL)) { + $targetUser->setEMailAddress($value); + } else { + throw new OCSException('', 102); + } + break; + default: + throw new OCSException('', 103); + } + return new DataResponse(); + } + + /** + * @NoAdminRequired + * + * @param string $userId + * @return DataResponse + * @throws OCSException + * @throws OCSForbiddenException + */ + public function deleteUser($userId) { + $currentLoggedInUser = $this->userSession->getUser(); + + $targetUser = $this->userManager->get($userId); + + if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { + throw new OCSException('', 101); + } + + // If not permitted + $subAdminManager = $this->groupManager->getSubAdmin(); + if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); + } + + // Go ahead with the delete + if($targetUser->delete()) { + return new DataResponse(); + } else { + throw new OCSException('', 101); + } + } + + /** + * @NoAdminRequired + * + * @param string $userId + * @return DataResponse + */ + public function disableUser($userId) { + return $this->setEnabled($userId, false); + } + + /** + * @NoAdminRequired + * + * @param string $userId + * @return DataResponse + */ + public function enableUser($userId) { + return $this->setEnabled($userId, true); + } + + /** + * @param string $userId + * @param bool $value + * @return DataResponse + * @throws OCSException + * @throws OCSForbiddenException + */ + private function setEnabled($userId, $value) { + $currentLoggedInUser = $this->userSession->getUser(); + + $targetUser = $this->userManager->get($userId); + if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { + throw new OCSException('', 101); + } + + // If not permitted + $subAdminManager = $this->groupManager->getSubAdmin(); + if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); + } + + // enable/disable the user now + $targetUser->setEnabled($value); + return new DataResponse(); + } + + /** + * @NoAdminRequired + * @NoSubAdminRequired + * + * @param string $userId + * @return DataResponse + * @throws OCSForbiddenException + * @throws OCSNotFoundException + */ + public function getUsersGroups($userId) { + $loggedInUser = $this->userSession->getUser(); + + $targetUser = $this->userManager->get($userId); + if($targetUser === null) { + throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); + } + + if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { + // Self lookup or admin lookup + return new DataResponse([ + 'groups' => $this->groupManager->getUserGroupIds($targetUser) + ]); + } else { + $subAdminManager = $this->groupManager->getSubAdmin(); + + // Looking up someone else + if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { + // Return the group that the method caller is subadmin of for the user in question + $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); + foreach ($getSubAdminsGroups as $key => $group) { + $getSubAdminsGroups[$key] = $group->getGID(); + } + $groups = array_intersect( + $getSubAdminsGroups, + $this->groupManager->getUserGroupIds($targetUser) + ); + return new DataResponse(['groups' => $groups]); + } else { + // Not permitted + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); + } + } + + } + + /** + * @param string $userId + * @param string $groupid + * @return DataResponse + * @throws OCSException + */ + public function addToGroup($userId, $groupid = '') { + if($groupid === '') { + throw new OCSException('', 101); + } + + $group = $this->groupManager->get($groupid); + $targetUser = $this->userManager->get($userId); + if($group === null) { + throw new OCSException('', 102); + } + if($targetUser === null) { + throw new OCSException('', 103); + } + + // Add user to group + $group->addUser($targetUser); + return new DataResponse(); + } + + /** + * @NoAdminRequired + * + * @param string userId + * @param string $groupid + * @return DataResponse + * @throws OCSException + */ + public function removeFromGroup($userId, $groupid) { + $loggedInUser = $this->userSession->getUser(); + + if($groupid === null) { + throw new OCSException('', 101); + } + + $group = $this->groupManager->get($groupid); + if($group === null) { + throw new OCSException('', 102); + } + + $targetUser = $this->userManager->get($userId); + if($targetUser === null) { + throw new OCSException('', 103); + } + + // If they're not an admin, check they are a subadmin of the group in question + $subAdminManager = $this->groupManager->getSubAdmin(); + if(!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminofGroup($loggedInUser, $group)) { + throw new OCSException('', 104); + } + // Check they aren't removing themselves from 'admin' or their 'subadmin; group + if($userId === $loggedInUser->getUID()) { + if($this->groupManager->isAdmin($loggedInUser->getUID())) { + if($group->getGID() === 'admin') { + throw new OCSException('Cannot remove yourself from the admin group', 105); + } + } else { + // Not an admin, check they are not removing themself from their subadmin group + $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); + foreach ($subAdminGroups as $key => $group) { + $subAdminGroups[$key] = $group->getGID(); + } + + if(in_array($group->getGID(), $subAdminGroups, true)) { + throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105); + } + } + } + + // Remove user from group + $group->removeUser($targetUser); + return new DataResponse(); + } + + /** + * Creates a subadmin + * + * @param string $userId + * @param string $groupid + * @return DataResponse + * @throws OCSException + */ + public function addSubAdmin($userId, $groupid) { + $group = $this->groupManager->get($groupid); + $user = $this->userManager->get($userId); + + // Check if the user exists + if($user === null) { + throw new OCSException('User does not exist', 101); + } + // Check if group exists + if($group === null) { + throw new OCSException('Group:'.$groupid.' does not exist', 102); + } + // Check if trying to make subadmin of admin group + if(strtolower($groupid) === 'admin') { + throw new OCSException('Cannot create subadmins for admin group', 103); + } + + $subAdminManager = $this->groupManager->getSubAdmin(); + + // We cannot be subadmin twice + if ($subAdminManager->isSubAdminofGroup($user, $group)) { + return new DataResponse(); + } + // Go + if($subAdminManager->createSubAdmin($user, $group)) { + return new DataResponse(); + } else { + throw new OCSException('Unknown error occurred', 103); + } + } + + /** + * Removes a subadmin from a group + * + * @param string $userId + * @param string $groupid + * @return DataResponse + * @throws OCSException + */ + public function removeSubAdmin($userId, $groupid) { + $group = $this->groupManager->get($groupid); + $user = $this->userManager->get($userId); + $subAdminManager = $this->groupManager->getSubAdmin(); + + // Check if the user exists + if($user === null) { + throw new OCSException('User does not exist', 101); + } + // Check if the group exists + if($group === null) { + throw new OCSException('Group does not exist', 101); + } + // Check if they are a subadmin of this said group + if(!$subAdminManager->isSubAdminofGroup($user, $group)) { + throw new OCSException('User is not a subadmin of this group', 102); + } + + // Go + if($subAdminManager->deleteSubAdmin($user, $group)) { + return new DataResponse(); + } else { + throw new OCSException('Unknown error occurred', 103); + } + } + + /** + * Get the groups a user is a subadmin of + * + * @param string $userId + * @return DataResponse + * @throws OCSException + */ + public function getUserSubAdminGroups($userId) { + $user = $this->userManager->get($userId); + // Check if the user exists + if($user === null) { + throw new OCSException('User does not exist', 101); + } + + // Get the subadmin groups + $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user); + foreach ($groups as $key => $group) { + $groups[$key] = $group->getGID(); + } + + if(!$groups) { + throw new OCSException('Unknown error occurred', 102); + } else { + return new DataResponse($groups); + } + } + + /** + * @param string $userId + * @return array + * @throws \OCP\Files\NotFoundException + */ + protected function fillStorageInfo($userId) { + try { + \OC_Util::tearDownFS(); + \OC_Util::setupFS($userId); + $storage = OC_Helper::getStorageInfo('/'); + $data = [ + 'free' => $storage['free'], + 'used' => $storage['used'], + 'total' => $storage['total'], + 'relative' => $storage['relative'], + ]; + } catch (NotFoundException $ex) { + $data = []; + } + return $data; + } +} diff --git a/apps/provisioning_api/lib/Users.php b/apps/provisioning_api/lib/Users.php deleted file mode 100644 index 0e5a8043e8a..00000000000 --- a/apps/provisioning_api/lib/Users.php +++ /dev/null @@ -1,631 +0,0 @@ - - * @author Joas Schilling - * @author Lukas Reschke - * @author michag86 - * @author Morris Jobke - * @author Roeland Jago Douma - * @author Thomas Müller - * @author Tom Needham - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Provisioning_API; - -use \OC_Helper; -use OCP\Files\NotFoundException; -use OCP\IConfig; -use OCP\IGroupManager; -use OCP\ILogger; -use OCP\IUserManager; -use OCP\IUserSession; - -class Users { - - /** @var IUserManager */ - private $userManager; - /** @var IConfig */ - private $config; - /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface - private $groupManager; - /** @var IUserSession */ - private $userSession; - /** @var ILogger */ - private $logger; - - /** - * @param IUserManager $userManager - * @param IConfig $config - * @param IGroupManager $groupManager - * @param IUserSession $userSession - * @param ILogger $logger - */ - public function __construct(IUserManager $userManager, - IConfig $config, - IGroupManager $groupManager, - IUserSession $userSession, - ILogger $logger) { - $this->userManager = $userManager; - $this->config = $config; - $this->groupManager = $groupManager; - $this->userSession = $userSession; - $this->logger = $logger; - } - - /** - * returns a list of users - * - * @return \OC\OCS\Result - */ - public function getUsers() { - $search = !empty($_GET['search']) ? $_GET['search'] : ''; - $limit = !empty($_GET['limit']) ? $_GET['limit'] : null; - $offset = !empty($_GET['offset']) ? $_GET['offset'] : null; - - // Check if user is logged in - $user = $this->userSession->getUser(); - if ($user === null) { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED); - } - - // Admin? Or SubAdmin? - $uid = $user->getUID(); - $subAdminManager = $this->groupManager->getSubAdmin(); - if($this->groupManager->isAdmin($uid)){ - $users = $this->userManager->search($search, $limit, $offset); - } else if ($subAdminManager->isSubAdmin($user)) { - $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); - foreach ($subAdminOfGroups as $key => $group) { - $subAdminOfGroups[$key] = $group->getGID(); - } - - if($offset === null) { - $offset = 0; - } - - $users = []; - foreach ($subAdminOfGroups as $group) { - $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search)); - } - - $users = array_slice($users, $offset, $limit); - } else { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED); - } - $users = array_keys($users); - - return new \OC\OCS\Result([ - 'users' => $users - ]); - } - - /** - * @return \OC\OCS\Result - */ - public function addUser() { - $userId = isset($_POST['userid']) ? $_POST['userid'] : null; - $password = isset($_POST['password']) ? $_POST['password'] : null; - $groups = isset($_POST['groups']) ? $_POST['groups'] : null; - $user = $this->userSession->getUser(); - $isAdmin = $this->groupManager->isAdmin($user->getUID()); - $subAdminManager = $this->groupManager->getSubAdmin(); - - if (!$isAdmin && !$subAdminManager->isSubAdmin($user)) { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED); - } - - if($this->userManager->userExists($userId)) { - $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); - return new \OC\OCS\Result(null, 102, 'User already exists'); - } - - if(is_array($groups)) { - foreach ($groups as $group) { - if(!$this->groupManager->groupExists($group)){ - return new \OC\OCS\Result(null, 104, 'group '.$group.' does not exist'); - } - if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) { - return new \OC\OCS\Result(null, 105, 'insufficient privileges for group '. $group); - } - } - } else { - if(!$isAdmin) { - return new \OC\OCS\Result(null, 106, 'no group specified (required for subadmins)'); - } - } - - try { - $newUser = $this->userManager->createUser($userId, $password); - $this->logger->info('Successful addUser call with userid: '.$userId, ['app' => 'ocs_api']); - - if (is_array($groups)) { - foreach ($groups as $group) { - $this->groupManager->get($group)->addUser($newUser); - $this->logger->info('Added userid '.$userId.' to group '.$group, ['app' => 'ocs_api']); - } - } - return new \OC\OCS\Result(null, 100); - } catch (\Exception $e) { - $this->logger->error('Failed addUser attempt with exception: '.$e->getMessage(), ['app' => 'ocs_api']); - return new \OC\OCS\Result(null, 101, 'Bad request'); - } - } - - /** - * gets user info - * - * @param array $parameters - * @return \OC\OCS\Result - */ - public function getUser($parameters) { - $userId = $parameters['userid']; - - // Check if user is logged in - $currentLoggedInUser = $this->userSession->getUser(); - if ($currentLoggedInUser === null) { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED); - } - - $data = []; - - // Check if the target user exists - $targetUserObject = $this->userManager->get($userId); - if($targetUserObject === null) { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The requested user could not be found'); - } - - // Admin? Or SubAdmin? - if($this->groupManager->isAdmin($currentLoggedInUser->getUID()) - || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { - $data['enabled'] = $this->config->getUserValue($userId, 'core', 'enabled', 'true'); - } else { - // Check they are looking up themselves - if($currentLoggedInUser->getUID() !== $userId) { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED); - } - } - - // Find the data - $data['quota'] = $this->fillStorageInfo($userId); - $data['email'] = $targetUserObject->getEMailAddress(); - $data['displayname'] = $targetUserObject->getDisplayName(); - - return new \OC\OCS\Result($data); - } - - /** - * edit users - * - * @param array $parameters - * @return \OC\OCS\Result - */ - public function editUser($parameters) { - /** @var string $targetUserId */ - $targetUserId = $parameters['userid']; - - // Check if user is logged in - $currentLoggedInUser = $this->userSession->getUser(); - if ($currentLoggedInUser === null) { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED); - } - - $targetUser = $this->userManager->get($targetUserId); - if($targetUser === null) { - return new \OC\OCS\Result(null, 997); - } - - $permittedFields = []; - if($targetUserId === $currentLoggedInUser->getUID()) { - // Editing self (display, email) - $permittedFields[] = 'display'; - $permittedFields[] = 'email'; - $permittedFields[] = 'password'; - // If admin they can edit their own quota - if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { - $permittedFields[] = 'quota'; - } - } else { - // Check if admin / subadmin - $subAdminManager = $this->groupManager->getSubAdmin(); - if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) - || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { - // They have permissions over the user - $permittedFields[] = 'display'; - $permittedFields[] = 'quota'; - $permittedFields[] = 'password'; - $permittedFields[] = 'email'; - } else { - // No rights - return new \OC\OCS\Result(null, 997); - } - } - // Check if permitted to edit this field - if(!in_array($parameters['_put']['key'], $permittedFields)) { - return new \OC\OCS\Result(null, 997); - } - // Process the edit - switch($parameters['_put']['key']) { - case 'display': - $targetUser->setDisplayName($parameters['_put']['value']); - break; - case 'quota': - $quota = $parameters['_put']['value']; - if($quota !== 'none' and $quota !== 'default') { - if (is_numeric($quota)) { - $quota = floatval($quota); - } else { - $quota = \OCP\Util::computerFileSize($quota); - } - if ($quota === false) { - return new \OC\OCS\Result(null, 103, "Invalid quota value {$parameters['_put']['value']}"); - } - if($quota === 0) { - $quota = 'default'; - }else if($quota === -1) { - $quota = 'none'; - } else { - $quota = \OCP\Util::humanFileSize($quota); - } - } - $targetUser->setQuota($quota); - break; - case 'password': - $targetUser->setPassword($parameters['_put']['value']); - break; - case 'email': - if(filter_var($parameters['_put']['value'], FILTER_VALIDATE_EMAIL)) { - $targetUser->setEMailAddress($parameters['_put']['value']); - } else { - return new \OC\OCS\Result(null, 102); - } - break; - default: - return new \OC\OCS\Result(null, 103); - } - return new \OC\OCS\Result(null, 100); - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function deleteUser($parameters) { - // Check if user is logged in - $currentLoggedInUser = $this->userSession->getUser(); - if ($currentLoggedInUser === null) { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED); - } - - $targetUser = $this->userManager->get($parameters['userid']); - - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { - return new \OC\OCS\Result(null, 101); - } - - // If not permitted - $subAdminManager = $this->groupManager->getSubAdmin(); - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { - return new \OC\OCS\Result(null, 997); - } - - // Go ahead with the delete - if($targetUser->delete()) { - return new \OC\OCS\Result(null, 100); - } else { - return new \OC\OCS\Result(null, 101); - } - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function disableUser($parameters) { - return $this->setEnabled($parameters, false); - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function enableUser($parameters) { - return $this->setEnabled($parameters, true); - } - - /** - * @param array $parameters - * @param bool $value - * @return \OC\OCS\Result - */ - private function setEnabled($parameters, $value) { - // Check if user is logged in - $currentLoggedInUser = $this->userSession->getUser(); - if ($currentLoggedInUser === null) { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED); - } - - $targetUser = $this->userManager->get($parameters['userid']); - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { - return new \OC\OCS\Result(null, 101); - } - - // If not permitted - $subAdminManager = $this->groupManager->getSubAdmin(); - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { - return new \OC\OCS\Result(null, 997); - } - - // enable/disable the user now - $targetUser->setEnabled($value); - return new \OC\OCS\Result(null, 100); - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function getUsersGroups($parameters) { - // Check if user is logged in - $loggedInUser = $this->userSession->getUser(); - if ($loggedInUser === null) { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED); - } - - $targetUser = $this->userManager->get($parameters['userid']); - if($targetUser === null) { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND); - } - - if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { - // Self lookup or admin lookup - return new \OC\OCS\Result([ - 'groups' => $this->groupManager->getUserGroupIds($targetUser) - ]); - } else { - $subAdminManager = $this->groupManager->getSubAdmin(); - - // Looking up someone else - if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { - // Return the group that the method caller is subadmin of for the user in question - $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); - foreach ($getSubAdminsGroups as $key => $group) { - $getSubAdminsGroups[$key] = $group->getGID(); - } - $groups = array_intersect( - $getSubAdminsGroups, - $this->groupManager->getUserGroupIds($targetUser) - ); - return new \OC\OCS\Result(array('groups' => $groups)); - } else { - // Not permitted - return new \OC\OCS\Result(null, 997); - } - } - - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function addToGroup($parameters) { - // Check if user is logged in - $user = $this->userSession->getUser(); - if ($user === null) { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED); - } - - // Check they're an admin - if(!$this->groupManager->isAdmin($user->getUID())) { - // This user doesn't have rights to add a user to this group - return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED); - } - - $groupId = !empty($_POST['groupid']) ? $_POST['groupid'] : null; - if($groupId === null) { - return new \OC\OCS\Result(null, 101); - } - - $group = $this->groupManager->get($groupId); - $targetUser = $this->userManager->get($parameters['userid']); - if($group === null) { - return new \OC\OCS\Result(null, 102); - } - if($targetUser === null) { - return new \OC\OCS\Result(null, 103); - } - - // Add user to group - $group->addUser($targetUser); - return new \OC\OCS\Result(null, 100); - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function removeFromGroup($parameters) { - // Check if user is logged in - $loggedInUser = $this->userSession->getUser(); - if ($loggedInUser === null) { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_UNAUTHORISED); - } - - $group = !empty($parameters['_delete']['groupid']) ? $parameters['_delete']['groupid'] : null; - if($group === null) { - return new \OC\OCS\Result(null, 101); - } - - $group = $this->groupManager->get($group); - if($group === null) { - return new \OC\OCS\Result(null, 102); - } - - $targetUser = $this->userManager->get($parameters['userid']); - if($targetUser === null) { - return new \OC\OCS\Result(null, 103); - } - - // If they're not an admin, check they are a subadmin of the group in question - $subAdminManager = $this->groupManager->getSubAdmin(); - if(!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminofGroup($loggedInUser, $group)) { - return new \OC\OCS\Result(null, 104); - } - // Check they aren't removing themselves from 'admin' or their 'subadmin; group - if($parameters['userid'] === $loggedInUser->getUID()) { - if($this->groupManager->isAdmin($loggedInUser->getUID())) { - if($group->getGID() === 'admin') { - return new \OC\OCS\Result(null, 105, 'Cannot remove yourself from the admin group'); - } - } else { - // Not an admin, check they are not removing themself from their subadmin group - $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); - foreach ($subAdminGroups as $key => $group) { - $subAdminGroups[$key] = $group->getGID(); - } - - if(in_array($group->getGID(), $subAdminGroups, true)) { - return new \OC\OCS\Result(null, 105, 'Cannot remove yourself from this group as you are a SubAdmin'); - } - } - } - - // Remove user from group - $group->removeUser($targetUser); - return new \OC\OCS\Result(null, 100); - } - - /** - * Creates a subadmin - * - * @param array $parameters - * @return \OC\OCS\Result - */ - public function addSubAdmin($parameters) { - $group = $this->groupManager->get($_POST['groupid']); - $user = $this->userManager->get($parameters['userid']); - - // Check if the user exists - if($user === null) { - return new \OC\OCS\Result(null, 101, 'User does not exist'); - } - // Check if group exists - if($group === null) { - return new \OC\OCS\Result(null, 102, 'Group:'.$_POST['groupid'].' does not exist'); - } - // Check if trying to make subadmin of admin group - if(strtolower($_POST['groupid']) === 'admin') { - return new \OC\OCS\Result(null, 103, 'Cannot create subadmins for admin group'); - } - - $subAdminManager = $this->groupManager->getSubAdmin(); - - // We cannot be subadmin twice - if ($subAdminManager->isSubAdminofGroup($user, $group)) { - return new \OC\OCS\Result(null, 100); - } - // Go - if($subAdminManager->createSubAdmin($user, $group)) { - return new \OC\OCS\Result(null, 100); - } else { - return new \OC\OCS\Result(null, 103, 'Unknown error occurred'); - } - } - - /** - * Removes a subadmin from a group - * - * @param array $parameters - * @return \OC\OCS\Result - */ - public function removeSubAdmin($parameters) { - $group = $this->groupManager->get($parameters['_delete']['groupid']); - $user = $this->userManager->get($parameters['userid']); - $subAdminManager = $this->groupManager->getSubAdmin(); - - // Check if the user exists - if($user === null) { - return new \OC\OCS\Result(null, 101, 'User does not exist'); - } - // Check if the group exists - if($group === null) { - return new \OC\OCS\Result(null, 101, 'Group does not exist'); - } - // Check if they are a subadmin of this said group - if(!$subAdminManager->isSubAdminofGroup($user, $group)) { - return new \OC\OCS\Result(null, 102, 'User is not a subadmin of this group'); - } - - // Go - if($subAdminManager->deleteSubAdmin($user, $group)) { - return new \OC\OCS\Result(null, 100); - } else { - return new \OC\OCS\Result(null, 103, 'Unknown error occurred'); - } - } - - /** - * Get the groups a user is a subadmin of - * - * @param array $parameters - * @return \OC\OCS\Result - */ - public function getUserSubAdminGroups($parameters) { - $user = $this->userManager->get($parameters['userid']); - // Check if the user exists - if($user === null) { - return new \OC\OCS\Result(null, 101, 'User does not exist'); - } - - // Get the subadmin groups - $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user); - foreach ($groups as $key => $group) { - $groups[$key] = $group->getGID(); - } - - if(!$groups) { - return new \OC\OCS\Result(null, 102, 'Unknown error occurred'); - } else { - return new \OC\OCS\Result($groups); - } - } - - /** - * @param string $userId - * @return array - * @throws \OCP\Files\NotFoundException - */ - protected function fillStorageInfo($userId) { - try { - \OC_Util::tearDownFS(); - \OC_Util::setupFS($userId); - $storage = OC_Helper::getStorageInfo('/'); - $data = [ - 'free' => $storage['free'], - 'used' => $storage['used'], - 'total' => $storage['total'], - 'relative' => $storage['relative'], - ]; - } catch (NotFoundException $ex) { - $data = []; - } - return $data; - } -} diff --git a/apps/provisioning_api/tests/Controller/UsersControllerTest.php b/apps/provisioning_api/tests/Controller/UsersControllerTest.php new file mode 100644 index 00000000000..e04ee86feae --- /dev/null +++ b/apps/provisioning_api/tests/Controller/UsersControllerTest.php @@ -0,0 +1,2296 @@ + + * @author Joas Schilling + * @author Lukas Reschke + * @author michag86 + * @author Morris Jobke + * @author Roeland Jago Douma + * @author Thomas Müller + * @author Tom Needham + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Provisioning_API\Tests\Controller; + +use OCA\Provisioning_API\Controller\UsersController; +use OCP\IUserManager; +use OCP\IConfig; +use OCP\IUserSession; +use PHPUnit_Framework_MockObject_MockObject; +use Test\TestCase as OriginalTest; +use OCP\ILogger; + +class UsersControllerTest extends OriginalTest { + + /** @var IUserManager | PHPUnit_Framework_MockObject_MockObject */ + protected $userManager; + /** @var IConfig | PHPUnit_Framework_MockObject_MockObject */ + protected $config; + /** @var \OC\Group\Manager | PHPUnit_Framework_MockObject_MockObject */ + protected $groupManager; + /** @var IUserSession | PHPUnit_Framework_MockObject_MockObject */ + protected $userSession; + /** @var ILogger | PHPUnit_Framework_MockObject_MockObject */ + protected $logger; + /** @var UsersController | PHPUnit_Framework_MockObject_MockObject */ + protected $api; + + protected function tearDown() { + parent::tearDown(); + } + + protected function setUp() { + parent::setUp(); + + $this->userManager = $this->getMockBuilder('OCP\IUserManager') + ->disableOriginalConstructor() + ->getMock(); + $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->groupManager = $this->getMockBuilder('OC\Group\Manager') + ->disableOriginalConstructor() + ->getMock(); + $this->userSession = $this->getMockBuilder('OCP\IUserSession') + ->disableOriginalConstructor() + ->getMock(); + $this->logger = $this->getMockBuilder('OCP\ILogger') + ->disableOriginalConstructor() + ->getMock(); + $request = $this->getMockBuilder('OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController') + ->setConstructorArgs([ + 'provisioning_api', + $request, + $this->userManager, + $this->config, + $this->groupManager, + $this->userSession, + $this->logger, + ]) + ->setMethods(['fillStorageInfo']) + ->getMock(); + } + + public function testGetUsersAsAdmin() { + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('admin')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->will($this->returnValue(true)); + $this->userManager + ->expects($this->once()) + ->method('search') + ->with('MyCustomSearch', null, null) + ->will($this->returnValue(['Admin' => [], 'Foo' => [], 'Bar' => []])); + + $expected = ['users' => [ + 'Admin', + 'Foo', + 'Bar', + ], + ]; + $this->assertEquals($expected, $this->api->getUsers('MyCustomSearch')->getData()); + } + + public function testGetUsersAsSubAdmin() { + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('subadmin')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->will($this->returnValue(false)); + $firstGroup = $this->getMockBuilder('OCP\IGroup') + ->disableOriginalConstructor() + ->getMock(); + $firstGroup + ->expects($this->once()) + ->method('getGID') + ->will($this->returnValue('FirstGroup')); + $secondGroup = $this->getMockBuilder('OCP\IGroup') + ->disableOriginalConstructor() + ->getMock(); + $secondGroup + ->expects($this->once()) + ->method('getGID') + ->will($this->returnValue('SecondGroup')); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isSubAdmin') + ->with($loggedInUser) + ->will($this->returnValue(true)); + $subAdminManager + ->expects($this->once()) + ->method('getSubAdminsGroups') + ->with($loggedInUser) + ->will($this->returnValue([$firstGroup, $secondGroup])); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + $this->groupManager + ->expects($this->any()) + ->method('displayNamesInGroup') + ->will($this->onConsecutiveCalls(['AnotherUserInTheFirstGroup' => []], ['UserInTheSecondGroup' => []])); + + $expected = [ + 'users' => [ + 'AnotherUserInTheFirstGroup', + 'UserInTheSecondGroup', + ], + ]; + $this->assertEquals($expected, $this->api->getUsers('MyCustomSearch')->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 102 + */ + public function testAddUserAlreadyExisting() { + $this->userManager + ->expects($this->once()) + ->method('userExists') + ->with('AlreadyExistingUser') + ->will($this->returnValue(true)); + $this->logger + ->expects($this->once()) + ->method('error') + ->with('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('adminUser')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('adminUser') + ->willReturn(true); + + $this->api->addUser('AlreadyExistingUser', null, null); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 104 + * @expectedExceptionMessage group NonExistingGroup does not exist + */ + public function testAddUserNonExistingGroup() { + $this->userManager + ->expects($this->once()) + ->method('userExists') + ->with('NewUser') + ->willReturn(false); + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('adminUser')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('adminUser') + ->willReturn(true); + $this->groupManager + ->expects($this->once()) + ->method('groupExists') + ->with('NonExistingGroup') + ->willReturn(false); + + $this->api->addUser('NewUser', 'pass', ['NonExistingGroup']); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 104 + * @expectedExceptionMessage group NonExistingGroup does not exist + */ + public function testAddUserExistingGroupNonExistingGroup() { + $this->userManager + ->expects($this->once()) + ->method('userExists') + ->with('NewUser') + ->willReturn(false); + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('adminUser')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('adminUser') + ->willReturn(true); + $this->groupManager + ->expects($this->exactly(2)) + ->method('groupExists') + ->withConsecutive( + ['ExistingGroup'], + ['NonExistingGroup'] + ) + ->will($this->returnValueMap([ + ['ExistingGroup', true], + ['NonExistingGroup', false] + ])); + + $this->api->addUser('NewUser', 'pass', ['ExistingGroup', 'NonExistingGroup']); + } + + public function testAddUserSuccessful() { + $this->userManager + ->expects($this->once()) + ->method('userExists') + ->with('NewUser') + ->will($this->returnValue(false)); + $this->userManager + ->expects($this->once()) + ->method('createUser') + ->with('NewUser', 'PasswordOfTheNewUser'); + $this->logger + ->expects($this->once()) + ->method('info') + ->with('Successful addUser call with userid: NewUser', ['app' => 'ocs_api']); + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('adminUser')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('adminUser') + ->willReturn(true); + + $this->assertEquals([], $this->api->addUser('NewUser', 'PasswordOfTheNewUser')->getData()); + } + + public function testAddUserExistingGroup() { + $this->userManager + ->expects($this->once()) + ->method('userExists') + ->with('NewUser') + ->willReturn(false); + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('adminUser')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('adminUser') + ->willReturn(true); + $this->groupManager + ->expects($this->once()) + ->method('groupExists') + ->with('ExistingGroup') + ->willReturn(true); + $user = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $this->userManager + ->expects($this->once()) + ->method('createUser') + ->with('NewUser', 'PasswordOfTheNewUser') + ->willReturn($user); + $group = $this->getMockBuilder('OCP\IGroup') + ->disableOriginalConstructor() + ->getMock(); + $group + ->expects($this->once()) + ->method('addUser') + ->with($user); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('ExistingGroup') + ->willReturn($group); + $this->logger + ->expects($this->exactly(2)) + ->method('info') + ->withConsecutive( + ['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']], + ['Added userid NewUser to group ExistingGroup', ['app' => 'ocs_api']] + ); + + $this->assertEquals([], $this->api->addUser('NewUser', 'PasswordOfTheNewUser', ['ExistingGroup'])->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + * @expectedExceptionMessage Bad request + */ + public function testAddUserUnsuccessful() { + $this->userManager + ->expects($this->once()) + ->method('userExists') + ->with('NewUser') + ->will($this->returnValue(false)); + $this->userManager + ->expects($this->once()) + ->method('createUser') + ->with('NewUser', 'PasswordOfTheNewUser') + ->will($this->throwException(new \Exception('User backend not found.'))); + $this->logger + ->expects($this->once()) + ->method('error') + ->with('Failed addUser attempt with exception: User backend not found.', ['app' => 'ocs_api']); + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('adminUser')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('adminUser') + ->willReturn(true); + + $this->api->addUser('NewUser', 'PasswordOfTheNewUser'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 106 + * @expectedExceptionMessage no group specified (required for subadmins) + */ + public function testAddUserAsSubAdminNoGroup() { + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('regularUser')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('regularUser') + ->willReturn(false); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->with() + ->willReturn($subAdminManager); + + $this->api->addUser('NewUser', 'PasswordOfTheNewUser', null); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 105 + * @expectedExceptionMessage insufficient privileges for group ExistingGroup + */ + public function testAddUserAsSubAdminValidGroupNotSubAdmin() { + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('regularUser')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('regularUser') + ->willReturn(false); + $existingGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('ExistingGroup') + ->willReturn($existingGroup); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isSubAdminOfGroup') + ->with($loggedInUser, $existingGroup) + ->willReturn(false); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->with() + ->willReturn($subAdminManager); + $this->groupManager + ->expects($this->once()) + ->method('groupExists') + ->with('ExistingGroup') + ->willReturn(true); + + $this->api->addUser('NewUser', 'PasswordOfTheNewUser', ['ExistingGroup'])->getData(); + } + + public function testAddUserAsSubAdminExistingGroups() { + $this->userManager + ->expects($this->once()) + ->method('userExists') + ->with('NewUser') + ->willReturn(false); + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('subAdminUser')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('subAdminUser') + ->willReturn(false); + $this->groupManager + ->expects($this->exactly(2)) + ->method('groupExists') + ->withConsecutive( + ['ExistingGroup1'], + ['ExistingGroup2'] + ) + ->willReturn(true); + $user = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $this->userManager + ->expects($this->once()) + ->method('createUser') + ->with('NewUser', 'PasswordOfTheNewUser') + ->willReturn($user); + $existingGroup1 = $this->getMockBuilder('OCP\IGroup') + ->disableOriginalConstructor() + ->getMock(); + $existingGroup2 = $this->getMockBuilder('OCP\IGroup') + ->disableOriginalConstructor() + ->getMock(); + $existingGroup1 + ->expects($this->once()) + ->method('addUser') + ->with($user); + $existingGroup2 + ->expects($this->once()) + ->method('addUser') + ->with($user); + $this->groupManager + ->expects($this->exactly(4)) + ->method('get') + ->withConsecutive( + ['ExistingGroup1'], + ['ExistingGroup2'], + ['ExistingGroup1'], + ['ExistingGroup2'] + ) + ->will($this->returnValueMap([ + ['ExistingGroup1', $existingGroup1], + ['ExistingGroup2', $existingGroup2] + ])); + $this->logger + ->expects($this->exactly(3)) + ->method('info') + ->withConsecutive( + ['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']], + ['Added userid NewUser to group ExistingGroup1', ['app' => 'ocs_api']], + ['Added userid NewUser to group ExistingGroup2', ['app' => 'ocs_api']] + ); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->willReturn($subAdminManager); + $subAdminManager + ->expects($this->exactly(2)) + ->method('isSubAdminOfGroup') + ->withConsecutive( + [$loggedInUser, $existingGroup1], + [$loggedInUser, $existingGroup2] + ) + ->willReturn(true); + + $this->assertEquals([], $this->api->addUser('NewUser', 'PasswordOfTheNewUser', ['ExistingGroup1', 'ExistingGroup2'])->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 998 + * @expectedExceptionMessage The requested user could not be found + */ + public function testGetUserTargetDoesNotExist() { + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToGet') + ->will($this->returnValue(null)); + + $this->api->getUser('UserToGet'); + } + + public function testGetUserAsAdmin() { + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('admin')); + $targetUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $targetUser->expects($this->once()) + ->method('getEMailAddress') + ->willReturn('demo@owncloud.org'); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToGet') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('admin') + ->will($this->returnValue(true)); + $this->config + ->expects($this->at(0)) + ->method('getUserValue') + ->with('UserToGet', 'core', 'enabled', 'true') + ->will($this->returnValue('true')); + $this->api + ->expects($this->once()) + ->method('fillStorageInfo') + ->with('UserToGet') + ->will($this->returnValue(['DummyValue'])); + $targetUser + ->expects($this->once()) + ->method('getDisplayName') + ->will($this->returnValue('Demo User')); + + $expected = [ + 'enabled' => 'true', + 'quota' => ['DummyValue'], + 'email' => 'demo@owncloud.org', + 'displayname' => 'Demo User', + ]; + $this->assertEquals($expected, $this->api->getUser('UserToGet')->getData()); + } + + public function testGetUserAsSubAdminAndUserIsAccessible() { + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('subadmin')); + $targetUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $targetUser + ->expects($this->once()) + ->method('getEMailAddress') + ->willReturn('demo@owncloud.org'); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToGet') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('subadmin') + ->will($this->returnValue(false)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor() + ->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isUserAccessible') + ->with($loggedInUser, $targetUser) + ->will($this->returnValue(true)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + $this->config + ->expects($this->at(0)) + ->method('getUserValue') + ->with('UserToGet', 'core', 'enabled', 'true') + ->will($this->returnValue('true')); + $this->api + ->expects($this->once()) + ->method('fillStorageInfo') + ->with('UserToGet') + ->will($this->returnValue(['DummyValue'])); + $targetUser + ->expects($this->once()) + ->method('getDisplayName') + ->will($this->returnValue('Demo User')); + + $expected = [ + 'enabled' => 'true', + 'quota' => ['DummyValue'], + 'email' => 'demo@owncloud.org', + 'displayname' => 'Demo User', + ]; + $this->assertEquals($expected, $this->api->getUser('UserToGet')->getData()); + } + + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 997 + */ + public function testGetUserAsSubAdminAndUserIsNotAccessible() { + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->exactly(2)) + ->method('getUID') + ->will($this->returnValue('subadmin')); + $targetUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToGet') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('subadmin') + ->will($this->returnValue(false)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor() + ->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isUserAccessible') + ->with($loggedInUser, $targetUser) + ->will($this->returnValue(false)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + + $this->api->getUser('UserToGet'); + } + + public function testGetUserAsSubAdminSelfLookup() { + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->exactly(2)) + ->method('getUID') + ->will($this->returnValue('subadmin')); + $targetUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('subadmin') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('subadmin') + ->will($this->returnValue(false)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor() + ->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isUserAccessible') + ->with($loggedInUser, $targetUser) + ->will($this->returnValue(false)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + $this->api + ->expects($this->once()) + ->method('fillStorageInfo') + ->with('subadmin') + ->will($this->returnValue(['DummyValue'])); + $targetUser + ->expects($this->once()) + ->method('getDisplayName') + ->will($this->returnValue('Subadmin User')); + $targetUser + ->expects($this->once()) + ->method('getEMailAddress') + ->will($this->returnValue('subadmin@owncloud.org')); + + $expected = [ + 'quota' => ['DummyValue'], + 'email' => 'subadmin@owncloud.org', + 'displayname' => 'Subadmin User', + ]; + $this->assertEquals($expected, $this->api->getUser('subadmin')->getData()); + } + + public function testEditUserRegularUserSelfEditChangeDisplayName() { + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('UserToEdit')); + $targetUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToEdit') + ->will($this->returnValue($targetUser)); + $targetUser + ->expects($this->once()) + ->method('setDisplayName') + ->with('NewDisplayName'); + + $this->assertEquals([], $this->api->editUser('UserToEdit', 'display', 'NewDisplayName')->getData()); + } + + public function testEditUserRegularUserSelfEditChangeEmailValid() { + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('UserToEdit')); + $targetUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToEdit') + ->will($this->returnValue($targetUser)); + $targetUser + ->expects($this->once()) + ->method('setEMailAddress') + ->with('demo@owncloud.org'); + + $this->assertEquals([], $this->api->editUser('UserToEdit', 'email', 'demo@owncloud.org')->getData()); + } + + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 102 + */ + public function testEditUserRegularUserSelfEditChangeEmailInvalid() { + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('UserToEdit')); + $targetUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToEdit') + ->will($this->returnValue($targetUser)); + + $this->api->editUser('UserToEdit', 'email', 'demo.org'); + } + + public function testEditUserRegularUserSelfEditChangePassword() { + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('UserToEdit')); + $targetUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToEdit') + ->will($this->returnValue($targetUser)); + $targetUser + ->expects($this->once()) + ->method('setPassword') + ->with('NewPassword'); + + $this->assertEquals([], $this->api->editUser('UserToEdit', 'password', 'NewPassword')->getData()); + } + + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 997 + */ + public function testEditUserRegularUserSelfEditChangeQuota() { + $loggedInUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('UserToEdit')); + $targetUser = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToEdit') + ->will($this->returnValue($targetUser)); + + $this->api->editUser('UserToEdit', 'quota', 'NewQuota'); + } + + public function testEditUserAdminUserSelfEditChangeValidQuota() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();; + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('UserToEdit')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetUser->expects($this->once()) + ->method('setQuota') + ->with('2.9 MB'); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToEdit') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('UserToEdit') + ->will($this->returnValue(true)); + + $this->assertEquals([], $this->api->editUser('UserToEdit', 'quota', '3042824')->getData()); + } + + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 103 + * @expectedExceptionMessage Invalid quota value ABC + */ + public function testEditUserAdminUserSelfEditChangeInvalidQuota() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('UserToEdit')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToEdit') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('UserToEdit') + ->will($this->returnValue(true)); + + $this->api->editUser('UserToEdit', 'quota', 'ABC'); + } + + public function testEditUserAdminUserEditChangeValidQuota() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('admin')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetUser->expects($this->once()) + ->method('setQuota') + ->with('2.9 MB'); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToEdit') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('admin') + ->will($this->returnValue(true)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor() + ->getMock(); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + + $this->assertEquals([], $this->api->editUser('UserToEdit', 'quota', '3042824')->getData()); + } + + public function testEditUserSubadminUserAccessible() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('subadmin')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetUser->expects($this->once()) + ->method('setQuota') + ->with('2.9 MB'); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToEdit') + ->will($this->returnValue($targetUser)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor() + ->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isUserAccessible') + ->with($loggedInUser, $targetUser) + ->will($this->returnValue(true)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + + $this->assertEquals([], $this->api->editUser('UserToEdit', 'quota', '3042824')->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 997 + */ + public function testEditUserSubadminUserInaccessible() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('subadmin')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToEdit') + ->will($this->returnValue($targetUser)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor() + ->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isUserAccessible') + ->with($loggedInUser, $targetUser) + ->will($this->returnValue(false)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + + $this->api->editUser('UserToEdit', 'quota', 'value'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + */ + public function testDeleteUserNotExistingUser() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('UserToEdit')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToDelete') + ->will($this->returnValue(null)); + + $this->api->deleteUser('UserToDelete'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + */ + public function testDeleteUserSelf() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('UserToDelete')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('UserToDelete')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToDelete') + ->will($this->returnValue($targetUser)); + + $this->api->deleteUser('UserToDelete'); + } + + public function testDeleteSuccessfulUserAsAdmin() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('admin')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('UserToDelete')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToDelete') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('admin') + ->will($this->returnValue(true)); + $targetUser + ->expects($this->once()) + ->method('delete') + ->will($this->returnValue(true)); + + $this->assertEquals([], $this->api->deleteUser('UserToDelete')->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + */ + public function testDeleteUnsuccessfulUserAsAdmin() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('admin')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('UserToDelete')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToDelete') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('admin') + ->will($this->returnValue(true)); + $targetUser + ->expects($this->once()) + ->method('delete') + ->will($this->returnValue(false)); + + $this->api->deleteUser('UserToDelete'); + } + + public function testDeleteSuccessfulUserAsSubadmin() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('subadmin')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('UserToDelete')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToDelete') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('subadmin') + ->will($this->returnValue(false)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isUserAccessible') + ->with($loggedInUser, $targetUser) + ->will($this->returnValue(true)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + $targetUser + ->expects($this->once()) + ->method('delete') + ->will($this->returnValue(true)); + + $this->assertEquals([], $this->api->deleteUser('UserToDelete')->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + */ + public function testDeleteUnsuccessfulUserAsSubadmin() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('subadmin')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('UserToDelete')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToDelete') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('subadmin') + ->will($this->returnValue(false)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isUserAccessible') + ->with($loggedInUser, $targetUser) + ->will($this->returnValue(true)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + $targetUser + ->expects($this->once()) + ->method('delete') + ->will($this->returnValue(false)); + + $this->api->deleteUser('UserToDelete'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 997 + */ + public function testDeleteUserAsSubAdminAndUserIsNotAccessible() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('subadmin')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('UserToDelete')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToDelete') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('subadmin') + ->will($this->returnValue(false)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isUserAccessible') + ->with($loggedInUser, $targetUser) + ->will($this->returnValue(false)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + + $this->api->deleteUser('UserToDelete'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 998 + */ + public function testGetUsersGroupsTargetUserNotExisting() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + + $this->api->getUsersGroups('UserToLookup'); + } + + public function testGetUsersGroupsSelfTargetted() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('UserToLookup')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('UserToLookup')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToLookup') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('getUserGroupIds') + ->with($targetUser) + ->will($this->returnValue(['DummyValue'])); + + $this->assertEquals(['groups' => ['DummyValue']], $this->api->getUsersGroups('UserToLookup')->getData()); + } + + public function testGetUsersGroupsForAdminUser() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->exactly(2)) + ->method('getUID') + ->will($this->returnValue('admin')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('UserToLookup')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToLookup') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('getUserGroupIds') + ->with($targetUser) + ->will($this->returnValue(['DummyValue'])); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('admin') + ->will($this->returnValue(true)); + + $this->assertEquals(['groups' => ['DummyValue']], $this->api->getUsersGroups('UserToLookup')->getData()); + } + + public function testGetUsersGroupsForSubAdminUserAndUserIsAccessible() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->exactly(2)) + ->method('getUID') + ->will($this->returnValue('subadmin')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('UserToLookup')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToLookup') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('subadmin') + ->will($this->returnValue(false)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isUserAccessible') + ->with($loggedInUser, $targetUser) + ->will($this->returnValue(true)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + $group1 = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $group1 + ->expects($this->any()) + ->method('getGID') + ->will($this->returnValue('Group1')); + $group2 = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $group2 + ->expects($this->any()) + ->method('getGID') + ->will($this->returnValue('Group2')); + $subAdminManager + ->expects($this->once()) + ->method('getSubAdminsGroups') + ->with($loggedInUser) + ->will($this->returnValue([$group1, $group2])); + $this->groupManager + ->expects($this->any()) + ->method('getUserGroupIds') + ->with($targetUser) + ->will($this->returnValue(['Group1'])); + + $this->assertEquals(['groups' => ['Group1']], $this->api->getUsersGroups('UserToLookup')->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 997 + */ + public function testGetUsersGroupsForSubAdminUserAndUserIsInaccessible() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->exactly(2)) + ->method('getUID') + ->will($this->returnValue('subadmin')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('UserToLookup')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('UserToLookup') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('subadmin') + ->will($this->returnValue(false)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isUserAccessible') + ->with($loggedInUser, $targetUser) + ->will($this->returnValue(false)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + $this->groupManager + ->expects($this->any()) + ->method('getUserGroupIds') + ->with($targetUser) + ->will($this->returnValue(['Group1'])); + + $this->api->getUsersGroups('UserToLookup'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 102 + */ + public function testAddToGroupWithTargetGroupNotExisting() { + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('GroupToAddTo') + ->will($this->returnValue(null)); + + $this->api->addToGroup('TargetUser', 'GroupToAddTo'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + */ + public function testAddToGroupWithNoGroupSpecified() { + $this->api->addToGroup('TargetUser'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 103 + */ + public function testAddToGroupWithTargetUserNotExisting() { + $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('GroupToAddTo') + ->will($this->returnValue($targetGroup)); + + $this->api->addToGroup('TargetUser', 'GroupToAddTo'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + */ + public function testRemoveFromGroupWithNoTargetGroup() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + + $this->api->removeFromGroup('TargetUser', null); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 102 + */ + public function testRemoveFromGroupWithNotExistingTargetGroup() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('TargetGroup') + ->will($this->returnValue(null)); + + $this->api->removeFromGroup('TargetUser', 'TargetGroup'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 103 + */ + public function testRemoveFromGroupWithNotExistingTargetUser() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('TargetGroup') + ->will($this->returnValue($targetGroup)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('TargetUser') + ->will($this->returnValue(null)); + + $this->api->removeFromGroup('TargetUser', 'TargetGroup'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 104 + */ + public function testRemoveFromGroupWithoutPermission() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('unauthorizedUser')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('TargetGroup') + ->will($this->returnValue($targetGroup)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('TargetUser') + ->will($this->returnValue($targetUser)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->with('unauthorizedUser') + ->will($this->returnValue(false)); + + $this->api->removeFromGroup('TargetUser', 'TargetGroup'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 105 + * @expectedExceptionMessage Cannot remove yourself from the admin group + */ + public function testRemoveFromGroupAsAdminFromAdmin() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('admin')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $targetGroup + ->expects($this->once()) + ->method('getGID') + ->will($this->returnValue('admin')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('admin') + ->will($this->returnValue($targetGroup)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('admin') + ->will($this->returnValue($targetUser)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + $this->groupManager + ->expects($this->any()) + ->method('isAdmin') + ->with('admin') + ->will($this->returnValue(true)); + + $this->api->removeFromGroup('admin', 'admin'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 105 + * @expectedExceptionMessage Cannot remove yourself from this group as you are a SubAdmin + */ + public function testRemoveFromGroupAsSubAdminFromSubAdmin() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('subadmin')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $targetGroup + ->expects($this->any()) + ->method('getGID') + ->will($this->returnValue('subadmin')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('subadmin') + ->will($this->returnValue($targetGroup)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('subadmin') + ->will($this->returnValue($targetUser)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isSubAdminofGroup') + ->with($loggedInUser, $targetGroup) + ->will($this->returnValue(true)); + $subAdminManager + ->expects($this->once()) + ->method('getSubAdminsGroups') + ->with($loggedInUser) + ->will($this->returnValue([$targetGroup])); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + $this->groupManager + ->expects($this->any()) + ->method('isAdmin') + ->with('subadmin') + ->will($this->returnValue(false)); + + $this->api->removeFromGroup('subadmin', 'subadmin'); + } + + public function testRemoveFromGroupSuccessful() { + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('admin')); + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('admin') + ->will($this->returnValue($targetGroup)); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('AnotherUser') + ->will($this->returnValue($targetUser)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + $this->groupManager + ->expects($this->any()) + ->method('isAdmin') + ->with('admin') + ->will($this->returnValue(true)); + $targetGroup + ->expects($this->once()) + ->method('removeUser') + ->with($targetUser); + + $this->assertEquals([], $this->api->removeFromGroup('AnotherUser', 'admin')->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + * @expectedExceptionMessage User does not exist + */ + public function testAddSubAdminWithNotExistingTargetUser() { + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('NotExistingUser') + ->will($this->returnValue(null)); + + $this->api->addSubAdmin('NotExistingUser', null); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 102 + * @expectedExceptionMessage Group:NotExistingGroup does not exist + */ + public function testAddSubAdminWithNotExistingTargetGroup() { + + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('ExistingUser') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('NotExistingGroup') + ->will($this->returnValue(null)); + + $this->api->addSubAdmin('ExistingUser', 'NotExistingGroup'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 103 + * @expectedExceptionMessage Cannot create subadmins for admin group + */ + public function testAddSubAdminToAdminGroup() { + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('ExistingUser') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('ADmiN') + ->will($this->returnValue($targetGroup)); + + $this->api->addSubAdmin('ExistingUser', 'ADmiN'); + } + + public function testAddSubAdminTwice() { + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('ExistingUser') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('TargetGroup') + ->will($this->returnValue($targetGroup)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isSubAdminOfGroup') + ->with($targetUser, $targetGroup) + ->will($this->returnValue(true)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + + $this->assertEquals([], $this->api->addSubAdmin('ExistingUser', 'TargetGroup')->getData()); + } + + public function testAddSubAdminSuccessful() { + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('ExistingUser') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('TargetGroup') + ->will($this->returnValue($targetGroup)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isSubAdminOfGroup') + ->with($targetUser, $targetGroup) + ->will($this->returnValue(false)); + $subAdminManager + ->expects($this->once()) + ->method('createSubAdmin') + ->with($targetUser, $targetGroup) + ->will($this->returnValue(true)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + + $this->assertEquals([], $this->api->addSubAdmin('ExistingUser', 'TargetGroup')->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 103 + * @expectedExceptionMessage Unknown error occurred + */ + public function testAddSubAdminUnsuccessful() { + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('ExistingUser') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('TargetGroup') + ->will($this->returnValue($targetGroup)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isSubAdminOfGroup') + ->with($targetUser, $targetGroup) + ->will($this->returnValue(false)); + $subAdminManager + ->expects($this->once()) + ->method('createSubAdmin') + ->with($targetUser, $targetGroup) + ->will($this->returnValue(false)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + + $this->api->addSubAdmin('ExistingUser', 'TargetGroup'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + * @expectedExceptionMessage User does not exist + */ + public function testRemoveSubAdminNotExistingTargetUser() { + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('NotExistingUser') + ->will($this->returnValue(null)); + + $this->api->removeSubAdmin('NotExistingUser', 'GroupToDeleteFrom'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + * @expectedExceptionMessage Group does not exist + */ + public function testRemoveSubAdminNotExistingTargetGroup() { + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('ExistingUser') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('GroupToDeleteFrom') + ->will($this->returnValue(null)); + + $this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom'); + } + + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 102 + * @expectedExceptionMessage User is not a subadmin of this group + */ + public function testRemoveSubAdminFromNotASubadmin() { + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('ExistingUser') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('GroupToDeleteFrom') + ->will($this->returnValue($targetGroup)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isSubAdminOfGroup') + ->with($targetUser, $targetGroup) + ->will($this->returnValue(false)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + + $this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom'); + } + + public function testRemoveSubAdminSuccessful() { + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('ExistingUser') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('GroupToDeleteFrom') + ->will($this->returnValue($targetGroup)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isSubAdminOfGroup') + ->with($targetUser, $targetGroup) + ->will($this->returnValue(true)); + $subAdminManager + ->expects($this->once()) + ->method('deleteSubAdmin') + ->with($targetUser, $targetGroup) + ->will($this->returnValue(true)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + + $this->assertEquals([], $this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom')->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 103 + * @expectedExceptionMessage Unknown error occurred + */ + public function testRemoveSubAdminUnsuccessful() { + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('ExistingUser') + ->will($this->returnValue($targetUser)); + $this->groupManager + ->expects($this->once()) + ->method('get') + ->with('GroupToDeleteFrom') + ->will($this->returnValue($targetGroup)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('isSubAdminOfGroup') + ->with($targetUser, $targetGroup) + ->will($this->returnValue(true)); + $subAdminManager + ->expects($this->once()) + ->method('deleteSubAdmin') + ->with($targetUser, $targetGroup) + ->will($this->returnValue(false)); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + + $this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom'); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + * @expectedExceptionMessage User does not exist + */ + public function testGetUserSubAdminGroupsNotExistingTargetUser() { + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('RequestedUser') + ->will($this->returnValue(null)); + + $this->api->getUserSubAdminGroups('RequestedUser'); + } + + public function testGetUserSubAdminGroupsWithGroups() { + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + $targetGroup + ->expects($this->once()) + ->method('getGID') + ->will($this->returnValue('TargetGroup')); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('RequestedUser') + ->will($this->returnValue($targetUser)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('getSubAdminsGroups') + ->with($targetUser) + ->will($this->returnValue([$targetGroup])); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + + $this->assertEquals(['TargetGroup'], $this->api->getUserSubAdminGroups('RequestedUser')->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 102 + * @expectedExceptionMessage Unknown error occurred + */ + public function testGetUserSubAdminGroupsWithoutGroups() { + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('RequestedUser') + ->will($this->returnValue($targetUser)); + $subAdminManager = $this->getMockBuilder('OC\SubAdmin') + ->disableOriginalConstructor()->getMock(); + $subAdminManager + ->expects($this->once()) + ->method('getSubAdminsGroups') + ->with($targetUser) + ->will($this->returnValue([])); + $this->groupManager + ->expects($this->once()) + ->method('getSubAdmin') + ->will($this->returnValue($subAdminManager)); + + $this->api->getUserSubAdminGroups('RequestedUser'); + } + + public function testEnableUser() { + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetUser->expects($this->once()) + ->method('setEnabled') + ->with(true); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('RequestedUser') + ->will($this->returnValue($targetUser)); + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->exactly(2)) + ->method('getUID') + ->will($this->returnValue('admin')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->will($this->returnValue(true)); + + $this->assertEquals([], $this->api->enableUser('RequestedUser')->getData()); + } + + public function testDisableUser() { + $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $targetUser->expects($this->once()) + ->method('setEnabled') + ->with(false); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('RequestedUser') + ->will($this->returnValue($targetUser)); + $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); + $loggedInUser + ->expects($this->exactly(2)) + ->method('getUID') + ->will($this->returnValue('admin')); + $this->userSession + ->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($loggedInUser)); + $this->groupManager + ->expects($this->once()) + ->method('isAdmin') + ->will($this->returnValue(true)); + + $this->assertEquals([], $this->api->disableUser('RequestedUser')->getData()); + } +} diff --git a/apps/provisioning_api/tests/UsersTest.php b/apps/provisioning_api/tests/UsersTest.php deleted file mode 100644 index e67d603e487..00000000000 --- a/apps/provisioning_api/tests/UsersTest.php +++ /dev/null @@ -1,2441 +0,0 @@ - - * @author Joas Schilling - * @author Lukas Reschke - * @author michag86 - * @author Morris Jobke - * @author Roeland Jago Douma - * @author Thomas Müller - * @author Tom Needham - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Provisioning_API\Tests; - -use OCA\Provisioning_API\Users; -use OCP\API; -use OCP\IUserManager; -use OCP\IConfig; -use OCP\IUserSession; -use PHPUnit_Framework_MockObject_MockObject; -use Test\TestCase as OriginalTest; -use OCP\ILogger; - -class UsersTest extends OriginalTest { - - /** @var IUserManager | PHPUnit_Framework_MockObject_MockObject */ - protected $userManager; - /** @var IConfig | PHPUnit_Framework_MockObject_MockObject */ - protected $config; - /** @var \OC\Group\Manager | PHPUnit_Framework_MockObject_MockObject */ - protected $groupManager; - /** @var IUserSession | PHPUnit_Framework_MockObject_MockObject */ - protected $userSession; - /** @var ILogger | PHPUnit_Framework_MockObject_MockObject */ - protected $logger; - /** @var Users | PHPUnit_Framework_MockObject_MockObject */ - protected $api; - - protected function tearDown() { - $_GET = null; - $_POST = null; - parent::tearDown(); - } - - protected function setUp() { - parent::setUp(); - - $this->userManager = $this->getMockBuilder('OCP\IUserManager') - ->disableOriginalConstructor() - ->getMock(); - $this->config = $this->getMockBuilder('OCP\IConfig') - ->disableOriginalConstructor() - ->getMock(); - $this->groupManager = $this->getMockBuilder('OC\Group\Manager') - ->disableOriginalConstructor() - ->getMock(); - $this->userSession = $this->getMockBuilder('OCP\IUserSession') - ->disableOriginalConstructor() - ->getMock(); - $this->logger = $this->getMockBuilder('OCP\ILogger') - ->disableOriginalConstructor() - ->getMock(); - $this->api = $this->getMockBuilder('OCA\Provisioning_API\Users') - ->setConstructorArgs([ - $this->userManager, - $this->config, - $this->groupManager, - $this->userSession, - $this->logger, - ]) - ->setMethods(['fillStorageInfo']) - ->getMock(); - } - - public function testGetUsersNotLoggedIn() { - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, API::RESPOND_UNAUTHORISED); - $this->assertEquals($expected, $this->api->getUsers()); - } - - public function testGetUsersAsAdmin() { - $_GET['search'] = 'MyCustomSearch'; - - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('admin')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->will($this->returnValue(true)); - $this->userManager - ->expects($this->once()) - ->method('search') - ->with('MyCustomSearch', null, null) - ->will($this->returnValue(['Admin' => [], 'Foo' => [], 'Bar' => []])); - - $expected = new \OC\OCS\Result([ - 'users' => [ - 'Admin', - 'Foo', - 'Bar', - ], - ]); - $this->assertEquals($expected, $this->api->getUsers()); - } - - public function testGetUsersAsSubAdmin() { - $_GET['search'] = 'MyCustomSearch'; - - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('subadmin')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->will($this->returnValue(false)); - $firstGroup = $this->getMockBuilder('OCP\IGroup') - ->disableOriginalConstructor() - ->getMock(); - $firstGroup - ->expects($this->once()) - ->method('getGID') - ->will($this->returnValue('FirstGroup')); - $secondGroup = $this->getMockBuilder('OCP\IGroup') - ->disableOriginalConstructor() - ->getMock(); - $secondGroup - ->expects($this->once()) - ->method('getGID') - ->will($this->returnValue('SecondGroup')); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isSubAdmin') - ->with($loggedInUser) - ->will($this->returnValue(true)); - $subAdminManager - ->expects($this->once()) - ->method('getSubAdminsGroups') - ->with($loggedInUser) - ->will($this->returnValue([$firstGroup, $secondGroup])); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - $this->groupManager - ->expects($this->any()) - ->method('displayNamesInGroup') - ->will($this->onConsecutiveCalls(['AnotherUserInTheFirstGroup' => []], ['UserInTheSecondGroup' => []])); - - $expected = new \OC\OCS\Result([ - 'users' => [ - 'AnotherUserInTheFirstGroup', - 'UserInTheSecondGroup', - ], - ]); - $this->assertEquals($expected, $this->api->getUsers()); - } - - public function testGetUsersAsRegularUser() { - $_GET['search'] = 'MyCustomSearch'; - - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('regularUser')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->will($this->returnValue(false)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isSubAdmin') - ->with($loggedInUser) - ->will($this->returnValue(false)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - - $expected = new \OC\OCS\Result(null, API::RESPOND_UNAUTHORISED); - $this->assertEquals($expected, $this->api->getUsers()); - } - - public function testAddUserAlreadyExisting() { - $_POST['userid'] = 'AlreadyExistingUser'; - $this->userManager - ->expects($this->once()) - ->method('userExists') - ->with('AlreadyExistingUser') - ->will($this->returnValue(true)); - $this->logger - ->expects($this->once()) - ->method('error') - ->with('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('adminUser')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('adminUser') - ->willReturn(true); - - $expected = new \OC\OCS\Result(null, 102, 'User already exists'); - $this->assertEquals($expected, $this->api->addUser()); - } - - public function testAddUserNonExistingGroup() { - $_POST['userid'] = 'NewUser'; - $_POST['groups'] = ['NonExistingGroup']; - $this->userManager - ->expects($this->once()) - ->method('userExists') - ->with('NewUser') - ->willReturn(false); - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('adminUser')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('adminUser') - ->willReturn(true); - $this->groupManager - ->expects($this->once()) - ->method('groupExists') - ->with('NonExistingGroup') - ->willReturn(false); - - $expected = new \OC\OCS\Result(null, 104, 'group NonExistingGroup does not exist'); - $this->assertEquals($expected, $this->api->addUser()); - } - - public function testAddUserExistingGroupNonExistingGroup() { - $_POST['userid'] = 'NewUser'; - $_POST['groups'] = ['ExistingGroup', 'NonExistingGroup']; - $this->userManager - ->expects($this->once()) - ->method('userExists') - ->with('NewUser') - ->willReturn(false); - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('adminUser')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('adminUser') - ->willReturn(true); - $this->groupManager - ->expects($this->exactly(2)) - ->method('groupExists') - ->withConsecutive( - ['ExistingGroup'], - ['NonExistingGroup'] - ) - ->will($this->returnValueMap([ - ['ExistingGroup', true], - ['NonExistingGroup', false] - ])); - - $expected = new \OC\OCS\Result(null, 104, 'group NonExistingGroup does not exist'); - $this->assertEquals($expected, $this->api->addUser()); - } - - public function testAddUserSuccessful() { - $_POST['userid'] = 'NewUser'; - $_POST['password'] = 'PasswordOfTheNewUser'; - $this->userManager - ->expects($this->once()) - ->method('userExists') - ->with('NewUser') - ->will($this->returnValue(false)); - $this->userManager - ->expects($this->once()) - ->method('createUser') - ->with('NewUser', 'PasswordOfTheNewUser'); - $this->logger - ->expects($this->once()) - ->method('info') - ->with('Successful addUser call with userid: NewUser', ['app' => 'ocs_api']); - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('adminUser')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('adminUser') - ->willReturn(true); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->addUser()); - } - - public function testAddUserExistingGroup() { - $_POST['userid'] = 'NewUser'; - $_POST['password'] = 'PasswordOfTheNewUser'; - $_POST['groups'] = ['ExistingGroup']; - $this->userManager - ->expects($this->once()) - ->method('userExists') - ->with('NewUser') - ->willReturn(false); - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('adminUser')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('adminUser') - ->willReturn(true); - $this->groupManager - ->expects($this->once()) - ->method('groupExists') - ->with('ExistingGroup') - ->willReturn(true); - $user = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $this->userManager - ->expects($this->once()) - ->method('createUser') - ->with('NewUser', 'PasswordOfTheNewUser') - ->willReturn($user); - $group = $this->getMockBuilder('OCP\IGroup') - ->disableOriginalConstructor() - ->getMock(); - $group - ->expects($this->once()) - ->method('addUser') - ->with($user); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('ExistingGroup') - ->willReturn($group); - $this->logger - ->expects($this->exactly(2)) - ->method('info') - ->withConsecutive( - ['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']], - ['Added userid NewUser to group ExistingGroup', ['app' => 'ocs_api']] - ); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->addUser()); - } - - public function testAddUserUnsuccessful() { - $_POST['userid'] = 'NewUser'; - $_POST['password'] = 'PasswordOfTheNewUser'; - $this->userManager - ->expects($this->once()) - ->method('userExists') - ->with('NewUser') - ->will($this->returnValue(false)); - $this->userManager - ->expects($this->once()) - ->method('createUser') - ->with('NewUser', 'PasswordOfTheNewUser') - ->will($this->throwException(new \Exception('User backend not found.'))); - $this->logger - ->expects($this->once()) - ->method('error') - ->with('Failed addUser attempt with exception: User backend not found.', ['app' => 'ocs_api']); - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('adminUser')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('adminUser') - ->willReturn(true); - - $expected = new \OC\OCS\Result(null, 101, 'Bad request'); - $this->assertEquals($expected, $this->api->addUser()); - } - - public function testAddUserAsRegularUser() { - $_POST['userid'] = 'NewUser'; - $_POST['password'] = 'PasswordOfTheNewUser'; - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('regularUser')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('regularUser') - ->willReturn(false); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isSubAdmin') - ->with($loggedInUser) - ->willReturn(false); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->with() - ->willReturn($subAdminManager); - - $expected = new \OC\OCS\Result(null, API::RESPOND_UNAUTHORISED); - $this->assertEquals($expected, $this->api->addUser()); - } - - public function testAddUserAsSubAdminNoGroup() { - $_POST['userid'] = 'NewUser'; - $_POST['password'] = 'PasswordOfTheNewUser'; - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('regularUser')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('regularUser') - ->willReturn(false); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isSubAdmin') - ->with($loggedInUser) - ->willReturn(true); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->with() - ->willReturn($subAdminManager); - - $expected = new \OC\OCS\Result(null, 106, 'no group specified (required for subadmins)'); - $this->assertEquals($expected, $this->api->addUser()); - } - - public function testAddUserAsSubAdminValidGroupNotSubAdmin() { - $_POST['userid'] = 'NewUser'; - $_POST['password'] = 'PasswordOfTheNewUser'; - $_POST['groups'] = ['ExistingGroup']; - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('regularUser')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('regularUser') - ->willReturn(false); - $existingGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('ExistingGroup') - ->willReturn($existingGroup); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isSubAdmin') - ->with($loggedInUser) - ->willReturn(true); - $subAdminManager - ->expects($this->once()) - ->method('isSubAdminOfGroup') - ->with($loggedInUser, $existingGroup) - ->willReturn(false); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->with() - ->willReturn($subAdminManager); - $this->groupManager - ->expects($this->once()) - ->method('groupExists') - ->with('ExistingGroup') - ->willReturn(true); - - $expected = new \OC\OCS\Result(null, 105, 'insufficient privileges for group ExistingGroup'); - $this->assertEquals($expected, $this->api->addUser()); - } - - public function testAddUserAsSubAdminExistingGroups() { - $_POST['userid'] = 'NewUser'; - $_POST['password'] = 'PasswordOfTheNewUser'; - $_POST['groups'] = ['ExistingGroup1', 'ExistingGroup2']; - $this->userManager - ->expects($this->once()) - ->method('userExists') - ->with('NewUser') - ->willReturn(false); - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('subAdminUser')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('subAdminUser') - ->willReturn(false); - $this->groupManager - ->expects($this->exactly(2)) - ->method('groupExists') - ->withConsecutive( - ['ExistingGroup1'], - ['ExistingGroup2'] - ) - ->willReturn(true); - $user = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $this->userManager - ->expects($this->once()) - ->method('createUser') - ->with('NewUser', 'PasswordOfTheNewUser') - ->willReturn($user); - $existingGroup1 = $this->getMockBuilder('OCP\IGroup') - ->disableOriginalConstructor() - ->getMock(); - $existingGroup2 = $this->getMockBuilder('OCP\IGroup') - ->disableOriginalConstructor() - ->getMock(); - $existingGroup1 - ->expects($this->once()) - ->method('addUser') - ->with($user); - $existingGroup2 - ->expects($this->once()) - ->method('addUser') - ->with($user); - $this->groupManager - ->expects($this->exactly(4)) - ->method('get') - ->withConsecutive( - ['ExistingGroup1'], - ['ExistingGroup2'], - ['ExistingGroup1'], - ['ExistingGroup2'] - ) - ->will($this->returnValueMap([ - ['ExistingGroup1', $existingGroup1], - ['ExistingGroup2', $existingGroup2] - ])); - $this->logger - ->expects($this->exactly(3)) - ->method('info') - ->withConsecutive( - ['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']], - ['Added userid NewUser to group ExistingGroup1', ['app' => 'ocs_api']], - ['Added userid NewUser to group ExistingGroup2', ['app' => 'ocs_api']] - ); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->willReturn($subAdminManager); - $subAdminManager - ->expects($this->once()) - ->method('isSubAdmin') - ->with($loggedInUser) - ->willReturn(true); - $subAdminManager - ->expects($this->exactly(2)) - ->method('isSubAdminOfGroup') - ->withConsecutive( - [$loggedInUser, $existingGroup1], - [$loggedInUser, $existingGroup2] - ) - ->willReturn(true); - - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->addUser()); - } - - - public function testGetUserNotLoggedIn() { - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, API::RESPOND_UNAUTHORISED); - $this->assertEquals($expected, $this->api->getUser(['userid' => 'UserToGet'])); - } - - public function testGetUserTargetDoesNotExist() { - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToGet') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, API::RESPOND_NOT_FOUND, 'The requested user could not be found'); - $this->assertEquals($expected, $this->api->getUser(['userid' => 'UserToGet'])); - } - - public function testGetUserAsAdmin() { - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('admin')); - $targetUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $targetUser->expects($this->once()) - ->method('getEMailAddress') - ->willReturn('demo@owncloud.org'); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToGet') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('admin') - ->will($this->returnValue(true)); - $this->config - ->expects($this->at(0)) - ->method('getUserValue') - ->with('UserToGet', 'core', 'enabled', 'true') - ->will($this->returnValue('true')); - $this->api - ->expects($this->once()) - ->method('fillStorageInfo') - ->with('UserToGet') - ->will($this->returnValue(['DummyValue'])); - $targetUser - ->expects($this->once()) - ->method('getDisplayName') - ->will($this->returnValue('Demo User')); - - $expected = new \OC\OCS\Result( - [ - 'enabled' => 'true', - 'quota' => ['DummyValue'], - 'email' => 'demo@owncloud.org', - 'displayname' => 'Demo User', - ] - ); - $this->assertEquals($expected, $this->api->getUser(['userid' => 'UserToGet'])); - } - - public function testGetUserAsSubAdminAndUserIsAccessible() { - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('subadmin')); - $targetUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $targetUser - ->expects($this->once()) - ->method('getEMailAddress') - ->willReturn('demo@owncloud.org'); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToGet') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('subadmin') - ->will($this->returnValue(false)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor() - ->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isUserAccessible') - ->with($loggedInUser, $targetUser) - ->will($this->returnValue(true)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - $this->config - ->expects($this->at(0)) - ->method('getUserValue') - ->with('UserToGet', 'core', 'enabled', 'true') - ->will($this->returnValue('true')); - $this->api - ->expects($this->once()) - ->method('fillStorageInfo') - ->with('UserToGet') - ->will($this->returnValue(['DummyValue'])); - $targetUser - ->expects($this->once()) - ->method('getDisplayName') - ->will($this->returnValue('Demo User')); - - $expected = new \OC\OCS\Result( - [ - 'enabled' => 'true', - 'quota' => ['DummyValue'], - 'email' => 'demo@owncloud.org', - 'displayname' => 'Demo User', - ] - ); - $this->assertEquals($expected, $this->api->getUser(['userid' => 'UserToGet'])); - } - - public function testGetUserAsSubAdminAndUserIsNotAccessible() { - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->exactly(2)) - ->method('getUID') - ->will($this->returnValue('subadmin')); - $targetUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToGet') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('subadmin') - ->will($this->returnValue(false)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor() - ->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isUserAccessible') - ->with($loggedInUser, $targetUser) - ->will($this->returnValue(false)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - - $expected = new \OC\OCS\Result(null, API::RESPOND_UNAUTHORISED); - $this->assertEquals($expected, $this->api->getUser(['userid' => 'UserToGet'])); - } - - public function testGetUserAsSubAdminSelfLookup() { - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->exactly(2)) - ->method('getUID') - ->will($this->returnValue('subadmin')); - $targetUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('subadmin') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('subadmin') - ->will($this->returnValue(false)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor() - ->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isUserAccessible') - ->with($loggedInUser, $targetUser) - ->will($this->returnValue(false)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - $this->api - ->expects($this->once()) - ->method('fillStorageInfo') - ->with('subadmin') - ->will($this->returnValue(['DummyValue'])); - $targetUser - ->expects($this->once()) - ->method('getDisplayName') - ->will($this->returnValue('Subadmin User')); - $targetUser - ->expects($this->once()) - ->method('getEMailAddress') - ->will($this->returnValue('subadmin@owncloud.org')); - - $expected = new \OC\OCS\Result([ - 'quota' => ['DummyValue'], - 'email' => 'subadmin@owncloud.org', - 'displayname' => 'Subadmin User', - ]); - $this->assertEquals($expected, $this->api->getUser(['userid' => 'subadmin'])); - } - - public function testEditUserNotLoggedIn() { - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, API::RESPOND_UNAUTHORISED); - $this->assertEquals($expected, $this->api->editUser(['userid' => 'UserToEdit'])); - } - - public function testEditUserRegularUserSelfEditChangeDisplayName() { - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('UserToEdit')); - $targetUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToEdit') - ->will($this->returnValue($targetUser)); - $targetUser - ->expects($this->once()) - ->method('setDisplayName') - ->with('NewDisplayName'); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->editUser(['userid' => 'UserToEdit', '_put' => ['key' => 'display', 'value' => 'NewDisplayName']])); - } - - public function testEditUserRegularUserSelfEditChangeEmailValid() { - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('UserToEdit')); - $targetUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToEdit') - ->will($this->returnValue($targetUser)); - $targetUser - ->expects($this->once()) - ->method('setEMailAddress') - ->with('demo@owncloud.org'); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->editUser(['userid' => 'UserToEdit', '_put' => ['key' => 'email', 'value' => 'demo@owncloud.org']])); - } - - public function testEditUserRegularUserSelfEditChangeEmailInvalid() { - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('UserToEdit')); - $targetUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToEdit') - ->will($this->returnValue($targetUser)); - - $expected = new \OC\OCS\Result(null, 102); - $this->assertEquals($expected, $this->api->editUser(['userid' => 'UserToEdit', '_put' => ['key' => 'email', 'value' => 'demo.org']])); - } - - public function testEditUserRegularUserSelfEditChangePassword() { - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('UserToEdit')); - $targetUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToEdit') - ->will($this->returnValue($targetUser)); - $targetUser - ->expects($this->once()) - ->method('setPassword') - ->with('NewPassword'); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->editUser(['userid' => 'UserToEdit', '_put' => ['key' => 'password', 'value' => 'NewPassword']])); - } - - public function testEditUserRegularUserSelfEditChangeQuota() { - $loggedInUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('UserToEdit')); - $targetUser = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToEdit') - ->will($this->returnValue($targetUser)); - - $expected = new \OC\OCS\Result(null, 997); - $this->assertEquals($expected, $this->api->editUser(['userid' => 'UserToEdit', '_put' => ['key' => 'quota', 'value' => 'NewQuota']])); - } - - public function testEditUserAdminUserSelfEditChangeValidQuota() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock();; - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('UserToEdit')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetUser->expects($this->once()) - ->method('setQuota') - ->with('2.9 MB'); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToEdit') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('UserToEdit') - ->will($this->returnValue(true)); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->editUser(['userid' => 'UserToEdit', '_put' => ['key' => 'quota', 'value' => '3042824']])); - } - - public function testEditUserAdminUserSelfEditChangeInvalidQuota() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('UserToEdit')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToEdit') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('UserToEdit') - ->will($this->returnValue(true)); - - $expected = new \OC\OCS\Result(null, 103, 'Invalid quota value ABC'); - $this->assertEquals($expected, $this->api->editUser(['userid' => 'UserToEdit', '_put' => ['key' => 'quota', 'value' => 'ABC']])); - } - - public function testEditUserAdminUserEditChangeValidQuota() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('admin')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetUser->expects($this->once()) - ->method('setQuota') - ->with('2.9 MB'); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToEdit') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('admin') - ->will($this->returnValue(true)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor() - ->getMock(); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->editUser(['userid' => 'UserToEdit', '_put' => ['key' => 'quota', 'value' => '3042824']])); - } - - public function testEditUserSubadminUserAccessible() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('subadmin')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetUser->expects($this->once()) - ->method('setQuota') - ->with('2.9 MB'); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToEdit') - ->will($this->returnValue($targetUser)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor() - ->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isUserAccessible') - ->with($loggedInUser, $targetUser) - ->will($this->returnValue(true)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->editUser(['userid' => 'UserToEdit', '_put' => ['key' => 'quota', 'value' => '3042824']])); - } - - public function testEditUserSubadminUserInaccessible() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('subadmin')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToEdit') - ->will($this->returnValue($targetUser)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor() - ->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isUserAccessible') - ->with($loggedInUser, $targetUser) - ->will($this->returnValue(false)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - - $expected = new \OC\OCS\Result(null, 997); - $this->assertEquals($expected, $this->api->editUser(['userid' => 'UserToEdit', '_put' => ['key' => 'quota', 'value' => '3042824']])); - } - - public function testDeleteUserNotLoggedIn() { - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, 997); - $this->assertEquals($expected, $this->api->deleteUser(['userid' => 'UserToDelete'])); - } - - public function testDeleteUserNotExistingUser() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('UserToEdit')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToDelete') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, 101); - $this->assertEquals($expected, $this->api->deleteUser(['userid' => 'UserToDelete'])); - } - - public function testDeleteUserSelf() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('UserToDelete')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('UserToDelete')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToDelete') - ->will($this->returnValue($targetUser)); - - $expected = new \OC\OCS\Result(null, 101); - $this->assertEquals($expected, $this->api->deleteUser(['userid' => 'UserToDelete'])); - } - - public function testDeleteSuccessfulUserAsAdmin() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('admin')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('UserToDelete')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToDelete') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('admin') - ->will($this->returnValue(true)); - $targetUser - ->expects($this->once()) - ->method('delete') - ->will($this->returnValue(true)); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->deleteUser(['userid' => 'UserToDelete'])); - } - - public function testDeleteUnsuccessfulUserAsAdmin() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('admin')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('UserToDelete')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToDelete') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('admin') - ->will($this->returnValue(true)); - $targetUser - ->expects($this->once()) - ->method('delete') - ->will($this->returnValue(false)); - - $expected = new \OC\OCS\Result(null, 101); - $this->assertEquals($expected, $this->api->deleteUser(['userid' => 'UserToDelete'])); - } - - public function testDeleteSuccessfulUserAsSubadmin() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('subadmin')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('UserToDelete')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToDelete') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('subadmin') - ->will($this->returnValue(false)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isUserAccessible') - ->with($loggedInUser, $targetUser) - ->will($this->returnValue(true)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - $targetUser - ->expects($this->once()) - ->method('delete') - ->will($this->returnValue(true)); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->deleteUser(['userid' => 'UserToDelete'])); - } - - public function testDeleteUnsuccessfulUserAsSubadmin() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('subadmin')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('UserToDelete')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToDelete') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('subadmin') - ->will($this->returnValue(false)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isUserAccessible') - ->with($loggedInUser, $targetUser) - ->will($this->returnValue(true)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - $targetUser - ->expects($this->once()) - ->method('delete') - ->will($this->returnValue(false)); - - $expected = new \OC\OCS\Result(null, 101); - $this->assertEquals($expected, $this->api->deleteUser(['userid' => 'UserToDelete'])); - } - - public function testDeleteUserAsSubAdminAndUserIsNotAccessible() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('subadmin')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('UserToDelete')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToDelete') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('subadmin') - ->will($this->returnValue(false)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isUserAccessible') - ->with($loggedInUser, $targetUser) - ->will($this->returnValue(false)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - - $expected = new \OC\OCS\Result(null, 997); - $this->assertEquals($expected, $this->api->deleteUser(['userid' => 'UserToDelete'])); - } - - public function testGetUsersGroupsNotLoggedIn() { - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, 997); - $this->assertEquals($expected, $this->api->getUsersGroups(['userid' => 'UserToLookup'])); - } - - public function testGetUsersGroupsTargetUserNotExisting() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - - $expected = new \OC\OCS\Result(null, 998); - $this->assertEquals($expected, $this->api->getUsersGroups(['userid' => 'UserToLookup'])); - } - - public function testGetUsersGroupsSelfTargetted() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('UserToLookup')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('UserToLookup')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToLookup') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('getUserGroupIds') - ->with($targetUser) - ->will($this->returnValue(['DummyValue'])); - - $expected = new \OC\OCS\Result(['groups' => ['DummyValue']]); - $this->assertEquals($expected, $this->api->getUsersGroups(['userid' => 'UserToLookup'])); - } - - public function testGetUsersGroupsForAdminUser() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->exactly(2)) - ->method('getUID') - ->will($this->returnValue('admin')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('UserToLookup')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToLookup') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('getUserGroupIds') - ->with($targetUser) - ->will($this->returnValue(['DummyValue'])); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('admin') - ->will($this->returnValue(true)); - - $expected = new \OC\OCS\Result(['groups' => ['DummyValue']]); - $this->assertEquals($expected, $this->api->getUsersGroups(['userid' => 'UserToLookup'])); - } - - public function testGetUsersGroupsForSubAdminUserAndUserIsAccessible() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->exactly(2)) - ->method('getUID') - ->will($this->returnValue('subadmin')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('UserToLookup')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToLookup') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('subadmin') - ->will($this->returnValue(false)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isUserAccessible') - ->with($loggedInUser, $targetUser) - ->will($this->returnValue(true)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - $group1 = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $group1 - ->expects($this->any()) - ->method('getGID') - ->will($this->returnValue('Group1')); - $group2 = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $group2 - ->expects($this->any()) - ->method('getGID') - ->will($this->returnValue('Group2')); - $subAdminManager - ->expects($this->once()) - ->method('getSubAdminsGroups') - ->with($loggedInUser) - ->will($this->returnValue([$group1, $group2])); - $this->groupManager - ->expects($this->any()) - ->method('getUserGroupIds') - ->with($targetUser) - ->will($this->returnValue(['Group1'])); - - $expected = new \OC\OCS\Result(['groups' => ['Group1']]); - $this->assertEquals($expected, $this->api->getUsersGroups(['userid' => 'UserToLookup'])); - } - - - public function testGetUsersGroupsForSubAdminUserAndUserIsInaccessible() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->exactly(2)) - ->method('getUID') - ->will($this->returnValue('subadmin')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('UserToLookup')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('UserToLookup') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('subadmin') - ->will($this->returnValue(false)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isUserAccessible') - ->with($loggedInUser, $targetUser) - ->will($this->returnValue(false)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - $this->groupManager - ->expects($this->any()) - ->method('getUserGroupIds') - ->with($targetUser) - ->will($this->returnValue(['Group1'])); - - $expected = new \OC\OCS\Result(null, 997); - $this->assertEquals($expected, $this->api->getUsersGroups(['userid' => 'UserToLookup'])); - } - - public function testAddToGroupNotLoggedIn() { - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, 997); - $this->assertEquals($expected, $this->api->addToGroup([])); - } - - public function testAddToGroupWithTargetGroupNotExisting() { - $_POST['groupid'] = 'GroupToAddTo'; - - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('admin')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('GroupToAddTo') - ->will($this->returnValue(null)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('admin') - ->will($this->returnValue(true)); - - $expected = new \OC\OCS\Result(null, 102); - $this->assertEquals($expected, $this->api->addToGroup(['userid' => 'TargetUser'])); - } - - public function testAddToGroupWithNoGroupSpecified() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('admin')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('admin') - ->will($this->returnValue(true)); - - $expected = new \OC\OCS\Result(null, 101); - $this->assertEquals($expected, $this->api->addToGroup(['userid' => 'TargetUser'])); - } - - public function testAddToGroupWithTargetUserNotExisting() { - $_POST['groupid'] = 'GroupToAddTo'; - - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('admin')); - $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('GroupToAddTo') - ->will($this->returnValue($targetGroup)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('admin') - ->will($this->returnValue(true)); - - $expected = new \OC\OCS\Result(null, 103); - $this->assertEquals($expected, $this->api->addToGroup(['userid' => 'TargetUser'])); - } - - public function testAddToGroupWithoutPermission() { - $_POST['groupid'] = 'GroupToAddTo'; - - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('admin')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('admin') - ->will($this->returnValue(false)); - - $expected = new \OC\OCS\Result(null, 997); - $this->assertEquals($expected, $this->api->addToGroup(['userid' => 'TargetUser'])); - } - - public function testRemoveFromGroupWithoutLogIn() { - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, 997); - $this->assertEquals($expected, $this->api->removeFromGroup(['userid' => 'TargetUser', '_delete' => ['groupid' => 'TargetGroup']])); - } - - public function testRemoveFromGroupWithNoTargetGroup() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $expected = new \OC\OCS\Result(null, 101); - $this->assertEquals($expected, $this->api->removeFromGroup(['userid' => 'TargetUser', '_delete' => []])); - } - - public function testRemoveFromGroupWithNotExistingTargetGroup() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('TargetGroup') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, 102); - $this->assertEquals($expected, $this->api->removeFromGroup(['userid' => 'TargetUser', '_delete' => ['groupid' => 'TargetGroup']])); - } - - public function testRemoveFromGroupWithNotExistingTargetUser() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('TargetGroup') - ->will($this->returnValue($targetGroup)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('TargetUser') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, 103); - $this->assertEquals($expected, $this->api->removeFromGroup(['userid' => 'TargetUser', '_delete' => ['groupid' => 'TargetGroup']])); - } - - public function testRemoveFromGroupWithoutPermission() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->once()) - ->method('getUID') - ->will($this->returnValue('unauthorizedUser')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('TargetGroup') - ->will($this->returnValue($targetGroup)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('TargetUser') - ->will($this->returnValue($targetUser)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->with('unauthorizedUser') - ->will($this->returnValue(false)); - - $expected = new \OC\OCS\Result(null, 104); - $this->assertEquals($expected, $this->api->removeFromGroup(['userid' => 'TargetUser', '_delete' => ['groupid' => 'TargetGroup']])); - } - - public function testRemoveFromGroupAsAdminFromAdmin() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('admin')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $targetGroup - ->expects($this->once()) - ->method('getGID') - ->will($this->returnValue('admin')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('admin') - ->will($this->returnValue($targetGroup)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('admin') - ->will($this->returnValue($targetUser)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - $this->groupManager - ->expects($this->any()) - ->method('isAdmin') - ->with('admin') - ->will($this->returnValue(true)); - - $expected = new \OC\OCS\Result(null, 105, 'Cannot remove yourself from the admin group'); - $this->assertEquals($expected, $this->api->removeFromGroup(['userid' => 'admin', '_delete' => ['groupid' => 'admin']])); - } - - public function testRemoveFromGroupAsSubAdminFromSubAdmin() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('subadmin')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $targetGroup - ->expects($this->any()) - ->method('getGID') - ->will($this->returnValue('subadmin')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('subadmin') - ->will($this->returnValue($targetGroup)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('subadmin') - ->will($this->returnValue($targetUser)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isSubAdminofGroup') - ->with($loggedInUser, $targetGroup) - ->will($this->returnValue(true)); - $subAdminManager - ->expects($this->once()) - ->method('getSubAdminsGroups') - ->with($loggedInUser) - ->will($this->returnValue([$targetGroup])); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - $this->groupManager - ->expects($this->any()) - ->method('isAdmin') - ->with('subadmin') - ->will($this->returnValue(false)); - - $expected = new \OC\OCS\Result(null, 105, 'Cannot remove yourself from this group as you are a SubAdmin'); - $this->assertEquals($expected, $this->api->removeFromGroup(['userid' => 'subadmin', '_delete' => ['groupid' => 'subadmin']])); - } - - public function testRemoveFromGroupSuccessful() { - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('admin')); - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('admin') - ->will($this->returnValue($targetGroup)); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('AnotherUser') - ->will($this->returnValue($targetUser)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - $this->groupManager - ->expects($this->any()) - ->method('isAdmin') - ->with('admin') - ->will($this->returnValue(true)); - $targetGroup - ->expects($this->once()) - ->method('removeUser') - ->with($targetUser); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->removeFromGroup(['userid' => 'AnotherUser', '_delete' => ['groupid' => 'admin']])); - } - - public function testAddSubAdminWithNotExistingTargetUser() { - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('NotExistingUser') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, 101, 'User does not exist'); - $this->assertEquals($expected, $this->api->addSubAdmin(['userid' => 'NotExistingUser'])); - } - - public function testAddSubAdminWithNotExistingTargetGroup() { - $_POST['groupid'] = 'NotExistingGroup'; - - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('ExistingUser') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('NotExistingGroup') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, 102, 'Group:NotExistingGroup does not exist'); - $this->assertEquals($expected, $this->api->addSubAdmin(['userid' => 'ExistingUser'])); - } - - public function testAddSubAdminToAdminGroup() { - $_POST['groupid'] = 'ADmiN'; - - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('ExistingUser') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('ADmiN') - ->will($this->returnValue($targetGroup)); - - $expected = new \OC\OCS\Result(null, 103, 'Cannot create subadmins for admin group'); - $this->assertEquals($expected, $this->api->addSubAdmin(['userid' => 'ExistingUser'])); - } - - public function testAddSubAdminTwice() { - $_POST['groupid'] = 'TargetGroup'; - - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('ExistingUser') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('TargetGroup') - ->will($this->returnValue($targetGroup)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isSubAdminOfGroup') - ->with($targetUser, $targetGroup) - ->will($this->returnValue(true)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->addSubAdmin(['userid' => 'ExistingUser'])); - } - - public function testAddSubAdminSuccessful() { - $_POST['groupid'] = 'TargetGroup'; - - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('ExistingUser') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('TargetGroup') - ->will($this->returnValue($targetGroup)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isSubAdminOfGroup') - ->with($targetUser, $targetGroup) - ->will($this->returnValue(false)); - $subAdminManager - ->expects($this->once()) - ->method('createSubAdmin') - ->with($targetUser, $targetGroup) - ->will($this->returnValue(true)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->addSubAdmin(['userid' => 'ExistingUser'])); - } - - public function testAddSubAdminUnsuccessful() { - $_POST['groupid'] = 'TargetGroup'; - - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('ExistingUser') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('TargetGroup') - ->will($this->returnValue($targetGroup)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isSubAdminOfGroup') - ->with($targetUser, $targetGroup) - ->will($this->returnValue(false)); - $subAdminManager - ->expects($this->once()) - ->method('createSubAdmin') - ->with($targetUser, $targetGroup) - ->will($this->returnValue(false)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - - $expected = new \OC\OCS\Result(null, 103, 'Unknown error occurred'); - $this->assertEquals($expected, $this->api->addSubAdmin(['userid' => 'ExistingUser'])); - } - - public function testRemoveSubAdminNotExistingTargetUser() { - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('NotExistingUser') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, 101, 'User does not exist'); - $this->assertEquals($expected, $this->api->removeSubAdmin(['userid' => 'NotExistingUser', '_delete' => ['groupid' => 'GroupToDeleteFrom']])); - } - - public function testRemoveSubAdminNotExistingTargetGroup() { - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('ExistingUser') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('GroupToDeleteFrom') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, 101, 'Group does not exist'); - $this->assertEquals($expected, $this->api->removeSubAdmin(['userid' => 'ExistingUser', '_delete' => ['groupid' => 'GroupToDeleteFrom']])); - } - - public function testRemoveSubAdminFromNotASubadmin() { - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('ExistingUser') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('GroupToDeleteFrom') - ->will($this->returnValue($targetGroup)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isSubAdminOfGroup') - ->with($targetUser, $targetGroup) - ->will($this->returnValue(false)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - - $expected = new \OC\OCS\Result(null, 102, 'User is not a subadmin of this group'); - $this->assertEquals($expected, $this->api->removeSubAdmin(['userid' => 'ExistingUser', '_delete' => ['groupid' => 'GroupToDeleteFrom']])); - } - - public function testRemoveSubAdminSuccessful() { - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('ExistingUser') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('GroupToDeleteFrom') - ->will($this->returnValue($targetGroup)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isSubAdminOfGroup') - ->with($targetUser, $targetGroup) - ->will($this->returnValue(true)); - $subAdminManager - ->expects($this->once()) - ->method('deleteSubAdmin') - ->with($targetUser, $targetGroup) - ->will($this->returnValue(true)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->removeSubAdmin(['userid' => 'ExistingUser', '_delete' => ['groupid' => 'GroupToDeleteFrom']])); - } - - public function testRemoveSubAdminUnsuccessful() { - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('ExistingUser') - ->will($this->returnValue($targetUser)); - $this->groupManager - ->expects($this->once()) - ->method('get') - ->with('GroupToDeleteFrom') - ->will($this->returnValue($targetGroup)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('isSubAdminOfGroup') - ->with($targetUser, $targetGroup) - ->will($this->returnValue(true)); - $subAdminManager - ->expects($this->once()) - ->method('deleteSubAdmin') - ->with($targetUser, $targetGroup) - ->will($this->returnValue(false)); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - - $expected = new \OC\OCS\Result(null, 103, 'Unknown error occurred'); - $this->assertEquals($expected, $this->api->removeSubAdmin(['userid' => 'ExistingUser', '_delete' => ['groupid' => 'GroupToDeleteFrom']])); - } - - public function testGetUserSubAdminGroupsNotExistingTargetUser() { - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('RequestedUser') - ->will($this->returnValue(null)); - - $expected = new \OC\OCS\Result(null, 101, 'User does not exist'); - $this->assertEquals($expected, $this->api->getUserSubAdminGroups(['userid' => 'RequestedUser'])); - } - - public function testGetUserSubAdminGroupsWithGroups() { - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); - $targetGroup - ->expects($this->once()) - ->method('getGID') - ->will($this->returnValue('TargetGroup')); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('RequestedUser') - ->will($this->returnValue($targetUser)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('getSubAdminsGroups') - ->with($targetUser) - ->will($this->returnValue([$targetGroup])); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - - $expected = new \OC\OCS\Result(['TargetGroup'], 100); - $this->assertEquals($expected, $this->api->getUserSubAdminGroups(['userid' => 'RequestedUser'])); - } - - public function testGetUserSubAdminGroupsWithoutGroups() { - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('RequestedUser') - ->will($this->returnValue($targetUser)); - $subAdminManager = $this->getMockBuilder('OC\SubAdmin') - ->disableOriginalConstructor()->getMock(); - $subAdminManager - ->expects($this->once()) - ->method('getSubAdminsGroups') - ->with($targetUser) - ->will($this->returnValue([])); - $this->groupManager - ->expects($this->once()) - ->method('getSubAdmin') - ->will($this->returnValue($subAdminManager)); - - $expected = new \OC\OCS\Result(null, 102, 'Unknown error occurred'); - $this->assertEquals($expected, $this->api->getUserSubAdminGroups(['userid' => 'RequestedUser'])); - } - - public function testEnableUser() { - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetUser->expects($this->once()) - ->method('setEnabled') - ->with(true); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('RequestedUser') - ->will($this->returnValue($targetUser)); - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->exactly(2)) - ->method('getUID') - ->will($this->returnValue('admin')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->will($this->returnValue(true)); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->enableUser(['userid' => 'RequestedUser'])); - } - - public function testDisableUser() { - $targetUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $targetUser->expects($this->once()) - ->method('setEnabled') - ->with(false); - $this->userManager - ->expects($this->once()) - ->method('get') - ->with('RequestedUser') - ->will($this->returnValue($targetUser)); - $loggedInUser = $this->getMockBuilder('\OCP\IUser')->disableOriginalConstructor()->getMock(); - $loggedInUser - ->expects($this->exactly(2)) - ->method('getUID') - ->will($this->returnValue('admin')); - $this->userSession - ->expects($this->once()) - ->method('getUser') - ->will($this->returnValue($loggedInUser)); - $this->groupManager - ->expects($this->once()) - ->method('isAdmin') - ->will($this->returnValue(true)); - - $expected = new \OC\OCS\Result(null, 100); - $this->assertEquals($expected, $this->api->disableUser(['userid' => 'RequestedUser'])); - } -} -- cgit v1.2.3 From 092b767ef998a6afe2e01eb34aef1f8d21f6ec69 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Fri, 12 Aug 2016 10:27:08 +0200 Subject: Move Apps to OCSController --- apps/provisioning_api/appinfo/routes.php | 16 +-- apps/provisioning_api/lib/Apps.php | 111 ----------------- .../lib/Controller/AppsController.php | 123 +++++++++++++++++++ apps/provisioning_api/tests/AppsTest.php | 128 -------------------- .../tests/Controller/AppsControllerTest.php | 134 +++++++++++++++++++++ 5 files changed, 263 insertions(+), 249 deletions(-) delete mode 100644 apps/provisioning_api/lib/Apps.php create mode 100644 apps/provisioning_api/lib/Controller/AppsController.php delete mode 100644 apps/provisioning_api/tests/AppsTest.php create mode 100644 apps/provisioning_api/tests/Controller/AppsControllerTest.php (limited to 'apps') diff --git a/apps/provisioning_api/appinfo/routes.php b/apps/provisioning_api/appinfo/routes.php index 473ae2ff716..a7366a32a06 100644 --- a/apps/provisioning_api/appinfo/routes.php +++ b/apps/provisioning_api/appinfo/routes.php @@ -33,6 +33,12 @@ use OCP\API; $app = new \OCA\Provisioning_API\AppInfo\Application(); $app->registerRoutes($this, [ 'ocs' => [ + // Apps + ['root' => '/cloud', 'name' => 'Apps#getApps', 'url' => '/apps', 'verb' => 'GET'], + ['root' => '/cloud', 'name' => 'Apps#getAppInfo', 'url' => '/apps/{app}', 'verb' => 'GET'], + ['root' => '/cloud', 'name' => 'Apps#enable', 'url' => '/apps/{app}', 'verb' => 'POST'], + ['root' => '/cloud', 'name' => 'Apps#disable', 'url' => '/apps/{app}', 'verb' => 'DELETE'], + // Groups ['root' => '/cloud', 'name' => 'Groups#getGroups', 'url' => '/groups', 'verb' => 'GET'], ['root' => '/cloud', 'name' => 'Groups#getGroup', 'url' => '/groups/{groupId}', 'verb' => 'GET'], @@ -57,13 +63,3 @@ $app->registerRoutes($this, [ ], ]); - -// Apps -$apps = new Apps( - \OC::$server->getAppManager(), - \OC::$server->getOcsClient() -); -API::register('get', '/cloud/apps', [$apps, 'getApps'], 'provisioning_api', API::ADMIN_AUTH); -API::register('get', '/cloud/apps/{appid}', [$apps, 'getAppInfo'], 'provisioning_api', API::ADMIN_AUTH); -API::register('post', '/cloud/apps/{appid}', [$apps, 'enable'], 'provisioning_api', API::ADMIN_AUTH); -API::register('delete', '/cloud/apps/{appid}', [$apps, 'disable'], 'provisioning_api', API::ADMIN_AUTH); diff --git a/apps/provisioning_api/lib/Apps.php b/apps/provisioning_api/lib/Apps.php deleted file mode 100644 index f880e41905b..00000000000 --- a/apps/provisioning_api/lib/Apps.php +++ /dev/null @@ -1,111 +0,0 @@ - - * @author Morris Jobke - * @author Roeland Jago Douma - * @author Tom Needham - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Provisioning_API; - -use OC\OCSClient; -use \OC_App; - -class Apps { - /** @var \OCP\App\IAppManager */ - private $appManager; - /** @var OCSClient */ - private $ocsClient; - - /** - * @param \OCP\App\IAppManager $appManager - */ - public function __construct(\OCP\App\IAppManager $appManager, - OCSClient $ocsClient) { - $this->appManager = $appManager; - $this->ocsClient = $ocsClient; - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function getApps($parameters) { - $apps = OC_App::listAllApps(false, true, $this->ocsClient); - $list = []; - foreach($apps as $app) { - $list[] = $app['id']; - } - $filter = isset($_GET['filter']) ? $_GET['filter'] : false; - if($filter){ - switch($filter){ - case 'enabled': - return new \OC\OCS\Result(array('apps' => \OC_App::getEnabledApps())); - break; - case 'disabled': - $enabled = OC_App::getEnabledApps(); - return new \OC\OCS\Result(array('apps' => array_diff($list, $enabled))); - break; - default: - // Invalid filter variable - return new \OC\OCS\Result(null, 101); - break; - } - - } else { - return new \OC\OCS\Result(array('apps' => $list)); - } - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function getAppInfo($parameters) { - $app = $parameters['appid']; - $info = \OCP\App::getAppInfo($app); - if(!is_null($info)) { - return new \OC\OCS\Result(OC_App::getAppInfo($app)); - } else { - return new \OC\OCS\Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The request app was not found'); - } - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function enable($parameters) { - $app = $parameters['appid']; - $this->appManager->enableApp($app); - return new \OC\OCS\Result(null, 100); - } - - /** - * @param array $parameters - * @return \OC\OCS\Result - */ - public function disable($parameters) { - $app = $parameters['appid']; - $this->appManager->disableApp($app); - return new \OC\OCS\Result(null, 100); - } - -} diff --git a/apps/provisioning_api/lib/Controller/AppsController.php b/apps/provisioning_api/lib/Controller/AppsController.php new file mode 100644 index 00000000000..3821fc343ad --- /dev/null +++ b/apps/provisioning_api/lib/Controller/AppsController.php @@ -0,0 +1,123 @@ + + * @author Morris Jobke + * @author Roeland Jago Douma + * @author Tom Needham + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Provisioning_API\Controller; + +use OC\OCSClient; +use \OC_App; +use OCP\App\IAppManager; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSException; +use OCP\AppFramework\OCS\OCSNotFoundException; +use OCP\AppFramework\OCSController; +use OCP\IRequest; + +class AppsController extends OCSController { + /** @var \OCP\App\IAppManager */ + private $appManager; + /** @var OCSClient */ + private $ocsClient; + + /** + * @param string $appName + * @param IRequest $request + * @param IAppManager $appManager + * @param OCSClient $ocsClient + */ + public function __construct( + $appName, + IRequest $request, + IAppManager $appManager, + OCSClient $ocsClient + ) { + parent::__construct($appName, $request); + + $this->appManager = $appManager; + $this->ocsClient = $ocsClient; + } + + /** + * @param string $filter + * @return DataResponse + * @throws OCSException + */ + public function getApps($filter = null) { + $apps = OC_App::listAllApps(false, true, $this->ocsClient); + $list = []; + foreach($apps as $app) { + $list[] = $app['id']; + } + if($filter){ + switch($filter){ + case 'enabled': + return new DataResponse(['apps' => \OC_App::getEnabledApps()]); + break; + case 'disabled': + $enabled = OC_App::getEnabledApps(); + return new DataResponse(['apps' => array_diff($list, $enabled)]); + break; + default: + // Invalid filter variable + throw new OCSException('', 101); + } + + } else { + return new DataResponse(['apps' => $list]); + } + } + + /** + * @param string $app + * @return DataResponse + * @throws OCSNotFoundException + */ + public function getAppInfo($app) { + $info = \OCP\App::getAppInfo($app); + if(!is_null($info)) { + return new DataResponse(OC_App::getAppInfo($app)); + } else { + throw new OCSException('The request app was not found', \OCP\API::RESPOND_NOT_FOUND); + } + } + + /** + * @param string $app + * @return DataResponse + */ + public function enable($app) { + $this->appManager->enableApp($app); + return new DataResponse(); + } + + /** + * @param string $app + * @return DataResponse + */ + public function disable($app) { + $this->appManager->disableApp($app); + return new DataResponse(); + } + +} diff --git a/apps/provisioning_api/tests/AppsTest.php b/apps/provisioning_api/tests/AppsTest.php deleted file mode 100644 index 35808b15816..00000000000 --- a/apps/provisioning_api/tests/AppsTest.php +++ /dev/null @@ -1,128 +0,0 @@ - - * @author Lukas Reschke - * @author Morris Jobke - * @author Roeland Jago Douma - * @author Thomas Müller - * @author Tom Needham - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Provisioning_API\Tests; - - -use OC\OCSClient; -use OCA\Provisioning_API\Apps; -use OCP\API; -use OCP\App\IAppManager; -use OCP\IUserSession; - -/** - * Class AppsTest - * - * @group DB - * - * @package OCA\Provisioning_API\Tests - */ -class AppsTest extends TestCase { - /** @var IAppManager */ - private $appManager; - /** @var Apps */ - private $api; - /** @var IUserSession */ - private $userSession; - /** @var OCSClient|\PHPUnit_Framework_MockObject_MockObject */ - private $ocsClient; - - protected function setUp() { - parent::setUp(); - - $this->appManager = \OC::$server->getAppManager(); - $this->groupManager = \OC::$server->getGroupManager(); - $this->userSession = \OC::$server->getUserSession(); - $this->ocsClient = $this->getMockBuilder('OC\OCSClient') - ->disableOriginalConstructor() - ->getMock(); - - $this->api = new Apps($this->appManager, $this->ocsClient); - } - - public function testGetAppInfo() { - $result = $this->api->getAppInfo(['appid' => 'provisioning_api']); - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertTrue($result->succeeded()); - } - - public function testGetAppInfoOnBadAppID() { - $result = $this->api->getAppInfo(['appid' => 'not_provisioning_api']); - $this->assertInstanceOf('\OC\OCS\Result', $result); - $this->assertFalse($result->succeeded()); - $this->assertEquals(API::RESPOND_NOT_FOUND, $result->getStatusCode()); - } - - public function testGetApps() { - $this->ocsClient - ->expects($this->any()) - ->method($this->anything()) - ->will($this->returnValue(null)); - $user = $this->generateUsers(); - $this->groupManager->get('admin')->addUser($user); - $this->userSession->setUser($user); - - $result = $this->api->getApps([]); - - $this->assertTrue($result->succeeded()); - $data = $result->getData(); - $this->assertEquals(count(\OC_App::listAllApps(false, true, $this->ocsClient)), count($data['apps'])); - } - - public function testGetAppsEnabled() { - $_GET['filter'] = 'enabled'; - $result = $this->api->getApps(['filter' => 'enabled']); - $this->assertTrue($result->succeeded()); - $data = $result->getData(); - $this->assertEquals(count(\OC_App::getEnabledApps()), count($data['apps'])); - } - - public function testGetAppsDisabled() { - $this->ocsClient - ->expects($this->any()) - ->method($this->anything()) - ->will($this->returnValue(null)); - $_GET['filter'] = 'disabled'; - $result = $this->api->getApps(['filter' => 'disabled']); - $this->assertTrue($result->succeeded()); - $data = $result->getData(); - $apps = \OC_App::listAllApps(false, true, $this->ocsClient); - $list = array(); - foreach($apps as $app) { - $list[] = $app['id']; - } - $disabled = array_diff($list, \OC_App::getEnabledApps()); - $this->assertEquals(count($disabled), count($data['apps'])); - } - - public function testGetAppsInvalidFilter() { - $_GET['filter'] = 'foo'; - $result = $this->api->getApps([]); - $this->assertFalse($result->succeeded()); - $this->assertEquals(101, $result->getStatusCode()); - } -} diff --git a/apps/provisioning_api/tests/Controller/AppsControllerTest.php b/apps/provisioning_api/tests/Controller/AppsControllerTest.php new file mode 100644 index 00000000000..9ac4a8290e4 --- /dev/null +++ b/apps/provisioning_api/tests/Controller/AppsControllerTest.php @@ -0,0 +1,134 @@ + + * @author Lukas Reschke + * @author Morris Jobke + * @author Roeland Jago Douma + * @author Thomas Müller + * @author Tom Needham + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Provisioning_API\Tests\Controller; + + +use OC\OCSClient; +use OCA\Provisioning_API\Controller\AppsController; +use OCP\API; +use OCP\App\IAppManager; +use OCP\IUserSession; + +/** + * Class AppsTest + * + * @group DB + * + * @package OCA\Provisioning_API\Tests + */ +class AppsControllerTest extends \OCA\Provisioning_API\Tests\TestCase { + /** @var IAppManager */ + private $appManager; + /** @var AppsController */ + private $api; + /** @var IUserSession */ + private $userSession; + /** @var OCSClient|\PHPUnit_Framework_MockObject_MockObject */ + private $ocsClient; + + protected function setUp() { + parent::setUp(); + + $this->appManager = \OC::$server->getAppManager(); + $this->groupManager = \OC::$server->getGroupManager(); + $this->userSession = \OC::$server->getUserSession(); + $this->ocsClient = $this->getMockBuilder('OC\OCSClient') + ->disableOriginalConstructor() + ->getMock(); + + $request = $this->getMockBuilder('OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + + $this->api = new AppsController( + 'provisioning_api', + $request, + $this->appManager, + $this->ocsClient + ); + } + + public function testGetAppInfo() { + $result = $this->api->getAppInfo('provisioning_api'); + $expected = \OC_App::getAppInfo('provisioning_api'); + $this->assertEquals($expected, $result->getData()); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 998 + */ + public function testGetAppInfoOnBadAppID() { + $this->api->getAppInfo('not_provisioning_api'); + } + + public function testGetApps() { + $this->ocsClient + ->expects($this->any()) + ->method($this->anything()) + ->will($this->returnValue(null)); + $user = $this->generateUsers(); + $this->groupManager->get('admin')->addUser($user); + $this->userSession->setUser($user); + + $result = $this->api->getApps(); + + $data = $result->getData(); + $this->assertEquals(count(\OC_App::listAllApps(false, true, $this->ocsClient)), count($data['apps'])); + } + + public function testGetAppsEnabled() { + $result = $this->api->getApps('enabled'); + $data = $result->getData(); + $this->assertEquals(count(\OC_App::getEnabledApps()), count($data['apps'])); + } + + public function testGetAppsDisabled() { + $this->ocsClient + ->expects($this->any()) + ->method($this->anything()) + ->will($this->returnValue(null)); + $result = $this->api->getApps('disabled'); + $data = $result->getData(); + $apps = \OC_App::listAllApps(false, true, $this->ocsClient); + $list = array(); + foreach($apps as $app) { + $list[] = $app['id']; + } + $disabled = array_diff($list, \OC_App::getEnabledApps()); + $this->assertEquals(count($disabled), count($data['apps'])); + } + + /** + * @expectedException \OCP\AppFramework\OCS\OCSException + * @expectedExceptionCode 101 + */ + public function testGetAppsInvalidFilter() { + $this->api->getApps('foo'); + } +} -- cgit v1.2.3