diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2016-01-22 07:58:36 +0100 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2016-01-22 07:58:36 +0100 |
commit | 766ba1cd5fac2019e73810b157bbf0bd3fdab1cf (patch) | |
tree | 246f3f59a170207039c41c2d68d8491d1ea0d686 | |
parent | b74cdb7da3bd7e3e91b674a32e197d8c3d432ea8 (diff) | |
parent | 658959592d637c3dc8f8bfac8cba962273c72f4b (diff) | |
download | nextcloud-server-766ba1cd5fac2019e73810b157bbf0bd3fdab1cf.tar.gz nextcloud-server-766ba1cd5fac2019e73810b157bbf0bd3fdab1cf.zip |
Merge pull request #21532 from owncloud/share2.0_getShares
[Share 2.0] get shares
-rw-r--r-- | apps/files_sharing/api/ocssharewrapper.php | 2 | ||||
-rw-r--r-- | apps/files_sharing/api/share20ocs.php | 98 | ||||
-rw-r--r-- | build/integration/features/sharing-v1.feature | 44 | ||||
-rw-r--r-- | lib/private/share20/defaultshareprovider.php | 178 | ||||
-rw-r--r-- | lib/private/share20/ishareprovider.php | 12 | ||||
-rw-r--r-- | lib/private/share20/manager.php | 51 | ||||
-rw-r--r-- | tests/lib/share20/defaultshareprovidertest.php | 438 |
7 files changed, 734 insertions, 89 deletions
diff --git a/apps/files_sharing/api/ocssharewrapper.php b/apps/files_sharing/api/ocssharewrapper.php index a186a34cf6a..cc52d478615 100644 --- a/apps/files_sharing/api/ocssharewrapper.php +++ b/apps/files_sharing/api/ocssharewrapper.php @@ -37,7 +37,7 @@ class OCSShareWrapper { } public function getAllShares($params) { - return \OCA\Files_Sharing\API\Local::getAllShares($params); + return $this->getShare20OCS()->getShares(); } public function createShare() { diff --git a/apps/files_sharing/api/share20ocs.php b/apps/files_sharing/api/share20ocs.php index e4cc50d9c1a..c2ff94db790 100644 --- a/apps/files_sharing/api/share20ocs.php +++ b/apps/files_sharing/api/share20ocs.php @@ -327,6 +327,104 @@ class Share20OCS { return new \OC_OCS_Result($share); } + private function getSharedWithMe() { + $userShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_USER, -1, 0); + $groupShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, -1, 0); + + $shares = array_merge($userShares, $groupShares); + + $formatted = []; + foreach ($shares as $share) { + $formatted[] = $this->formatShare($share); + } + + return new \OC_OCS_Result($formatted); + } + + /** + * @param \OCP\Files\Folder $folder + * @return \OC_OCS_Result + */ + private function getSharesInDir($folder) { + if (!($folder instanceof \OCP\Files\Folder)) { + return new \OC_OCS_Result(null, 400, "not a directory"); + } + + $nodes = $folder->getDirectoryListing(); + /** @var IShare[] $shares */ + $shares = []; + foreach ($nodes as $node) { + $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $node, false, -1, 0)); + $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $node, false, -1, 0)); + $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $node, false, -1, 0)); + //TODO: Add federated shares + + } + + $formatted = []; + foreach ($shares as $share) { + $formatted[] = $this->formatShare($share); + } + + return new \OC_OCS_Result($formatted); + } + + /** + * The getShares function. + * + * - Get shares by the current user + * - Get shares by the current user and reshares (?reshares=true) + * - Get shares with the current user (?shared_with_me=true) + * - Get shares for a specific path (?path=...) + * - Get all shares in a folder (?subfiles=true&path=..) + * + * @return \OC_OCS_Result + */ + public function getShares() { + $sharedWithMe = $this->request->getParam('shared_with_me', null); + $reshares = $this->request->getParam('reshares', null); + $subfiles = $this->request->getParam('subfiles'); + $path = $this->request->getParam('path', null); + + if ($sharedWithMe === 'true') { + return $this->getSharedWithMe(); + } + + if ($path !== null) { + $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); + try { + $path = $userFolder->get($path); + } catch (\OCP\Files\NotFoundException $e) { + return new \OC_OCS_Result(null, 404, 'wrong path, file/folder doesn\'t exist'); + } + } + + if ($subfiles === 'true') { + return $this->getSharesInDir($path); + } + + if ($reshares === 'true') { + $reshares = true; + } else { + $reshares = false; + } + + // Get all shares + $userShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $path, $reshares, -1, 0); + $groupShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0); + $linkShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0); + //TODO: Add federated shares + + $shares = array_merge($userShares, $groupShares, $linkShares); + + $formatted = []; + foreach ($shares as $share) { + $formatted[] = $this->formatShare($share); + } + + return new \OC_OCS_Result($formatted); + } + /** * @param IShare $share * @return bool diff --git a/build/integration/features/sharing-v1.feature b/build/integration/features/sharing-v1.feature index 1287a2daf86..dedf2c388fc 100644 --- a/build/integration/features/sharing-v1.feature +++ b/build/integration/features/sharing-v1.feature @@ -231,23 +231,20 @@ Feature: sharing And User "user2" should be included in the response And User "user3" should not be included in the response -# Skip this test for now. Since the new shares do not create reshares -# TODO enable when getshares is updated -# -# Scenario: getting all shares of a file with reshares -# Given user "user0" exists -# And user "user1" exists -# And user "user2" exists -# And user "user3" exists -# And file "textfile0.txt" of user "user0" is shared with user "user1" -# And file "textfile0.txt" of user "user1" is shared with user "user2" -# And As an "user0" -# When sending "GET" to "/apps/files_sharing/api/v1/shares?reshares=true&path=textfile0.txt" -# Then the OCS status code should be "100" -# And the HTTP status code should be "200" -# And User "user1" should be included in the response -# And User "user2" should be included in the response -# And User "user3" should not be included in the response + Scenario: getting all shares of a file with reshares + Given user "user0" exists + And user "user1" exists + And user "user2" exists + And user "user3" exists + And file "textfile0.txt" of user "user0" is shared with user "user1" + And file "textfile0 (2).txt" of user "user1" is shared with user "user2" + And As an "user0" + When sending "GET" to "/apps/files_sharing/api/v1/shares?reshares=true&path=textfile0.txt" + Then the OCS status code should be "100" + And the HTTP status code should be "200" + And User "user1" should be included in the response + And User "user2" should be included in the response + And User "user3" should not be included in the response Scenario: getting share info of a share Given user "user0" exists @@ -316,6 +313,19 @@ Feature: sharing And the HTTP status code should be "200" And last share_id is included in the answer + Scenario: Sharee can see the group share + Given As an "admin" + And user "user0" exists + And user "user1" exists + And group "group0" exists + And user "user1" belongs to group "group0" + And file "textfile0.txt" of user "user0" is shared with group "group0" + And As an "user1" + When sending "GET" to "/apps/files_sharing/api/v1/shares?shared_with_me=true" + Then the OCS status code should be "100" + And the HTTP status code should be "200" + And last share_id is included in the answer + Scenario: User is not allowed to reshare file As an "admin" Given user "user0" exists diff --git a/lib/private/share20/defaultshareprovider.php b/lib/private/share20/defaultshareprovider.php index d47919d21a3..5d768a4bc4b 100644 --- a/lib/private/share20/defaultshareprovider.php +++ b/lib/private/share20/defaultshareprovider.php @@ -32,6 +32,8 @@ use OCP\Files\IRootFolder; use OCP\IDBConnection; use OCP\Files\Node; +use Doctrine\DBAL\Connection; + /** * Class DefaultShareProvider * @@ -240,15 +242,67 @@ class DefaultShareProvider implements IShareProvider { } /** - * Get all shares by the given user + * Get all shares by the given user. Sharetype and path can be used to filter. * * @param IUser $user * @param int $shareType + * @param \OCP\Files\File|\OCP\Files\Folder $node + * @param bool $reshares Also get the shares where $user is the owner instead of just the shares where $user is the initiator + * @param int $limit The maximum number of shares to be returned, -1 for all shares * @param int $offset - * @param int $limit * @return Share[] */ - public function getShares(IUser $user, $shareType, $offset, $limit) { + public function getSharesBy(IUser $user, $shareType, $node, $reshares, $limit, $offset) { + $qb = $this->dbConn->getQueryBuilder(); + $qb->select('*') + ->from('share'); + + $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType))); + + /** + * Reshares for this user are shares where they are the owner. + */ + if ($reshares === false) { + //Special case for old shares created via the web UI + $or1 = $qb->expr()->andX( + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($user->getUID())), + $qb->expr()->isNull('uid_initiator') + ); + + $qb->andWhere( + $qb->expr()->orX( + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($user->getUID())), + $or1 + ) + ); + } else { + $qb->andWhere( + $qb->expr()->orX( + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($user->getUID())), + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($user->getUID())) + ) + ); + } + + if ($node !== null) { + $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId()))); + } + + if ($limit !== -1) { + $qb->setMaxResults($limit); + } + + $qb->setFirstResult($offset); + $qb->orderBy('id'); + + $cursor = $qb->execute(); + $shares = []; + while($data = $cursor->fetch()) { + $shares[] = $this->createShare($data); + } + $cursor->closeCursor(); + + return $shares; } /** @@ -324,11 +378,94 @@ class DefaultShareProvider implements IShareProvider { /** * Get shared with the given user * - * @param IUser $user - * @param int $shareType - * @param Share + * @param IUser $user get shares where this user is the recipient + * @param int $shareType \OCP\Share::SHARE_TYPE_USER or \OCP\Share::SHARE_TYPE_GROUP are supported + * @param int $limit The maximum number of shares, -1 for all + * @param int $offset + * @return IShare[] + * @throws BackendError */ - public function getSharedWithMe(IUser $user, $shareType = null) { + public function getSharedWith(IUser $user, $shareType, $limit, $offset) { + /** @var Share[] $shares */ + $shares = []; + + if ($shareType === \OCP\Share::SHARE_TYPE_USER) { + //Get shares directly with this user + $qb = $this->dbConn->getQueryBuilder(); + $qb->select('*') + ->from('share'); + + // Order by id + $qb->orderBy('id'); + + // Set limit and offset + if ($limit !== -1) { + $qb->setMaxResults($limit); + } + $qb->setFirstResult($offset); + + $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))); + $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($user->getUID()))); + + $cursor = $qb->execute(); + + while($data = $cursor->fetch()) { + $shares[] = $this->createShare($data); + } + $cursor->closeCursor(); + + } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { + $allGroups = $this->groupManager->getUserGroups($user); + + $start = 0; + while(true) { + $groups = array_slice($allGroups, $start, 100); + $start += 100; + + if ($groups === []) { + break; + } + + $qb = $this->dbConn->getQueryBuilder(); + $qb->select('*') + ->from('share') + ->orderBy('id') + ->setFirstResult(0); + + if ($limit !== -1) { + $qb->setMaxResults($limit - count($shares)); + } + + $groups = array_map(function(IGroup $group) { return $group->getGID(); }, $groups); + + $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))); + $qb->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter( + $groups, + Connection::PARAM_STR_ARRAY + ))); + + $cursor = $qb->execute(); + while($data = $cursor->fetch()) { + if ($offset > 0) { + $offset--; + continue; + } + $shares[] = $this->createShare($data); + } + $cursor->closeCursor(); + } + + /* + * Resolve all group shares to user specific shares + * TODO: Optmize this! + */ + $shares = array_map([$this, 'resolveGroupShare'], $shares); + } else { + throw new BackendError('Invalid backend'); + } + + + return $shares; } /** @@ -454,4 +591,31 @@ class DefaultShareProvider implements IShareProvider { return $nodes[0]; } + /** + * Resolve a group share to a user specific share + * Thus if the user moved their group share make sure this is properly reflected here. + * + * @param Share $share + * @return Share Returns the updated share if one was found else return the original share. + */ + private function resolveGroupShare(Share $share) { + $qb = $this->dbConn->getQueryBuilder(); + + $stmt = $qb->select('*') + ->from('share') + ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) + ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) + ->execute(); + + $data = $stmt->fetch(); + $stmt->closeCursor(); + + if ($data !== false) { + $share->setPermissions($data['permissions']); + $share->setTarget($data['file_target']); + } + + return $share; + } + }
\ No newline at end of file diff --git a/lib/private/share20/ishareprovider.php b/lib/private/share20/ishareprovider.php index 81770a45874..36d0f10c7f1 100644 --- a/lib/private/share20/ishareprovider.php +++ b/lib/private/share20/ishareprovider.php @@ -62,11 +62,13 @@ interface IShareProvider { * * @param IUser $user * @param int $shareType + * @param \OCP\Files\File|\OCP\Files\Folder $node + * @param bool $reshares Also get the shares where $user is the owner instead of just the shares where $user is the initiator + * @param int $limit The maximum number of shares to be returned, -1 for all shares * @param int $offset - * @param int $limit * @return Share[] */ - public function getShares(IUser $user, $shareType, $offset, $limit); + public function getSharesBy(IUser $user, $shareType, $node, $reshares, $limit, $offset); /** * Get share by id @@ -96,11 +98,13 @@ interface IShareProvider { /** * Get shared with the given user * - * @param IUser $user + * @param IUser $user get shares where this user is the recipient * @param int $shareType + * @param int $limit The max number of entries returned, -1 for all + * @param int $offset * @param Share */ - public function getSharedWithMe(IUser $user, $shareType = null); + public function getSharedWith(IUser $user, $shareType, $limit, $offset); /** * Get a share by token diff --git a/lib/private/share20/manager.php b/lib/private/share20/manager.php index 2be8fb5174d..3935307b977 100644 --- a/lib/private/share20/manager.php +++ b/lib/private/share20/manager.php @@ -605,14 +605,43 @@ class Manager { \OC_Hook::emit('OCP\Share', 'post_unshare', $hookParams); } + /** - * Retrieve all shares by the current user + * Get shares shared by (initiated) by the provided user. * - * @param int $page - * @param int $perPage - * @return Share[] + * @param IUser $user + * @param int $shareType + * @param \OCP\Files\File|\OCP\Files\Folder $path + * @param bool $reshares + * @param int $limit The maximum number of returned results, -1 for all results + * @param int $offset + * @return IShare[] */ - public function getShares($page=0, $perPage=50) { + public function getSharesBy(IUser $user, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) { + if ($path !== null && + !($path instanceof \OCP\Files\File) && + !($path instanceof \OCP\Files\Folder)) { + throw new \InvalidArgumentException('invalid path'); + } + + $provider = $this->factory->getProviderForType($shareType); + + return $provider->getSharesBy($user, $shareType, $path, $reshares, $limit, $offset); + } + + /** + * Get shares shared with $user. + * + * @param IUser $user + * @param int $shareType + * @param int $limit The maximum number of shares returned, -1 for all + * @param int $offset + * @return IShare[] + */ + public function getSharedWith(IUser $user, $shareType, $limit = 50, $offset = 0) { + $provider = $this->factory->getProviderForType($shareType); + + return $provider->getSharedWith($user, $shareType, $limit, $offset); } /** @@ -650,18 +679,6 @@ class Manager { } /** - * Get all shares that are shared with the current user - * - * @param int $shareType - * @param int $page - * @param int $perPage - * - * @return Share[] - */ - public function getSharedWithMe($shareType = null, $page=0, $perPage=50) { - } - - /** * Get the share by token possible with password * * @param string $token diff --git a/tests/lib/share20/defaultshareprovidertest.php b/tests/lib/share20/defaultshareprovidertest.php index 039692772a0..812c6ecc27e 100644 --- a/tests/lib/share20/defaultshareprovidertest.php +++ b/tests/lib/share20/defaultshareprovidertest.php @@ -37,13 +37,13 @@ class DefaultShareProviderTest extends \Test\TestCase { /** @var IDBConnection */ protected $dbConn; - /** @var IUserManager */ + /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */ protected $userManager; - /** @var IGroupManager */ + /** @var IGroupManager | \PHPUnit_Framework_MockObject_MockObject */ protected $groupManager; - /** @var IRootFolder */ + /** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */ protected $rootFolder; /** @var DefaultShareProvider */ @@ -92,16 +92,7 @@ class DefaultShareProviderTest extends \Test\TestCase { ]); $qb->execute(); - // Get the id - $qb = $this->dbConn->getQueryBuilder(); - $cursor = $qb->select('id') - ->from('share') - ->setMaxResults(1) - ->orderBy('id', 'DESC') - ->execute(); - $id = $cursor->fetch(); - $id = $id['id']; - $cursor->closeCursor(); + $id = $qb->getLastInsertId(); $sharedWith = $this->getMock('OCP\IUser'); $sharedBy = $this->getMock('OCP\IUser'); @@ -165,15 +156,7 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->assertEquals(1, $qb->execute()); // Get the id - $qb = $this->dbConn->getQueryBuilder(); - $cursor = $qb->select('id') - ->from('share') - ->setMaxResults(1) - ->orderBy('id', 'DESC') - ->execute(); - $id = $cursor->fetch(); - $id = $id['id']; - $cursor->closeCursor(); + $id = $qb->getLastInsertId(); $sharedWith = $this->getMock('OCP\IGroup'); $sharedBy = $this->getMock('OCP\IUser'); @@ -242,16 +225,7 @@ class DefaultShareProviderTest extends \Test\TestCase { ]); $this->assertEquals(1, $qb->execute()); - // Get the id - $qb = $this->dbConn->getQueryBuilder(); - $cursor = $qb->select('id') - ->from('share') - ->setMaxResults(1) - ->orderBy('id', 'DESC') - ->execute(); - $id = $cursor->fetch(); - $id = $id['id']; - $cursor->closeCursor(); + $id = $qb->getLastInsertId(); $sharedBy = $this->getMock('OCP\IUser'); $sharedBy->method('getUID')->willReturn('sharedBy'); @@ -311,17 +285,7 @@ class DefaultShareProviderTest extends \Test\TestCase { ]); $this->assertEquals(1, $qb->execute()); - // Get the id - $qb = $this->dbConn->getQueryBuilder(); - $cursor = $qb->select('id') - ->from('share') - ->setMaxResults(1) - ->orderBy('id', 'DESC') - ->execute(); - $id = $cursor->fetch(); - $id = $id['id']; - $cursor->closeCursor(); - + $id = $qb->getLastInsertId(); $share = $this->getMock('OC\Share20\IShare'); $share->method('getId')->willReturn($id); @@ -771,4 +735,392 @@ class DefaultShareProviderTest extends \Test\TestCase { public function testGetShareByTokenNotFound() { $this->provider->getShareByToken('invalidtoken'); } + + public function testGetSharedWithUser() { + $qb = $this->dbConn->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER), + 'share_with' => $qb->expr()->literal('sharedWith'), + 'uid_owner' => $qb->expr()->literal('shareOwner'), + 'uid_initiator' => $qb->expr()->literal('sharedBy'), + 'item_type' => $qb->expr()->literal('file'), + 'file_source' => $qb->expr()->literal(42), + 'file_target' => $qb->expr()->literal('myTarget'), + 'permissions' => $qb->expr()->literal(13), + ]); + $this->assertEquals(1, $qb->execute()); + $id = $qb->getLastInsertId(); + + $qb = $this->dbConn->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER), + 'share_with' => $qb->expr()->literal('sharedWith2'), + 'uid_owner' => $qb->expr()->literal('shareOwner2'), + 'uid_initiator' => $qb->expr()->literal('sharedBy2'), + 'item_type' => $qb->expr()->literal('file2'), + 'file_source' => $qb->expr()->literal(43), + 'file_target' => $qb->expr()->literal('myTarget2'), + 'permissions' => $qb->expr()->literal(14), + ]); + $this->assertEquals(1, $qb->execute()); + + $user = $this->getMock('\OCP\IUser'); + $user->method('getUID')->willReturn('sharedWith'); + $owner = $this->getMock('\OCP\IUser'); + $owner->method('getUID')->willReturn('shareOwner'); + $initiator = $this->getMock('\OCP\IUser'); + $initiator->method('getUID')->willReturn('sharedBy'); + + $this->userManager->method('get')->willReturnMap([ + ['sharedWith', $user], + ['shareOwner', $owner], + ['sharedBy', $initiator], + ]); + + $file = $this->getMock('\OCP\Files\File'); + $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf()); + $this->rootFolder->method('getById')->with(42)->willReturn([$file]); + + $share = $this->provider->getSharedWith($user, \OCP\Share::SHARE_TYPE_USER, 1 , 0); + $this->assertCount(1, $share); + + $share = $share[0]; + $this->assertEquals($id, $share->getId()); + $this->assertEquals($user, $share->getSharedWith()); + $this->assertEquals($owner, $share->getShareOwner()); + $this->assertEquals($initiator, $share->getSharedBy()); + $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType()); + } + + public function testGetSharedWithGroup() { + $qb = $this->dbConn->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP), + 'share_with' => $qb->expr()->literal('sharedWith'), + 'uid_owner' => $qb->expr()->literal('shareOwner2'), + 'uid_initiator' => $qb->expr()->literal('sharedBy2'), + 'item_type' => $qb->expr()->literal('file'), + 'file_source' => $qb->expr()->literal(43), + 'file_target' => $qb->expr()->literal('myTarget2'), + 'permissions' => $qb->expr()->literal(14), + ]); + $this->assertEquals(1, $qb->execute()); + + $qb = $this->dbConn->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP), + 'share_with' => $qb->expr()->literal('sharedWith'), + 'uid_owner' => $qb->expr()->literal('shareOwner'), + 'uid_initiator' => $qb->expr()->literal('sharedBy'), + 'item_type' => $qb->expr()->literal('file'), + 'file_source' => $qb->expr()->literal(42), + 'file_target' => $qb->expr()->literal('myTarget'), + 'permissions' => $qb->expr()->literal(13), + ]); + $this->assertEquals(1, $qb->execute()); + $id = $qb->getLastInsertId(); + + $groups = []; + foreach(range(0, 100) as $i) { + $group = $this->getMock('\OCP\IGroup'); + $group->method('getGID')->willReturn('group'.$i); + $groups[] = $group; + } + + $group = $this->getMock('\OCP\IGroup'); + $group->method('getGID')->willReturn('sharedWith'); + $groups[] = $group; + + $user = $this->getMock('\OCP\IUser'); + $user->method('getUID')->willReturn('sharedWith'); + $owner = $this->getMock('\OCP\IUser'); + $owner->method('getUID')->willReturn('shareOwner'); + $initiator = $this->getMock('\OCP\IUser'); + $initiator->method('getUID')->willReturn('sharedBy'); + + $this->userManager->method('get')->willReturnMap([ + ['shareOwner', $owner], + ['sharedBy', $initiator], + ]); + $this->groupManager->method('getUserGroups')->with($user)->willReturn($groups); + $this->groupManager->method('get')->with('sharedWith')->willReturn($group); + + $file = $this->getMock('\OCP\Files\File'); + $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf()); + $this->rootFolder->method('getById')->with(42)->willReturn([$file]); + + $share = $this->provider->getSharedWith($user, \OCP\Share::SHARE_TYPE_GROUP, 20 , 1); + $this->assertCount(1, $share); + + $share = $share[0]; + $this->assertEquals($id, $share->getId()); + $this->assertEquals($group, $share->getSharedWith()); + $this->assertEquals($owner, $share->getShareOwner()); + $this->assertEquals($initiator, $share->getSharedBy()); + $this->assertEquals(\OCP\Share::SHARE_TYPE_GROUP, $share->getShareType()); + } + + public function testGetSharedWithGroupUserModified() { + $qb = $this->dbConn->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP), + 'share_with' => $qb->expr()->literal('sharedWith'), + 'uid_owner' => $qb->expr()->literal('shareOwner'), + 'uid_initiator' => $qb->expr()->literal('sharedBy'), + 'item_type' => $qb->expr()->literal('file'), + 'file_source' => $qb->expr()->literal(42), + 'file_target' => $qb->expr()->literal('myTarget'), + 'permissions' => $qb->expr()->literal(13), + ]); + $this->assertEquals(1, $qb->execute()); + $id = $qb->getLastInsertId(); + + $qb = $this->dbConn->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(2), + 'share_with' => $qb->expr()->literal('user'), + 'uid_owner' => $qb->expr()->literal('shareOwner'), + 'uid_initiator' => $qb->expr()->literal('sharedBy'), + 'item_type' => $qb->expr()->literal('file'), + 'file_source' => $qb->expr()->literal(42), + 'file_target' => $qb->expr()->literal('userTarget'), + 'permissions' => $qb->expr()->literal(0), + 'parent' => $qb->expr()->literal($id), + ]); + $this->assertEquals(1, $qb->execute()); + + $group = $this->getMock('\OCP\IGroup'); + $group->method('getGID')->willReturn('sharedWith'); + $groups = [$group]; + + $user = $this->getMock('\OCP\IUser'); + $user->method('getUID')->willReturn('user'); + $owner = $this->getMock('\OCP\IUser'); + $owner->method('getUID')->willReturn('shareOwner'); + $initiator = $this->getMock('\OCP\IUser'); + $initiator->method('getUID')->willReturn('sharedBy'); + + $this->userManager->method('get')->willReturnMap([ + ['shareOwner', $owner], + ['sharedBy', $initiator], + ]); + $this->groupManager->method('getUserGroups')->with($user)->willReturn($groups); + $this->groupManager->method('get')->with('sharedWith')->willReturn($group); + + $file = $this->getMock('\OCP\Files\File'); + $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf()); + $this->rootFolder->method('getById')->with(42)->willReturn([$file]); + + $share = $this->provider->getSharedWith($user, \OCP\Share::SHARE_TYPE_GROUP, -1, 0); + $this->assertCount(1, $share); + + $share = $share[0]; + $this->assertEquals($id, $share->getId()); + $this->assertEquals($group, $share->getSharedWith()); + $this->assertEquals($owner, $share->getShareOwner()); + $this->assertEquals($initiator, $share->getSharedBy()); + $this->assertEquals(\OCP\Share::SHARE_TYPE_GROUP, $share->getShareType()); + $this->assertEquals(0, $share->getPermissions()); + $this->assertEquals('userTarget', $share->getTarget()); + } + + public function testGetSharesBy() { + $qb = $this->dbConn->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER), + 'share_with' => $qb->expr()->literal('sharedWith'), + 'uid_owner' => $qb->expr()->literal('shareOwner'), + 'uid_initiator' => $qb->expr()->literal('sharedBy'), + 'item_type' => $qb->expr()->literal('file'), + 'file_source' => $qb->expr()->literal(42), + 'file_target' => $qb->expr()->literal('myTarget'), + 'permissions' => $qb->expr()->literal(13), + ]); + $this->assertEquals(1, $qb->execute()); + $id = $qb->getLastInsertId(); + + $qb = $this->dbConn->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER), + 'share_with' => $qb->expr()->literal('sharedWith'), + 'uid_owner' => $qb->expr()->literal('shareOwner'), + 'uid_initiator' => $qb->expr()->literal('sharedBy2'), + 'item_type' => $qb->expr()->literal('file'), + 'file_source' => $qb->expr()->literal(42), + 'file_target' => $qb->expr()->literal('userTarget'), + 'permissions' => $qb->expr()->literal(0), + 'parent' => $qb->expr()->literal($id), + ]); + $this->assertEquals(1, $qb->execute()); + + $user = $this->getMock('\OCP\IUser'); + $user->method('getUID')->willReturn('sharedWith'); + $owner = $this->getMock('\OCP\IUser'); + $owner->method('getUID')->willReturn('shareOwner'); + $initiator = $this->getMock('\OCP\IUser'); + $initiator->method('getUID')->willReturn('sharedBy'); + + $this->userManager->method('get')->willReturnMap([ + ['sharedWith', $user], + ['shareOwner', $owner], + ['sharedBy', $initiator], + ]); + + $file = $this->getMock('\OCP\Files\File'); + $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf()); + $this->rootFolder->method('getById')->with(42)->willReturn([$file]); + + $share = $this->provider->getSharesBy($initiator, \OCP\Share::SHARE_TYPE_USER, null, false, 1, 0); + $this->assertCount(1, $share); + + $share = $share[0]; + $this->assertEquals($id, $share->getId()); + $this->assertEquals($user, $share->getSharedWith()); + $this->assertEquals($owner, $share->getShareOwner()); + $this->assertEquals($initiator, $share->getSharedBy()); + $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType()); + $this->assertEquals(13, $share->getPermissions()); + $this->assertEquals('myTarget', $share->getTarget()); + } + + public function testGetSharesNode() { + $qb = $this->dbConn->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER), + 'share_with' => $qb->expr()->literal('sharedWith'), + 'uid_owner' => $qb->expr()->literal('shareOwner'), + 'uid_initiator' => $qb->expr()->literal('sharedBy'), + 'item_type' => $qb->expr()->literal('file'), + 'file_source' => $qb->expr()->literal(42), + 'file_target' => $qb->expr()->literal('myTarget'), + 'permissions' => $qb->expr()->literal(13), + ]); + $this->assertEquals(1, $qb->execute()); + $id = $qb->getLastInsertId(); + + $qb = $this->dbConn->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER), + 'share_with' => $qb->expr()->literal('sharedWith'), + 'uid_owner' => $qb->expr()->literal('shareOwner'), + 'uid_initiator' => $qb->expr()->literal('sharedBy'), + 'item_type' => $qb->expr()->literal('file'), + 'file_source' => $qb->expr()->literal(43), + 'file_target' => $qb->expr()->literal('userTarget'), + 'permissions' => $qb->expr()->literal(0), + 'parent' => $qb->expr()->literal($id), + ]); + $this->assertEquals(1, $qb->execute()); + + $user = $this->getMock('\OCP\IUser'); + $user->method('getUID')->willReturn('sharedWith'); + $owner = $this->getMock('\OCP\IUser'); + $owner->method('getUID')->willReturn('shareOwner'); + $initiator = $this->getMock('\OCP\IUser'); + $initiator->method('getUID')->willReturn('sharedBy'); + + $this->userManager->method('get')->willReturnMap([ + ['sharedWith', $user], + ['shareOwner', $owner], + ['sharedBy', $initiator], + ]); + + $file = $this->getMock('\OCP\Files\File'); + $file->method('getId')->willReturn(42); + $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf()); + $this->rootFolder->method('getById')->with(42)->willReturn([$file]); + + $share = $this->provider->getSharesBy($initiator, \OCP\Share::SHARE_TYPE_USER, $file, false, 1, 0); + $this->assertCount(1, $share); + + $share = $share[0]; + $this->assertEquals($id, $share->getId()); + $this->assertEquals($user, $share->getSharedWith()); + $this->assertEquals($owner, $share->getShareOwner()); + $this->assertEquals($initiator, $share->getSharedBy()); + $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType()); + $this->assertEquals(13, $share->getPermissions()); + $this->assertEquals('myTarget', $share->getTarget()); + } + + public function testGetSharesReshare() { + $qb = $this->dbConn->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER), + 'share_with' => $qb->expr()->literal('sharedWith'), + 'uid_owner' => $qb->expr()->literal('shareOwner'), + 'uid_initiator' => $qb->expr()->literal('shareOwner'), + 'item_type' => $qb->expr()->literal('file'), + 'file_source' => $qb->expr()->literal(42), + 'file_target' => $qb->expr()->literal('myTarget'), + 'permissions' => $qb->expr()->literal(13), + ]); + $this->assertEquals(1, $qb->execute()); + $id1 = $qb->getLastInsertId(); + + $qb = $this->dbConn->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_USER), + 'share_with' => $qb->expr()->literal('sharedWith'), + 'uid_owner' => $qb->expr()->literal('shareOwner'), + 'uid_initiator' => $qb->expr()->literal('sharedBy'), + 'item_type' => $qb->expr()->literal('file'), + 'file_source' => $qb->expr()->literal(42), + 'file_target' => $qb->expr()->literal('userTarget'), + 'permissions' => $qb->expr()->literal(0), + ]); + $this->assertEquals(1, $qb->execute()); + $id2 = $qb->getLastInsertId(); + + $user = $this->getMock('\OCP\IUser'); + $user->method('getUID')->willReturn('sharedWith'); + $owner = $this->getMock('\OCP\IUser'); + $owner->method('getUID')->willReturn('shareOwner'); + $initiator = $this->getMock('\OCP\IUser'); + $initiator->method('getUID')->willReturn('sharedBy'); + + $this->userManager->method('get')->willReturnMap([ + ['sharedWith', $user], + ['shareOwner', $owner], + ['sharedBy', $initiator], + ]); + + $file = $this->getMock('\OCP\Files\File'); + $file->method('getId')->willReturn(42); + $this->rootFolder->method('getUserFolder')->with('shareOwner')->will($this->returnSelf()); + $this->rootFolder->method('getById')->with(42)->willReturn([$file]); + + $shares = $this->provider->getSharesBy($owner, \OCP\Share::SHARE_TYPE_USER, null, true, -1, 0); + $this->assertCount(2, $shares); + + $share = $shares[0]; + $this->assertEquals($id1, $share->getId()); + $this->assertSame($user, $share->getSharedWith()); + $this->assertSame($owner, $share->getShareOwner()); + $this->assertSame($owner, $share->getSharedBy()); + $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType()); + $this->assertEquals(13, $share->getPermissions()); + $this->assertEquals('myTarget', $share->getTarget()); + + $share = $shares[1]; + $this->assertEquals($id2, $share->getId()); + $this->assertSame($user, $share->getSharedWith()); + $this->assertSame($owner, $share->getShareOwner()); + $this->assertSame($initiator, $share->getSharedBy()); + $this->assertEquals(\OCP\Share::SHARE_TYPE_USER, $share->getShareType()); + $this->assertEquals(0, $share->getPermissions()); + $this->assertEquals('userTarget', $share->getTarget()); + } } |