diff options
author | John Molakvoæ <skjnldsv@users.noreply.github.com> | 2019-10-26 16:58:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-26 16:58:37 +0200 |
commit | 9348fd53b1450b80ed279f9caa18ca36e3169b08 (patch) | |
tree | c82df3a960e23ae8eb4a03b878abd734d1433e53 /apps | |
parent | a71b21c3d07f1751d14a2b05b5eb73d76f2e1416 (diff) | |
parent | d0b205d0dd59152455a4942d1882c2198623956c (diff) | |
download | nextcloud-server-9348fd53b1450b80ed279f9caa18ca36e3169b08.tar.gz nextcloud-server-9348fd53b1450b80ed279f9caa18ca36e3169b08.zip |
Cleanup and do not list current user shares in getShares too (#16789)
Cleanup and do not list current user shares in getShares too
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files_sharing/lib/Controller/ShareAPIController.php | 145 | ||||
-rw-r--r-- | apps/files_sharing/tests/Controller/ShareAPIControllerTest.php | 597 |
2 files changed, 668 insertions, 74 deletions
diff --git a/apps/files_sharing/lib/Controller/ShareAPIController.php b/apps/files_sharing/lib/Controller/ShareAPIController.php index cde4f93a0f0..e44ca84a09f 100644 --- a/apps/files_sharing/lib/Controller/ShareAPIController.php +++ b/apps/files_sharing/lib/Controller/ShareAPIController.php @@ -612,60 +612,31 @@ class ShareAPIController extends OCSController { /** * @param \OCP\Files\Folder $folder - * @return DataResponse + * @return array * @throws OCSBadRequestException */ - private function getSharesInDir(Node $folder): DataResponse { + private function getSharesInDir(Node $folder): array { if (!($folder instanceof \OCP\Files\Folder)) { throw new OCSBadRequestException($this->l->t('Not a directory')); } $nodes = $folder->getDirectoryListing(); - /** @var \OCP\Share\IShare[] $shares */ - $shares = []; - foreach ($nodes as $node) { - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_USER, $node, true, -1, 0)); - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_GROUP, $node, true, -1, 0)); - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_LINK, $node, true, -1, 0)); - if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_EMAIL, $node, true, -1, 0)); - } - if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE, $node, true, -1, 0)); - } - $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_ROOM, $node, true, -1, 0)); - } + /** @var \OCP\Share\IShare[] $shares */ + $shares = array_reduce($nodes, function($carry, $node) { + $carry = array_merge($carry, $this->getAllShares($node, true)); + return $carry; + }, []); - $formatted = $miniFormatted = []; - $resharingRight = false; + // filter out duplicate shares $known = []; - foreach ($shares as $share) { - if (in_array($share->getId(), $known) || $share->getSharedWith() === $this->currentUser) { - continue; - } - - try { - $format = $this->formatShare($share); - - $known[] = $share->getId(); - $formatted[] = $format; - if ($share->getSharedBy() === $this->currentUser) { - $miniFormatted[] = $format; - } - if (!$resharingRight && $this->shareProviderResharingRights($this->currentUser, $share, $folder)) { - $resharingRight = true; - } - } catch (\Exception $e) { - //Ignore this share + return array_filter($shares, function($share) use (&$known) { + if (in_array($share->getId(), $known)) { + return false; } - } - - if (!$resharingRight) { - $formatted = $miniFormatted; - } - - return new DataResponse($formatted); + $known[] = $share->getId(); + return true; + }); } /** @@ -714,56 +685,45 @@ class ShareAPIController extends OCSController { return $result; } - if ($subfiles === 'true') { - $result = $this->getSharesInDir($path); - return $result; - } - if ($reshares === 'true') { $reshares = true; } else { $reshares = false; } - // Get all shares - $userShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_USER, $path, $reshares, -1, 0); - $groupShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0); - $linkShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0); - if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { - $mailShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_EMAIL, $path, $reshares, -1, 0); - } else { - $mailShares = []; - } - if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_CIRCLE)) { - $circleShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_CIRCLE, $path, $reshares, -1, 0); + if ($subfiles === 'true') { + $shares = $this->getSharesInDir($path); + $recipientNode = null; } else { - $circleShares = []; - } - $roomShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_ROOM, $path, $reshares, -1, 0); - - $shares = array_merge($userShares, $groupShares, $linkShares, $mailShares, $circleShares, $roomShares); - - if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { - $federatedShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0); - $shares = array_merge($shares, $federatedShares); - } - - if ($this->shareManager->outgoingServer2ServerGroupSharesAllowed()) { - $federatedShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE_GROUP, $path, $reshares, -1, 0); - $shares = array_merge($shares, $federatedShares); + // get all shares + $shares = $this->getAllShares($path, $reshares); + $recipientNode = $path; } + // process all shares $formatted = $miniFormatted = []; $resharingRight = false; foreach ($shares as $share) { /** @var IShare $share */ + + // do not list the shares of the current user + if ($share->getSharedWith() === $this->currentUser) { + continue; + } + try { - $format = $this->formatShare($share, $path); + $format = $this->formatShare($share, $recipientNode); $formatted[] = $format; + + // let's also build a list of shares created + // by the current user only, in case + // there is no resharing rights if ($share->getSharedBy() === $this->currentUser) { $miniFormatted[] = $format; } + // check if one of those share is shared with me + // and if I have resharing rights on it if (!$resharingRight && $this->shareProviderResharingRights($this->currentUser, $share, $path)) { $resharingRight = true; } @@ -1324,4 +1284,41 @@ class ShareAPIController extends OCSController { return false; } + /** + * Get all the shares for the current user + * + * @param Node|null $path + * @param boolean $reshares + * @return void + */ + private function getAllShares(?Node $path = null, bool $reshares = false) { + // Get all shares + $userShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_USER, $path, $reshares, -1, 0); + $groupShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0); + $linkShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0); + + // EMAIL SHARES + $mailShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_EMAIL, $path, $reshares, -1, 0); + + // CIRCLE SHARES + $circleShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_CIRCLE, $path, $reshares, -1, 0); + + // TALK SHARES + $roomShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_ROOM, $path, $reshares, -1, 0); + + // FEDERATION + if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { + $federatedShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0); + } else { + $federatedShares = []; + } + if ($this->shareManager->outgoingServer2ServerGroupSharesAllowed()) { + $federatedGroupShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE_GROUP, $path, $reshares, -1, 0); + } else { + $federatedGroupShares = []; + } + + return array_merge($userShares, $groupShares, $linkShares, $mailShares, $circleShares, $roomShares, $federatedShares, $federatedGroupShares); + } + } diff --git a/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php index 5a84897fe91..c972c5c794e 100644 --- a/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php @@ -784,6 +784,603 @@ class ShareAPIControllerTest extends TestCase { $this->ocs->getShare(42); } + public function dataGetShares() { + $folder = $this->getMockBuilder(Folder::class)->getMock(); + $file1 = $this->getMockBuilder(File::class)->getMock(); + $file1->method('getName') + ->willReturn('file1'); + $file2 = $this->getMockBuilder(File::class)->getMock(); + $file2->method('getName') + ->willReturn('file2'); + + $folder->method('getDirectoryListing') + ->willReturn([$file1, $file2]); + + $file1UserShareOwner = \OC::$server->getShareManager()->newShare(); + $file1UserShareOwner->setShareType(IShare::TYPE_USER) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(4); + + $file1UserShareOwnerExpected = [ + 'id' => 4, + 'share_type' => IShare::TYPE_USER, + ]; + + $file1UserShareInitiator = \OC::$server->getShareManager()->newShare(); + $file1UserShareInitiator->setShareType(IShare::TYPE_USER) + ->setSharedWith('recipient') + ->setSharedBy('currentUser') + ->setShareOwner('owner') + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(8); + + $file1UserShareInitiatorExpected = [ + 'id' => 8, + 'share_type' => IShare::TYPE_USER, + ]; + + $file1UserShareRecipient = \OC::$server->getShareManager()->newShare(); + $file1UserShareRecipient->setShareType(IShare::TYPE_USER) + ->setSharedWith('currentUser') + ->setSharedBy('initiator') + ->setShareOwner('owner') + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(15); + + $file1UserShareRecipientExpected = [ + 'id' => 15, + 'share_type' => IShare::TYPE_USER, + ]; + + $file1UserShareOther = \OC::$server->getShareManager()->newShare(); + $file1UserShareOther->setShareType(IShare::TYPE_USER) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('owner') + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(16); + + $file1UserShareOtherExpected = [ + 'id' => 16, + 'share_type' => IShare::TYPE_USER, + ]; + + $file1GroupShareOwner = \OC::$server->getShareManager()->newShare(); + $file1GroupShareOwner->setShareType(IShare::TYPE_GROUP) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(23); + + $file1GroupShareOwnerExpected = [ + 'id' => 23, + 'share_type' => IShare::TYPE_GROUP, + ]; + + $file1GroupShareRecipient = \OC::$server->getShareManager()->newShare(); + $file1GroupShareRecipient->setShareType(IShare::TYPE_GROUP) + ->setSharedWith('currentUserGroup') + ->setSharedBy('initiator') + ->setShareOwner('owner') + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(42); + + $file1GroupShareRecipientExpected = [ + 'id' => 42, + 'share_type' => IShare::TYPE_GROUP, + ]; + + $file1GroupShareOther = \OC::$server->getShareManager()->newShare(); + $file1GroupShareOther->setShareType(IShare::TYPE_GROUP) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('owner') + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(108); + + $file1LinkShareOwner = \OC::$server->getShareManager()->newShare(); + $file1LinkShareOwner->setShareType(IShare::TYPE_LINK) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(415); + + $file1LinkShareOwnerExpected = [ + 'id' => 415, + 'share_type' => IShare::TYPE_LINK, + ]; + + $file1EmailShareOwner = \OC::$server->getShareManager()->newShare(); + $file1EmailShareOwner->setShareType(IShare::TYPE_EMAIL) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(416); + + $file1EmailShareOwnerExpected = [ + 'id' => 416, + 'share_type' => IShare::TYPE_EMAIL, + ]; + + $file1CircleShareOwner = \OC::$server->getShareManager()->newShare(); + $file1CircleShareOwner->setShareType(IShare::TYPE_CIRCLE) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(423); + + $file1CircleShareOwnerExpected = [ + 'id' => 423, + 'share_type' => IShare::TYPE_CIRCLE, + ]; + + $file1RoomShareOwner = \OC::$server->getShareManager()->newShare(); + $file1RoomShareOwner->setShareType(IShare::TYPE_ROOM) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(442); + + $file1RoomShareOwnerExpected = [ + 'id' => 442, + 'share_type' => IShare::TYPE_ROOM, + ]; + + $file1RemoteShareOwner = \OC::$server->getShareManager()->newShare(); + $file1RemoteShareOwner->setShareType(IShare::TYPE_REMOTE) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(815); + + $file1RemoteShareOwnerExpected = [ + 'id' => 815, + 'share_type' => IShare::TYPE_REMOTE, + ]; + + $file1RemoteGroupShareOwner = \OC::$server->getShareManager()->newShare(); + $file1RemoteGroupShareOwner->setShareType(IShare::TYPE_REMOTE_GROUP) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1) + ->setId(816); + + $file1RemoteGroupShareOwnerExpected = [ + 'id' => 816, + 'share_type' => IShare::TYPE_REMOTE_GROUP, + ]; + + $file2UserShareOwner = \OC::$server->getShareManager()->newShare(); + $file2UserShareOwner->setShareType(IShare::TYPE_USER) + ->setSharedWith('recipient') + ->setSharedBy('initiator') + ->setShareOwner('currentUser') + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file2) + ->setId(823); + + $file2UserShareOwnerExpected = [ + 'id' => 823, + 'share_type' => IShare::TYPE_USER, + ]; + + $data = [ + [ + [ + 'path' => $file1, + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareOwner, $file1UserShareOwner], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + $file1UserShareOwnerExpected, + $file1UserShareOwnerExpected, + ] + ], + [ + [ + 'path' => $file1, + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareRecipient], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + ] + ], + [ + [ + 'path' => $file1, + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + $file1UserShareInitiatorExpected, + $file1UserShareOtherExpected, + ] + ], + [ + [ + 'path' => $file1, + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther], + ], + ], + [ + ], + [ + $file1UserShareInitiatorExpected, + ] + ], + [ + [ + 'path' => $file1, + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner], + IShare::TYPE_GROUP => [$file1GroupShareRecipient], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + $file1GroupShareRecipientExpected, + ] + ], + [ + [ + 'path' => $file1, + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner], + IShare::TYPE_GROUP => [$file1GroupShareOwner], + IShare::TYPE_LINK => [$file1LinkShareOwner], + IShare::TYPE_EMAIL => [$file1EmailShareOwner], + IShare::TYPE_CIRCLE => [$file1CircleShareOwner], + IShare::TYPE_ROOM => [$file1RoomShareOwner], + IShare::TYPE_REMOTE => [$file1RemoteShareOwner], + IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + $file1GroupShareOwnerExpected, + $file1LinkShareOwnerExpected, + $file1EmailShareOwnerExpected, + $file1CircleShareOwnerExpected, + $file1RoomShareOwnerExpected, + ] + ], + [ + [ + 'path' => $file1, + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner], + IShare::TYPE_GROUP => [$file1GroupShareOwner], + IShare::TYPE_LINK => [$file1LinkShareOwner], + IShare::TYPE_EMAIL => [$file1EmailShareOwner], + IShare::TYPE_CIRCLE => [$file1CircleShareOwner], + IShare::TYPE_ROOM => [$file1RoomShareOwner], + IShare::TYPE_REMOTE => [$file1RemoteShareOwner], + IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner], + ], + ], + [ + IShare::TYPE_REMOTE => true, + IShare::TYPE_REMOTE_GROUP => true, + ], + [ + $file1UserShareOwnerExpected, + $file1GroupShareOwnerExpected, + $file1LinkShareOwnerExpected, + $file1EmailShareOwnerExpected, + $file1CircleShareOwnerExpected, + $file1RoomShareOwnerExpected, + $file1RemoteShareOwnerExpected, + $file1RemoteGroupShareOwnerExpected, + ] + ], + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner], + ], + 'file2' => [ + IShare::TYPE_USER => [$file2UserShareOwner], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + $file2UserShareOwnerExpected, + ] + ], + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareOwner, $file1UserShareOwner], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + ] + ], + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner, $file1UserShareRecipient], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected + ] + ], + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther], + ], + 'file2' => [ + IShare::TYPE_USER => [$file2UserShareOwner], + ], + ], + [ + ], + [ + $file1UserShareInitiatorExpected, + $file1UserShareOtherExpected, + $file2UserShareOwnerExpected, + ] + ], + // This might not happen in a real environment, as the combination + // of shares does not seem to be possible on a folder without + // resharing rights; if the folder has resharing rights then the + // share with others would be included too in the results. + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareRecipient, $file1UserShareInitiator, $file1UserShareOther], + ], + ], + [ + ], + [ + $file1UserShareInitiatorExpected, + ] + ], + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner], + IShare::TYPE_GROUP => [$file1GroupShareRecipient], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + $file1GroupShareRecipientExpected, + ] + ], + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner], + IShare::TYPE_GROUP => [$file1GroupShareOwner], + IShare::TYPE_LINK => [$file1LinkShareOwner], + IShare::TYPE_EMAIL => [$file1EmailShareOwner], + IShare::TYPE_CIRCLE => [$file1CircleShareOwner], + IShare::TYPE_ROOM => [$file1RoomShareOwner], + IShare::TYPE_REMOTE => [$file1RemoteShareOwner], + IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner], + ], + ], + [ + ], + [ + $file1UserShareOwnerExpected, + $file1GroupShareOwnerExpected, + $file1LinkShareOwnerExpected, + $file1EmailShareOwnerExpected, + $file1CircleShareOwnerExpected, + $file1RoomShareOwnerExpected, + ] + ], + [ + [ + 'path' => $folder, + 'subfiles' => 'true', + ], + [ + 'file1' => [ + IShare::TYPE_USER => [$file1UserShareOwner], + IShare::TYPE_GROUP => [$file1GroupShareOwner], + IShare::TYPE_LINK => [$file1LinkShareOwner], + IShare::TYPE_EMAIL => [$file1EmailShareOwner], + IShare::TYPE_CIRCLE => [$file1CircleShareOwner], + IShare::TYPE_ROOM => [$file1RoomShareOwner], + IShare::TYPE_REMOTE => [$file1RemoteShareOwner], + IShare::TYPE_REMOTE_GROUP => [$file1RemoteGroupShareOwner], + ], + ], + [ + IShare::TYPE_REMOTE => true, + IShare::TYPE_REMOTE_GROUP => true, + ], + [ + $file1UserShareOwnerExpected, + $file1GroupShareOwnerExpected, + $file1LinkShareOwnerExpected, + $file1EmailShareOwnerExpected, + $file1CircleShareOwnerExpected, + $file1RoomShareOwnerExpected, + $file1RemoteShareOwnerExpected, + $file1RemoteGroupShareOwnerExpected, + ] + ], + ]; + + return $data; + } + + /** + * @dataProvider dataGetShares + */ + public function testGetShares(array $getSharesParameters, array $shares, array $extraShareTypes, array $expected) { + /** @var \OCA\Files_Sharing\Controller\ShareAPIController $ocs */ + $ocs = $this->getMockBuilder(ShareAPIController::class) + ->setConstructorArgs([ + $this->appName, + $this->request, + $this->shareManager, + $this->groupManager, + $this->userManager, + $this->rootFolder, + $this->urlGenerator, + $this->currentUser, + $this->l, + $this->config, + $this->appManager, + $this->serverContainer + ])->setMethods(['formatShare']) + ->getMock(); + + $ocs->method('formatShare') + ->will($this->returnCallback( + function($share) { + return [ + 'id' => $share->getId(), + 'share_type' => $share->getShareType() + ]; + } + )); + + $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock(); + $userFolder->method('get') + ->with('path') + ->willReturn($getSharesParameters['path']); + + $this->rootFolder->method('getUserFolder') + ->with($this->currentUser) + ->willReturn($userFolder); + + $this->shareManager + ->method('getSharesBy') + ->will($this->returnCallback( + function($user, $shareType, $node) use ($shares) { + if (!isset($shares[$node->getName()]) || !isset($shares[$node->getName()][$shareType])) { + return []; + } + return $shares[$node->getName()][$shareType]; + } + )); + + $this->shareManager + ->method('outgoingServer2ServerSharesAllowed') + ->willReturn($extraShareTypes[ISHARE::TYPE_REMOTE] ?? false); + + $this->shareManager + ->method('outgoingServer2ServerGroupSharesAllowed') + ->willReturn($extraShareTypes[ISHARE::TYPE_REMOTE_GROUP] ?? false); + + $this->groupManager + ->method('isInGroup') + ->will($this->returnCallback( + function($user, $group) { + return $group === 'currentUserGroup'; + } + )); + + $result = $ocs->getShares( + $getSharesParameters['sharedWithMe'] ?? 'false', + $getSharesParameters['reshares'] ?? 'false', + $getSharesParameters['subfiles'] ?? 'false', + 'path' + ); + + $this->assertEquals($expected, $result->getData()); + } + public function testCanAccessShare() { $share = $this->getMockBuilder(IShare::class)->getMock(); $share->method('getShareOwner')->willReturn($this->currentUser); |