summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
Diffstat (limited to 'apps')
-rw-r--r--apps/files/templates/appnavigation.php4
-rw-r--r--apps/files_sharing/api/ocssharewrapper.php14
-rw-r--r--apps/files_sharing/api/share20ocs.php100
-rw-r--r--apps/files_sharing/tests/api/share20ocstest.php259
-rw-r--r--apps/provisioning_api/appinfo/routes.php2
-rw-r--r--apps/provisioning_api/lib/users.php45
-rw-r--r--apps/provisioning_api/tests/userstest.php381
7 files changed, 768 insertions, 37 deletions
diff --git a/apps/files/templates/appnavigation.php b/apps/files/templates/appnavigation.php
index 512300e3a58..d05a02ee7db 100644
--- a/apps/files/templates/appnavigation.php
+++ b/apps/files/templates/appnavigation.php
@@ -16,9 +16,7 @@
</button>
</div>
<div id="app-settings-content">
- <h2>
- <label for="webdavurl"><?php p($l->t('WebDAV'));?></label>
- </h2>
+ <label for="webdavurl"><?php p($l->t('WebDAV'));?></label>
<input id="webdavurl" type="text" readonly="readonly" value="<?php p(\OCP\Util::linkToRemote('webdav')); ?>" />
<em><?php print_unescaped($l->t('Use this address to <a href="%s" target="_blank">access your Files via WebDAV</a>', array(link_to_docs('user-webdav'))));?></em>
</div>
diff --git a/apps/files_sharing/api/ocssharewrapper.php b/apps/files_sharing/api/ocssharewrapper.php
index 8c0d8f7d150..3ce2901dfb4 100644
--- a/apps/files_sharing/api/ocssharewrapper.php
+++ b/apps/files_sharing/api/ocssharewrapper.php
@@ -35,15 +35,16 @@ class OCSShareWrapper {
\OC::$server->getUserFolder(),
new \OC\Share20\DefaultShareProvider(
\OC::$server->getDatabaseConnection(),
- \OC::$server->getUserManager(),
- \OC::$server->getGroupManager(),
- \OC::$server->getUserFolder()
+ \OC::$server->getUserManager(),
+ \OC::$server->getGroupManager(),
+ \OC::$server->getUserFolder()
)
),
\OC::$server->getGroupManager(),
\OC::$server->getUserManager(),
\OC::$server->getRequest(),
- \OC::$server->getUserFolder());
+ \OC::$server->getUserFolder(),
+ \OC::$server->getURLGenerator());
}
public function getAllShares($params) {
@@ -55,7 +56,8 @@ class OCSShareWrapper {
}
public function getShare($params) {
- return \OCA\Files_Sharing\API\Local::getShare($params);
+ $id = $params['id'];
+ return $this->getShare20OCS()->getShare($id);
}
public function updateShare($params) {
@@ -63,7 +65,7 @@ class OCSShareWrapper {
}
public function deleteShare($params) {
- $id = (int)$params['id'];
+ $id = $params['id'];
return $this->getShare20OCS()->deleteShare($id);
}
}
diff --git a/apps/files_sharing/api/share20ocs.php b/apps/files_sharing/api/share20ocs.php
index 8a7f90c0023..aaf5a3c72b6 100644
--- a/apps/files_sharing/api/share20ocs.php
+++ b/apps/files_sharing/api/share20ocs.php
@@ -20,39 +20,125 @@
*/
namespace OCA\Files_Sharing\API;
+use OC\Share20\IShare;
+
class Share20OCS {
- /** @var OC\Share20\Manager */
+ /** @var \OC\Share20\Manager */
private $shareManager;
- /** @var OCP\IGroupManager */
+ /** @var \OCP\IGroupManager */
private $groupManager;
- /** @var OCP\IUserManager */
+ /** @var \OCP\IUserManager */
private $userManager;
- /** @var OCP\IRequest */
+ /** @var \OCP\IRequest */
private $request;
- /** @var OCP\Files\Folder */
+ /** @var \OCP\Files\Folder */
private $userFolder;
public function __construct(\OC\Share20\Manager $shareManager,
\OCP\IGroupManager $groupManager,
\OCP\IUserManager $userManager,
\OCP\IRequest $request,
- \OCP\Files\Folder $userFolder) {
+ \OCP\Files\Folder $userFolder,
+ \OCP\IURLGenerator $urlGenerator) {
$this->shareManager = $shareManager;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->request = $request;
$this->userFolder = $userFolder;
+ $this->urlGenerator = $urlGenerator;
+ }
+
+ /**
+ * Convert an IShare to an array for OCS output
+ *
+ * @param IShare $share
+ * @return array
+ */
+ protected function formatShare($share) {
+ $result = [
+ 'id' => $share->getId(),
+ 'share_type' => $share->getShareType(),
+ 'uid_owner' => $share->getSharedBy()->getUID(),
+ 'displayname_owner' => $share->getSharedBy()->getDisplayName(),
+ 'permissions' => $share->getPermissions(),
+ 'stime' => $share->getShareTime(),
+ 'parent' => $share->getParent(),
+ 'expiration' => null,
+ 'token' => null,
+ ];
+
+ $path = $share->getPath();
+ $result['path'] = $this->userFolder->getRelativePath($path->getPath());
+ if ($path instanceOf \OCP\Files\Folder) {
+ $result['item_type'] = 'folder';
+ } else {
+ $result['item_type'] = 'file';
+ }
+ $result['storage_id'] = $path->getStorage()->getId();
+ $result['storage'] = \OC\Files\Cache\Storage::getNumericStorageId($path->getStorage()->getId());
+ $result['item_source'] = $path->getId();
+ $result['file_source'] = $path->getId();
+ $result['file_parent'] = $path->getParent()->getId();
+ $result['file_target'] = $share->getTarget();
+
+ if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
+ $sharedWith = $share->getSharedWith();
+ $result['share_with'] = $sharedWith->getUID();
+ $result['share_with_displayname'] = $sharedWith->getDisplayName();
+ } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
+ $sharedWith = $share->getSharedWith();
+ $result['share_with'] = $sharedWith->getGID();
+ $result['share_with_displayname'] = $sharedWith->getGID();
+ } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
+
+ $result['share_with'] = $share->getPassword();
+ $result['share_with_displayname'] = $share->getPassword();
+
+ $result['token'] = $share->getToken();
+ $result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]);
+
+ $expiration = $share->getExpirationDate();
+ if ($expiration !== null) {
+ $result['expiration'] = $expiration->format('Y-m-d 00:00:00');
+ }
+
+ } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
+ $result['share_with'] = $share->getSharedWith();
+ $result['share_with_displayname'] = $share->getSharedWith();
+ $result['token'] = $share->getToken();
+ }
+
+ $result['mail_send'] = $share->getMailSend() ? 1 : 0;
+
+ return $result;
+ }
+
+ /**
+ * Get a specific share by id
+ *
+ * @param string $id
+ * @return \OC_OCS_Result
+ */
+ public function getShare($id) {
+ try {
+ $share = $this->shareManager->getShareById($id);
+ } catch (\OC\Share20\Exception\ShareNotFound $e) {
+ return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.');
+ }
+
+ $share = $this->formatShare($share);
+ return new \OC_OCS_Result($share);
}
/**
* Delete a share
*
- * @param int $id
+ * @param string $id
* @return \OC_OCS_Result
*/
public function deleteShare($id) {
diff --git a/apps/files_sharing/tests/api/share20ocstest.php b/apps/files_sharing/tests/api/share20ocstest.php
index 9c4377a2a7f..f74585eb47d 100644
--- a/apps/files_sharing/tests/api/share20ocstest.php
+++ b/apps/files_sharing/tests/api/share20ocstest.php
@@ -39,6 +39,9 @@ class Share20OCSTest extends \Test\TestCase {
/** @var OCP\Files\Folder */
private $userFolder;
+ /** @var OCP\IURLGenerator */
+ private $urlGenerator;
+
/** @var OCS */
private $ocs;
@@ -46,24 +49,18 @@ class Share20OCSTest extends \Test\TestCase {
$this->shareManager = $this->getMockBuilder('OC\Share20\Manager')
->disableOriginalConstructor()
->getMock();
- $this->groupManager = $this->getMockBuilder('OCP\IGroupManager')
- ->disableOriginalConstructor()
- ->getMock();
- $this->userManager = $this->getMockBuilder('OCP\IUserManager')
- ->disableOriginalConstructor()
- ->getMock();
- $this->request = $this->getMockBuilder('OCP\IRequest')
- ->disableOriginalConstructor()
- ->getMock();
- $this->userFolder = $this->getMockBuilder('OCP\Files\Folder')
- ->disableOriginalConstructor()
- ->getMock();
+ $this->groupManager = $this->getMock('OCP\IGroupManager');
+ $this->userManager = $this->getMock('OCP\IUserManager');
+ $this->request = $this->getMock('OCP\IRequest');
+ $this->userFolder = $this->getMock('OCP\Files\Folder');
+ $this->urlGenerator = $this->getMock('OCP\IURLGenerator');
$this->ocs = new Share20OCS($this->shareManager,
$this->groupManager,
$this->userManager,
$this->request,
- $this->userFolder);
+ $this->userFolder,
+ $this->urlGenerator);
}
public function testDeleteShareShareNotFound() {
@@ -110,4 +107,240 @@ class Share20OCSTest extends \Test\TestCase {
$expected = new \OC_OCS_Result();
$this->assertEquals($expected, $this->ocs->deleteShare(42));
}
+
+ public function testGetGetShareNotExists() {
+ $this->shareManager
+ ->expects($this->once())
+ ->method('getShareById')
+ ->with(42)
+ ->will($this->throwException(new \OC\Share20\Exception\ShareNotFound()));
+
+ $expected = new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.');
+ $this->assertEquals($expected, $this->ocs->getShare(42));
+ }
+
+ public function createShare($id, $shareType, $sharedWith, $sharedBy, $path, $permissions,
+ $shareTime, $expiration, $parent, $target, $mail_send, $token=null,
+ $password=null) {
+ $share = $this->getMock('OC\Share20\IShare');
+ $share->method('getId')->willReturn($id);
+ $share->method('getShareType')->willReturn($shareType);
+ $share->method('getSharedWith')->willReturn($sharedWith);
+ $share->method('getSharedBy')->willReturn($sharedBy);
+ $share->method('getPath')->willReturn($path);
+ $share->method('getPermissions')->willReturn($permissions);
+ $share->method('getShareTime')->willReturn($shareTime);
+ $share->method('getExpirationDate')->willReturn($expiration);
+ $share->method('getParent')->willReturn($parent);
+ $share->method('getTarget')->willReturn($target);
+ $share->method('getMailSend')->willReturn($mail_send);
+ $share->method('getToken')->willReturn($token);
+ $share->method('getPassword')->willReturn($password);
+
+ return $share;
+ }
+
+ public function dataGetShare() {
+ $data = [];
+
+ $owner = $this->getMock('OCP\IUser');
+ $owner->method('getUID')->willReturn('ownerId');
+ $owner->method('getDisplayName')->willReturn('ownerDisplay');
+
+ $user = $this->getMock('OCP\IUser');
+ $user->method('getUID')->willReturn('userId');
+ $user->method('getDisplayName')->willReturn('userDisplay');
+
+ $group = $this->getMock('OCP\IGroup');
+ $group->method('getGID')->willReturn('groupId');
+
+ $storage = $this->getMock('OCP\Files\Storage');
+ $storage->method('getId')->willReturn('STORAGE');
+
+ $parentFolder = $this->getMock('OCP\Files\Folder');
+ $parentFolder->method('getId')->willReturn(3);
+
+ $file = $this->getMock('OCP\Files\File');
+ $file->method('getId')->willReturn(1);
+ $file->method('getPath')->willReturn('file');
+ $file->method('getStorage')->willReturn($storage);
+ $file->method('getParent')->willReturn($parentFolder);
+
+ $folder = $this->getMock('OCP\Files\Folder');
+ $folder->method('getId')->willReturn(2);
+ $folder->method('getPath')->willReturn('folder');
+ $folder->method('getStorage')->willReturn($storage);
+ $folder->method('getParent')->willReturn($parentFolder);
+
+ // File shared with user
+ $share = $this->createShare(100,
+ \OCP\Share::SHARE_TYPE_USER,
+ $user,
+ $owner,
+ $file,
+ 4,
+ 5,
+ null,
+ 6,
+ 'target',
+ 0);
+ $expected = [
+ 'id' => 100,
+ 'share_type' => \OCP\Share::SHARE_TYPE_USER,
+ 'share_with' => 'userId',
+ 'share_with_displayname' => 'userDisplay',
+ 'uid_owner' => 'ownerId',
+ 'displayname_owner' => 'ownerDisplay',
+ 'item_type' => 'file',
+ 'item_source' => 1,
+ 'file_source' => 1,
+ 'file_target' => 'target',
+ 'file_parent' => 3,
+ 'token' => null,
+ 'expiration' => null,
+ 'permissions' => 4,
+ 'stime' => 5,
+ 'parent' => 6,
+ 'storage_id' => 'STORAGE',
+ 'path' => 'file',
+ 'storage' => null, // HACK around static function
+ 'mail_send' => 0,
+ ];
+ $data[] = [$share, $expected];
+
+ // Folder shared with group
+ $share = $this->createShare(101,
+ \OCP\Share::SHARE_TYPE_GROUP,
+ $group,
+ $owner,
+ $folder,
+ 4,
+ 5,
+ null,
+ 6,
+ 'target',
+ 0);
+ $expected = [
+ 'id' => 101,
+ 'share_type' => \OCP\Share::SHARE_TYPE_GROUP,
+ 'share_with' => 'groupId',
+ 'share_with_displayname' => 'groupId',
+ 'uid_owner' => 'ownerId',
+ 'displayname_owner' => 'ownerDisplay',
+ 'item_type' => 'folder',
+ 'item_source' => 2,
+ 'file_source' => 2,
+ 'file_target' => 'target',
+ 'file_parent' => 3,
+ 'token' => null,
+ 'expiration' => null,
+ 'permissions' => 4,
+ 'stime' => 5,
+ 'parent' => 6,
+ 'storage_id' => 'STORAGE',
+ 'path' => 'folder',
+ 'storage' => null, // HACK around static function
+ 'mail_send' => 0,
+ ];
+ $data[] = [$share, $expected];
+
+ // Folder shared with remote
+ $share = $this->createShare(101,
+ \OCP\Share::SHARE_TYPE_REMOTE,
+ 'user@remote.com',
+ $owner,
+ $folder,
+ 4,
+ 5,
+ null,
+ 6,
+ 'target',
+ 0);
+ $expected = [
+ 'id' => 101,
+ 'share_type' => \OCP\Share::SHARE_TYPE_REMOTE,
+ 'share_with' => 'user@remote.com',
+ 'share_with_displayname' => 'user@remote.com',
+ 'uid_owner' => 'ownerId',
+ 'displayname_owner' => 'ownerDisplay',
+ 'item_type' => 'folder',
+ 'item_source' => 2,
+ 'file_source' => 2,
+ 'file_target' => 'target',
+ 'file_parent' => 3,
+ 'token' => null,
+ 'expiration' => null,
+ 'permissions' => 4,
+ 'stime' => 5,
+ 'parent' => 6,
+ 'storage_id' => 'STORAGE',
+ 'path' => 'folder',
+ 'storage' => null, // HACK around static function
+ 'mail_send' => 0,
+ ];
+ $data[] = [$share, $expected];
+
+ // File shared by link with Expire
+ $expire = \DateTime::createFromFormat('Y-m-d h:i:s', '2000-01-02 01:02:03');
+ $share = $this->createShare(101,
+ \OCP\Share::SHARE_TYPE_LINK,
+ null,
+ $owner,
+ $folder,
+ 4,
+ 5,
+ $expire,
+ 6,
+ 'target',
+ 0,
+ 'token',
+ 'password');
+ $expected = [
+ 'id' => 101,
+ 'share_type' => \OCP\Share::SHARE_TYPE_LINK,
+ 'share_with' => 'password',
+ 'share_with_displayname' => 'password',
+ 'uid_owner' => 'ownerId',
+ 'displayname_owner' => 'ownerDisplay',
+ 'item_type' => 'folder',
+ 'item_source' => 2,
+ 'file_source' => 2,
+ 'file_target' => 'target',
+ 'file_parent' => 3,
+ 'token' => 'token',
+ 'expiration' => '2000-01-02 00:00:00',
+ 'permissions' => 4,
+ 'stime' => 5,
+ 'parent' => 6,
+ 'storage_id' => 'STORAGE',
+ 'path' => 'folder',
+ 'storage' => null, // HACK around static function
+ 'mail_send' => 0,
+ 'url' => 'url',
+ ];
+ $data[] = [$share, $expected];
+
+ return $data;
+ }
+
+ /**
+ * @dataProvider dataGetShare
+ */
+ public function testGetShare(\OC\Share20\IShare $share, array $result) {
+ $this->shareManager
+ ->expects($this->once())
+ ->method('getShareById')
+ ->with($share->getId())
+ ->willReturn($share);
+
+ $this->userFolder
+ ->method('getRelativePath')
+ ->will($this->returnArgument(0));
+
+ $this->urlGenerator
+ ->method('linkToRouteAbsolute')
+ ->willReturn('url');
+
+ $expected = new \OC_OCS_Result($result);
+ $this->assertEquals($expected->getData(), $this->ocs->getShare($share->getId())->getData()); }
}
diff --git a/apps/provisioning_api/appinfo/routes.php b/apps/provisioning_api/appinfo/routes.php
index dcf18e0e53b..d2cba549846 100644
--- a/apps/provisioning_api/appinfo/routes.php
+++ b/apps/provisioning_api/appinfo/routes.php
@@ -37,7 +37,7 @@ $users = new \OCA\Provisioning_API\Users(
\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::ADMIN_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);
diff --git a/apps/provisioning_api/lib/users.php b/apps/provisioning_api/lib/users.php
index 304fe901cfd..a2568425d0f 100644
--- a/apps/provisioning_api/lib/users.php
+++ b/apps/provisioning_api/lib/users.php
@@ -117,19 +117,50 @@ class Users {
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 {
- try {
- $this->userManager->createUser($userId, $password);
- $this->logger->info('Successful addUser call with userid: '.$_POST['userid'], ['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');
+ 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');
+ }
}
/**
diff --git a/apps/provisioning_api/tests/userstest.php b/apps/provisioning_api/tests/userstest.php
index ba4ed8a2e2f..63180eb3472 100644
--- a/apps/provisioning_api/tests/userstest.php
+++ b/apps/provisioning_api/tests/userstest.php
@@ -218,11 +218,95 @@ class UsersTest extends OriginalTest {
->expects($this->once())
->method('error')
->with('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
+ $loggedInUser = $this->getMock('\OCP\IUser');
+ $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->getMock('\OCP\IUser');
+ $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->getMock('\OCP\IUser');
+ $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';
@@ -239,6 +323,76 @@ class UsersTest extends OriginalTest {
->expects($this->once())
->method('info')
->with('Successful addUser call with userid: NewUser', ['app' => 'ocs_api']);
+ $loggedInUser = $this->getMock('\OCP\IUser');
+ $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->getMock('\OCP\IUser');
+ $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->getMock('\OCP\IUser');
+ $this->userManager
+ ->expects($this->once())
+ ->method('createUser')
+ ->with('NewUser', 'PasswordOfTheNewUser')
+ ->willReturn($user);
+ $group = $this->getMock('\OCP\IGroup');
+ $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());
@@ -261,11 +415,238 @@ class UsersTest extends OriginalTest {
->expects($this->once())
->method('error')
->with('Failed addUser attempt with exception: User backend not found.', ['app' => 'ocs_api']);
+ $loggedInUser = $this->getMock('\OCP\IUser');
+ $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->getMock('\OCP\IUser');
+ $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, \OCP\API::RESPOND_UNAUTHORISED);
+ $this->assertEquals($expected, $this->api->addUser());
+ }
+
+ public function testAddUserAsSubAdminNoGroup() {
+ $_POST['userid'] = 'NewUser';
+ $_POST['password'] = 'PasswordOfTheNewUser';
+ $loggedInUser = $this->getMock('\OCP\IUser');
+ $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->getMock('\OCP\IUser');
+ $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->getMock('\OCP\IGroup');
+ $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->getMock('\OCP\IUser');
+ $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->getMock('\OCP\IUser');
+ $this->userManager
+ ->expects($this->once())
+ ->method('createUser')
+ ->with('NewUser', 'PasswordOfTheNewUser')
+ ->willReturn($user);
+ $existingGroup1 = $this->getMock('\OCP\IGroup');
+ $existingGroup2 = $this->getMock('\OCP\IGroup');
+ $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())