aboutsummaryrefslogtreecommitdiffstats
path: root/apps/provisioning_api
diff options
context:
space:
mode:
Diffstat (limited to 'apps/provisioning_api')
-rw-r--r--apps/provisioning_api/appinfo/info.xml4
-rw-r--r--apps/provisioning_api/appinfo/routes.php1
-rw-r--r--apps/provisioning_api/lib/Controller/UsersController.php105
-rw-r--r--apps/provisioning_api/tests/Controller/UsersControllerTest.php404
-rw-r--r--apps/provisioning_api/tests/Middleware/ProvisioningApiMiddlewareTest.php122
5 files changed, 593 insertions, 43 deletions
diff --git a/apps/provisioning_api/appinfo/info.xml b/apps/provisioning_api/appinfo/info.xml
index 0ebcee9a7f7..e633df00bd9 100644
--- a/apps/provisioning_api/appinfo/info.xml
+++ b/apps/provisioning_api/appinfo/info.xml
@@ -17,12 +17,12 @@
<documentation>
<admin>admin-provisioning-api</admin>
</documentation>
- <version>1.1.0</version>
+ <version>1.2.0</version>
<namespace>Provisioning_API</namespace>
<types>
<prevent_group_restriction/>
</types>
<dependencies>
- <nextcloud min-version="11" max-version="11" />
+ <nextcloud min-version="12" max-version="12" />
</dependencies>
</info>
diff --git a/apps/provisioning_api/appinfo/routes.php b/apps/provisioning_api/appinfo/routes.php
index 04a34fba903..baa4e475be8 100644
--- a/apps/provisioning_api/appinfo/routes.php
+++ b/apps/provisioning_api/appinfo/routes.php
@@ -45,6 +45,7 @@ return [
['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#getCurrentUser', 'url' => '/user', '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'],
diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php
index cc1d63d2d34..1e8a767b33a 100644
--- a/apps/provisioning_api/lib/Controller/UsersController.php
+++ b/apps/provisioning_api/lib/Controller/UsersController.php
@@ -29,14 +29,15 @@
namespace OCA\Provisioning_API\Controller;
+use OC\Accounts\AccountManager;
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\IGroup;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IRequest;
@@ -53,6 +54,8 @@ class UsersController extends OCSController {
private $groupManager;
/** @var IUserSession */
private $userSession;
+ /** @var AccountManager */
+ private $accountManager;
/** @var ILogger */
private $logger;
@@ -63,6 +66,7 @@ class UsersController extends OCSController {
* @param IConfig $config
* @param IGroupManager $groupManager
* @param IUserSession $userSession
+ * @param AccountManager $accountManager
* @param ILogger $logger
*/
public function __construct($appName,
@@ -71,6 +75,7 @@ class UsersController extends OCSController {
IConfig $config,
IGroupManager $groupManager,
IUserSession $userSession,
+ AccountManager $accountManager,
ILogger $logger) {
parent::__construct($appName, $request);
@@ -78,6 +83,7 @@ class UsersController extends OCSController {
$this->config = $config;
$this->groupManager = $groupManager;
$this->userSession = $userSession;
+ $this->accountManager = $accountManager;
$this->logger = $logger;
}
@@ -107,7 +113,7 @@ class UsersController extends OCSController {
}
if($offset === null) {
- $offset = 0;
+ $offset = 0;
}
$users = [];
@@ -159,7 +165,7 @@ class UsersController extends OCSController {
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']);
@@ -188,6 +194,42 @@ class UsersController extends OCSController {
* @throws OCSException
*/
public function getUser($userId) {
+ $data = $this->getUserData($userId);
+ return new DataResponse($data);
+ }
+
+ /**
+ * @NoAdminRequired
+ * @NoSubAdminRequired
+ *
+ * gets user info from the currently logged in user
+ *
+ * @return DataResponse
+ * @throws OCSException
+ */
+ public function getCurrentUser() {
+ $user = $this->userSession->getUser();
+ if ($user) {
+ $data = $this->getUserData($user->getUID());
+ // rename "displayname" to "display-name" only for this call to keep
+ // the API stable.
+ $data['display-name'] = $data['displayname'];
+ unset($data['displayname']);
+ return new DataResponse($data);
+
+ }
+
+ throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
+ }
+
+ /**
+ * creates a array with all user data
+ *
+ * @param $userId
+ * @return array
+ * @throws OCSException
+ */
+ protected function getUserData($userId) {
$currentLoggedInUser = $this->userSession->getUser();
$data = [];
@@ -209,12 +251,19 @@ class UsersController extends OCSController {
}
}
+ $userAccount = $this->accountManager->getUser($targetUserObject);
+
// Find the data
+ $data['id'] = $targetUserObject->getUID();
$data['quota'] = $this->fillStorageInfo($userId);
$data['email'] = $targetUserObject->getEMailAddress();
$data['displayname'] = $targetUserObject->getDisplayName();
+ $data['phone'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_PHONE]['value'];
+ $data['address'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_ADDRESS]['value'];
+ $data['webpage'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_WEBSITE]['value'];
+ $data['twitter'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_TWITTER]['value'];
- return new DataResponse($data);
+ return $data;
}
/**
@@ -275,9 +324,9 @@ class UsersController extends OCSController {
break;
case 'quota':
$quota = $value;
- if($quota !== 'none' and $quota !== 'default') {
+ if($quota !== 'none' && $quota !== 'default') {
if (is_numeric($quota)) {
- $quota = floatval($quota);
+ $quota = (float) $quota;
} else {
$quota = \OCP\Util::computerFileSize($quota);
}
@@ -421,6 +470,7 @@ class UsersController extends OCSController {
// Looking up someone else
if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
// Return the group that the method caller is subadmin of for the user in question
+ /** @var IGroup[] $getSubAdminsGroups */
$getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
foreach ($getSubAdminsGroups as $key => $group) {
$getSubAdminsGroups[$key] = $group->getGID();
@@ -435,11 +485,13 @@ class UsersController extends OCSController {
throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
}
}
-
+
}
/**
* @PasswordConfirmationRequired
+ * @NoAdminRequired
+ *
* @param string $userId
* @param string $groupid
* @return DataResponse
@@ -459,6 +511,13 @@ class UsersController extends OCSController {
throw new OCSException('', 103);
}
+ // If they're not an admin, check they are a subadmin of the group in question
+ $loggedInUser = $this->userSession->getUser();
+ $subAdminManager = $this->groupManager->getSubAdmin();
+ if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
+ throw new OCSException('', 104);
+ }
+
// Add user to group
$group->addUser($targetUser);
return new DataResponse();
@@ -492,25 +551,33 @@ class UsersController extends OCSController {
// 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)) {
+ 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') {
+ 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();
- }
+ // Not an admin, so the user must be a subadmin of this group, but that is not allowed.
+ throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105);
+ }
- if(in_array($group->getGID(), $subAdminGroups, true)) {
- throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105);
- }
+ } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) {
+ /** @var IGroup[] $subAdminGroups */
+ $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
+ $subAdminGroups = array_map(function (IGroup $subAdminGroup) {
+ return $subAdminGroup->getGID();
+ }, $subAdminGroups);
+ $userGroups = $this->groupManager->getUserGroupIds($targetUser);
+ $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups);
+
+ if (count($userSubAdminGroups) <= 1) {
+ // Subadmin must not be able to remove a user from all their subadmin groups.
+ throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105);
}
}
diff --git a/apps/provisioning_api/tests/Controller/UsersControllerTest.php b/apps/provisioning_api/tests/Controller/UsersControllerTest.php
index e04ee86feae..a3e5bf6fde6 100644
--- a/apps/provisioning_api/tests/Controller/UsersControllerTest.php
+++ b/apps/provisioning_api/tests/Controller/UsersControllerTest.php
@@ -29,7 +29,12 @@
namespace OCA\Provisioning_API\Tests\Controller;
+use OC\Accounts\AccountManager;
use OCA\Provisioning_API\Controller\UsersController;
+use OCP\AppFramework\Http\DataResponse;
+use OCP\IGroup;
+use OCP\IRequest;
+use OCP\IUser;
use OCP\IUserManager;
use OCP\IConfig;
use OCP\IUserSession;
@@ -38,7 +43,7 @@ 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 */
@@ -51,6 +56,10 @@ class UsersControllerTest extends OriginalTest {
protected $logger;
/** @var UsersController | PHPUnit_Framework_MockObject_MockObject */
protected $api;
+ /** @var AccountManager | PHPUnit_Framework_MockObject_MockObject */
+ protected $accountManager;
+ /** @var IRequest | PHPUnit_Framework_MockObject_MockObject */
+ protected $request;
protected function tearDown() {
parent::tearDown();
@@ -74,17 +83,21 @@ class UsersControllerTest extends OriginalTest {
$this->logger = $this->getMockBuilder('OCP\ILogger')
->disableOriginalConstructor()
->getMock();
- $request = $this->getMockBuilder('OCP\IRequest')
+ $this->request = $this->getMockBuilder('OCP\IRequest')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->accountManager = $this->getMockBuilder(AccountManager::class)
->disableOriginalConstructor()
->getMock();
$this->api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController')
->setConstructorArgs([
'provisioning_api',
- $request,
+ $this->request,
$this->userManager,
$this->config,
$this->groupManager,
$this->userSession,
+ $this->accountManager,
$this->logger,
])
->setMethods(['fillStorageInfo'])
@@ -621,7 +634,7 @@ class UsersControllerTest extends OriginalTest {
$this->api->getUser('UserToGet');
}
- public function testGetUserAsAdmin() {
+ public function testGetUserDataAsAdmin() {
$loggedInUser = $this->getMockBuilder('OCP\IUser')
->disableOriginalConstructor()
->getMock();
@@ -649,6 +662,16 @@ class UsersControllerTest extends OriginalTest {
->method('isAdmin')
->with('admin')
->will($this->returnValue(true));
+ $this->accountManager->expects($this->any())->method('getUser')
+ ->with($targetUser)
+ ->willReturn(
+ [
+ AccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
+ AccountManager::PROPERTY_PHONE => ['value' => 'phone'],
+ AccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
+ AccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
+ ]
+ );
$this->config
->expects($this->at(0))
->method('getUserValue')
@@ -663,17 +686,26 @@ class UsersControllerTest extends OriginalTest {
->expects($this->once())
->method('getDisplayName')
->will($this->returnValue('Demo User'));
+ $targetUser
+ ->expects($this->once())
+ ->method('getUID')
+ ->will($this->returnValue('UID'));
$expected = [
+ 'id' => 'UID',
'enabled' => 'true',
'quota' => ['DummyValue'],
'email' => 'demo@owncloud.org',
'displayname' => 'Demo User',
+ 'phone' => 'phone',
+ 'address' => 'address',
+ 'webpage' => 'website',
+ 'twitter' => 'twitter'
];
- $this->assertEquals($expected, $this->api->getUser('UserToGet')->getData());
+ $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UserToGet']));
}
- public function testGetUserAsSubAdminAndUserIsAccessible() {
+ public function testGetUserDataAsSubAdminAndUserIsAccessible() {
$loggedInUser = $this->getMockBuilder('OCP\IUser')
->disableOriginalConstructor()
->getMock();
@@ -728,14 +760,33 @@ class UsersControllerTest extends OriginalTest {
->expects($this->once())
->method('getDisplayName')
->will($this->returnValue('Demo User'));
+ $targetUser
+ ->expects($this->once())
+ ->method('getUID')
+ ->will($this->returnValue('UID'));
+ $this->accountManager->expects($this->any())->method('getUser')
+ ->with($targetUser)
+ ->willReturn(
+ [
+ AccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
+ AccountManager::PROPERTY_PHONE => ['value' => 'phone'],
+ AccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
+ AccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
+ ]
+ );
$expected = [
+ 'id' => 'UID',
'enabled' => 'true',
'quota' => ['DummyValue'],
'email' => 'demo@owncloud.org',
'displayname' => 'Demo User',
+ 'phone' => 'phone',
+ 'address' => 'address',
+ 'webpage' => 'website',
+ 'twitter' => 'twitter'
];
- $this->assertEquals($expected, $this->api->getUser('UserToGet')->getData());
+ $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UserToGet']));
}
@@ -743,7 +794,7 @@ class UsersControllerTest extends OriginalTest {
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 997
*/
- public function testGetUserAsSubAdminAndUserIsNotAccessible() {
+ public function testGetUserDataAsSubAdminAndUserIsNotAccessible() {
$loggedInUser = $this->getMockBuilder('OCP\IUser')
->disableOriginalConstructor()
->getMock();
@@ -781,10 +832,10 @@ class UsersControllerTest extends OriginalTest {
->method('getSubAdmin')
->will($this->returnValue($subAdminManager));
- $this->api->getUser('UserToGet');
+ $this->invokePrivate($this->api, 'getUserData', ['UserToGet']);
}
- public function testGetUserAsSubAdminSelfLookup() {
+ public function testGetUserDataAsSubAdminSelfLookup() {
$loggedInUser = $this->getMockBuilder('OCP\IUser')
->disableOriginalConstructor()
->getMock();
@@ -834,13 +885,32 @@ class UsersControllerTest extends OriginalTest {
->expects($this->once())
->method('getEMailAddress')
->will($this->returnValue('subadmin@owncloud.org'));
+ $targetUser
+ ->expects($this->once())
+ ->method('getUID')
+ ->will($this->returnValue('UID'));
+ $this->accountManager->expects($this->any())->method('getUser')
+ ->with($targetUser)
+ ->willReturn(
+ [
+ AccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
+ AccountManager::PROPERTY_PHONE => ['value' => 'phone'],
+ AccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
+ AccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
+ ]
+ );
$expected = [
+ 'id' => 'UID',
'quota' => ['DummyValue'],
'email' => 'subadmin@owncloud.org',
'displayname' => 'Subadmin User',
+ 'phone' => 'phone',
+ 'address' => 'address',
+ 'webpage' => 'website',
+ 'twitter' => 'twitter'
];
- $this->assertEquals($expected, $this->api->getUser('subadmin')->getData());
+ $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['subadmin']));
}
public function testEditUserRegularUserSelfEditChangeDisplayName() {
@@ -1598,11 +1668,10 @@ class UsersControllerTest extends OriginalTest {
* @expectedExceptionCode 102
*/
public function testAddToGroupWithTargetGroupNotExisting() {
- $this->groupManager
- ->expects($this->once())
+ $this->groupManager->expects($this->once())
->method('get')
->with('GroupToAddTo')
- ->will($this->returnValue(null));
+ ->willReturn(null);
$this->api->addToGroup('TargetUser', 'GroupToAddTo');
}
@@ -1620,16 +1689,149 @@ class UsersControllerTest extends OriginalTest {
* @expectedExceptionCode 103
*/
public function testAddToGroupWithTargetUserNotExisting() {
- $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
- $this->groupManager
- ->expects($this->once())
+ $targetGroup = $this->createMock(IGroup::class);
+ $this->groupManager->expects($this->once())
->method('get')
->with('GroupToAddTo')
- ->will($this->returnValue($targetGroup));
+ ->willReturn($targetGroup);
+
+ $this->api->addToGroup('TargetUser', 'GroupToAddTo');
+ }
+
+ /**
+ * @expectedException \OCP\AppFramework\OCS\OCSException
+ * @expectedExceptionCode 104
+ */
+ public function testAddToGroupNoSubadmin() {
+ $targetUser = $this->createMock(IUser::class);
+ $loggedInUser = $this->createMock(IUser::class);
+ $loggedInUser->expects($this->once())
+ ->method('getUID')
+ ->willReturn('subadmin');
+
+ $targetGroup = $this->createMock(IGroup::class);
+ $targetGroup->expects($this->never())
+ ->method('addUser')
+ ->with($targetUser);
+
+ $this->groupManager->expects($this->once())
+ ->method('get')
+ ->with('GroupToAddTo')
+ ->willReturn($targetGroup);
+
+
+ $subAdminManager = $this->createMock(\OC\SubAdmin::class);
+ $subAdminManager->expects($this->once())
+ ->method('isSubAdminOfGroup')
+ ->with($loggedInUser, $targetGroup)
+ ->willReturn(false);
+
+ $this->groupManager->expects($this->once())
+ ->method('getSubAdmin')
+ ->willReturn($subAdminManager);
+ $this->groupManager->expects($this->once())
+ ->method('isAdmin')
+ ->with('subadmin')
+ ->willReturn(false);
+
+ $this->userManager->expects($this->once())
+ ->method('get')
+ ->with('TargetUser')
+ ->willReturn($targetUser);
+
+ $this->userSession->expects($this->once())
+ ->method('getUser')
+ ->willReturn($loggedInUser);
$this->api->addToGroup('TargetUser', 'GroupToAddTo');
}
+ public function testAddToGroupSuccessAsSubadmin() {
+ $targetUser = $this->createMock(IUser::class);
+ $loggedInUser = $this->createMock(IUser::class);
+ $loggedInUser->expects($this->once())
+ ->method('getUID')
+ ->willReturn('subadmin');
+
+ $targetGroup = $this->createMock(IGroup::class);
+ $targetGroup->expects($this->once())
+ ->method('addUser')
+ ->with($targetUser);
+
+ $this->groupManager->expects($this->once())
+ ->method('get')
+ ->with('GroupToAddTo')
+ ->willReturn($targetGroup);
+
+
+ $subAdminManager = $this->createMock(\OC\SubAdmin::class);
+ $subAdminManager->expects($this->once())
+ ->method('isSubAdminOfGroup')
+ ->with($loggedInUser, $targetGroup)
+ ->willReturn(true);
+
+ $this->groupManager->expects($this->once())
+ ->method('getSubAdmin')
+ ->willReturn($subAdminManager);
+ $this->groupManager->expects($this->once())
+ ->method('isAdmin')
+ ->with('subadmin')
+ ->willReturn(false);
+
+ $this->userManager->expects($this->once())
+ ->method('get')
+ ->with('TargetUser')
+ ->willReturn($targetUser);
+
+ $this->userSession->expects($this->once())
+ ->method('getUser')
+ ->willReturn($loggedInUser);
+
+ $this->assertEquals(new DataResponse(), $this->api->addToGroup('TargetUser', 'GroupToAddTo'));
+ }
+
+ public function testAddToGroupSuccessAsAdmin() {
+ $targetUser = $this->createMock(IUser::class);
+ $loggedInUser = $this->createMock(IUser::class);
+ $loggedInUser->expects($this->once())
+ ->method('getUID')
+ ->willReturn('admin');
+
+ $targetGroup = $this->createMock(IGroup::class);
+ $targetGroup->expects($this->once())
+ ->method('addUser')
+ ->with($targetUser);
+
+ $this->groupManager->expects($this->once())
+ ->method('get')
+ ->with('GroupToAddTo')
+ ->willReturn($targetGroup);
+
+
+ $subAdminManager = $this->createMock(\OC\SubAdmin::class);
+ $subAdminManager->expects($this->never())
+ ->method('isSubAdminOfGroup');
+
+ $this->groupManager->expects($this->once())
+ ->method('getSubAdmin')
+ ->willReturn($subAdminManager);
+ $this->groupManager->expects($this->once())
+ ->method('isAdmin')
+ ->with('admin')
+ ->willReturn(true);
+
+ $this->userManager->expects($this->once())
+ ->method('get')
+ ->with('TargetUser')
+ ->willReturn($targetUser);
+
+ $this->userSession->expects($this->once())
+ ->method('getUser')
+ ->willReturn($loggedInUser);
+
+ $this->assertEquals(new DataResponse(), $this->api->addToGroup('TargetUser', 'GroupToAddTo'));
+ }
+
/**
* @expectedException \OCP\AppFramework\OCS\OCSException
* @expectedExceptionCode 101
@@ -1813,22 +2015,79 @@ class UsersControllerTest extends OriginalTest {
->method('isSubAdminofGroup')
->with($loggedInUser, $targetGroup)
->will($this->returnValue(true));
+ $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');
+ }
+
+ /**
+ * @expectedException \OCP\AppFramework\OCS\OCSException
+ * @expectedExceptionCode 105
+ * @expectedExceptionMessage Cannot remove user from this group as this is the only remaining group you are a SubAdmin of
+ */
+ public function testRemoveFromGroupAsSubAdminFromLastSubAdminGroup() {
+ $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('AnotherUser')
+ ->will($this->returnValue($targetUser));
+ $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
+ ->disableOriginalConstructor()->getMock();
$subAdminManager
->expects($this->once())
- ->method('getSubAdminsGroups')
- ->with($loggedInUser)
- ->will($this->returnValue([$targetGroup]));
+ ->method('isSubAdminofGroup')
+ ->with($loggedInUser, $targetGroup)
+ ->will($this->returnValue(true));
$this->groupManager
->expects($this->once())
->method('getSubAdmin')
->will($this->returnValue($subAdminManager));
+ $subAdminManager
+ ->expects($this->once())
+ ->method('getSubAdminsGroups')
+ ->with($loggedInUser)
+ ->will($this->returnValue([$targetGroup]));
+
$this->groupManager
->expects($this->any())
->method('isAdmin')
->with('subadmin')
->will($this->returnValue(false));
+ $this->groupManager
+ ->expects($this->once())
+ ->method('getUserGroupIds')
+ ->with($targetUser)
+ ->willReturn(['subadmin', 'other group']);
- $this->api->removeFromGroup('subadmin', 'subadmin');
+ $this->api->removeFromGroup('AnotherUser', 'subadmin');
}
public function testRemoveFromGroupSuccessful() {
@@ -2293,4 +2552,105 @@ class UsersControllerTest extends OriginalTest {
$this->assertEquals([], $this->api->disableUser('RequestedUser')->getData());
}
+
+ public function testGetCurrentUserLoggedIn() {
+
+ $user = $this->getMock(IUser::class);
+ $user->expects($this->once())->method('getUID')->willReturn('UID');
+
+ $this->userSession->expects($this->once())->method('getUser')
+ ->willReturn($user);
+
+ /** @var UsersController | PHPUnit_Framework_MockObject_MockObject $api */
+ $api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController')
+ ->setConstructorArgs([
+ 'provisioning_api',
+ $this->request,
+ $this->userManager,
+ $this->config,
+ $this->groupManager,
+ $this->userSession,
+ $this->accountManager,
+ $this->logger,
+ ])
+ ->setMethods(['getUserData'])
+ ->getMock();
+
+ $api->expects($this->once())->method('getUserData')->with('UID')
+ ->willReturn(
+ [
+ 'id' => 'UID',
+ 'enabled' => 'true',
+ 'quota' => ['DummyValue'],
+ 'email' => 'demo@owncloud.org',
+ 'displayname' => 'Demo User',
+ 'phone' => 'phone',
+ 'address' => 'address',
+ 'webpage' => 'website',
+ 'twitter' => 'twitter'
+ ]
+ );
+
+ $expected = [
+ 'id' => 'UID',
+ 'enabled' => 'true',
+ 'quota' => ['DummyValue'],
+ 'email' => 'demo@owncloud.org',
+ 'phone' => 'phone',
+ 'address' => 'address',
+ 'webpage' => 'website',
+ 'twitter' => 'twitter',
+ 'display-name' => 'Demo User'
+ ];
+
+ $this->assertSame($expected, $api->getCurrentUser()->getData());
+ }
+
+ /**
+ * @expectedException \OCP\AppFramework\OCS\OCSException
+ */
+ public function testGetCurrentUserNotLoggedIn() {
+
+ $this->userSession->expects($this->once())->method('getUser')
+ ->willReturn(null);
+
+ $this->api->getCurrentUser();
+ }
+
+
+ public function testGetUser() {
+ /** @var UsersController | PHPUnit_Framework_MockObject_MockObject $api */
+ $api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController')
+ ->setConstructorArgs([
+ 'provisioning_api',
+ $this->request,
+ $this->userManager,
+ $this->config,
+ $this->groupManager,
+ $this->userSession,
+ $this->accountManager,
+ $this->logger,
+ ])
+ ->setMethods(['getUserData'])
+ ->getMock();
+
+ $expected = [
+ 'id' => 'UID',
+ 'enabled' => 'true',
+ 'quota' => ['DummyValue'],
+ 'email' => 'demo@owncloud.org',
+ 'phone' => 'phone',
+ 'address' => 'address',
+ 'webpage' => 'website',
+ 'twitter' => 'twitter',
+ 'displayname' => 'Demo User'
+ ];
+
+ $api->expects($this->once())->method('getUserData')
+ ->with('uid')
+ ->willReturn($expected);
+
+ $this->assertSame($expected, $api->getUser('uid')->getData());
+ }
+
}
diff --git a/apps/provisioning_api/tests/Middleware/ProvisioningApiMiddlewareTest.php b/apps/provisioning_api/tests/Middleware/ProvisioningApiMiddlewareTest.php
new file mode 100644
index 00000000000..2d8b79842f2
--- /dev/null
+++ b/apps/provisioning_api/tests/Middleware/ProvisioningApiMiddlewareTest.php
@@ -0,0 +1,122 @@
+<?php
+/**
+ * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+namespace OCA\Provisioning_API\Tests\Middleware;
+
+use OCA\Provisioning_API\Middleware\Exceptions\NotSubAdminException;
+use OCA\Provisioning_API\Middleware\ProvisioningApiMiddleware;
+use OCP\AppFramework\Controller;
+use OCP\AppFramework\OCS\OCSException;
+use OCP\AppFramework\Utility\IControllerMethodReflector;
+use Test\TestCase;
+
+class ProvisioningApiMiddlewareTest extends TestCase {
+
+ /** @var IControllerMethodReflector|\PHPUnit_Framework_MockObject_MockObject */
+ private $reflector;
+
+ public function setUp() {
+ parent::setUp();
+
+ $this->reflector = $this->createMock(IControllerMethodReflector::class);
+ }
+
+ public function dataAnnotation() {
+ return [
+ [false, false, false, false],
+ [false, false, true, false],
+ [false, true, true, false],
+ [ true, false, false, true],
+ [ true, false, true, false],
+ [ true, true, false, false],
+ [ true, true, true, false],
+ ];
+ }
+
+ /**
+ * @dataProvider dataAnnotation
+ *
+ * @param bool $subadminRequired
+ * @param bool $isAdmin
+ * @param bool $isSubAdmin
+ * @param bool $shouldThrowException
+ */
+ public function testBeforeController($subadminRequired, $isAdmin, $isSubAdmin, $shouldThrowException) {
+ $middleware = new ProvisioningApiMiddleware(
+ $this->reflector,
+ $isAdmin,
+ $isSubAdmin
+ );
+
+ $this->reflector->method('hasAnnotation')
+ ->with('NoSubAdminRequired')
+ ->willReturn(!$subadminRequired);
+
+ try {
+ $middleware->beforeController(
+ $this->createMock(Controller::class),
+ 'myMethod'
+ );
+ $this->assertFalse($shouldThrowException);
+ } catch (NotSubAdminException $e) {
+ $this->assertTrue($shouldThrowException);
+ }
+ }
+
+ public function dataAfterException() {
+ return [
+ [new NotSubAdminException(), false],
+ [new \Exception('test', 42), true],
+ ];
+ }
+
+ /**
+ * @dataProvider dataAfterException
+ *
+ * @param \Exception $e
+ * @param bool $forwared
+ */
+ public function testAfterException(\Exception $exception, $forwared) {
+ $middleware = new ProvisioningApiMiddleware(
+ $this->reflector,
+ false,
+ false
+ );
+
+ try {
+ $middleware->afterException(
+ $this->createMock(Controller::class),
+ 'myMethod',
+ $exception
+ );
+ $this->fail();
+ } catch (OCSException $e) {
+ $this->assertFalse($forwared);
+ $this->assertSame($exception->getMessage(), $e->getMessage());
+ $this->assertSame(\OCP\API::RESPOND_UNAUTHORISED, $e->getCode());
+ } catch (\Exception $e) {
+ $this->assertTrue($forwared);
+ $this->assertSame($exception, $e);
+ }
+
+ }
+}