diff options
author | Morris Jobke <hey@morrisjobke.de> | 2017-04-15 13:06:24 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-15 13:06:24 -0500 |
commit | 10290eb0060eb2194be2dd4fc8b707aa7eef5c0a (patch) | |
tree | b71665687f9c19f5b6f8e9f34ffbd5a8a2d4f5e8 /apps | |
parent | dafa9c740abee38bb54e9df05947bcf09857380a (diff) | |
parent | cab41118f67e6a4743bac2cfcfa69a4bee28f2a3 (diff) | |
download | nextcloud-server-10290eb0060eb2194be2dd4fc8b707aa7eef5c0a.tar.gz nextcloud-server-10290eb0060eb2194be2dd4fc8b707aa7eef5c0a.zip |
Merge pull request #2834 from nextcloud/accesListToShareManager
Access list to share manager
Diffstat (limited to 'apps')
4 files changed, 186 insertions, 1 deletions
diff --git a/apps/federatedfilesharing/lib/FederatedShareProvider.php b/apps/federatedfilesharing/lib/FederatedShareProvider.php index 120365263a9..34e02391e05 100644 --- a/apps/federatedfilesharing/lib/FederatedShareProvider.php +++ b/apps/federatedfilesharing/lib/FederatedShareProvider.php @@ -28,6 +28,7 @@ namespace OCA\FederatedFileSharing; use OC\Share20\Share; use OCP\Federation\ICloudIdManager; +use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\IConfig; @@ -974,4 +975,43 @@ class FederatedShareProvider implements IShareProvider { $result = $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes'); return ($result === 'yes'); } + + /** + * @inheritdoc + */ + public function getAccessList($nodes, $currentAccess) { + $ids = []; + foreach ($nodes as $node) { + $ids[] = $node->getId(); + } + + $qb = $this->dbConnection->getQueryBuilder(); + $qb->select('share_with', 'token', 'file_source') + ->from('share') + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_REMOTE))) + ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))) + ->andWhere($qb->expr()->orX( + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) + )); + $cursor = $qb->execute(); + + if ($currentAccess === false) { + $remote = $cursor->fetch() !== false; + $cursor->closeCursor(); + + return ['remote' => $remote]; + } + + $remote = []; + while ($row = $cursor->fetch()) { + $remote[$row['share_with']] = [ + 'node_id' => $row['file_source'], + 'token' => $row['token'], + ]; + } + $cursor->closeCursor(); + + return ['remote' => $remote]; + } } diff --git a/apps/federatedfilesharing/tests/FederatedShareProviderTest.php b/apps/federatedfilesharing/tests/FederatedShareProviderTest.php index d9bc9a7e2ad..e01e02c83ba 100644 --- a/apps/federatedfilesharing/tests/FederatedShareProviderTest.php +++ b/apps/federatedfilesharing/tests/FederatedShareProviderTest.php @@ -53,7 +53,7 @@ class FederatedShareProviderTest extends \Test\TestCase { protected $addressHandler; /** @var Notifications | \PHPUnit_Framework_MockObject_MockObject */ protected $notifications; - /** @var TokenHandler */ + /** @var TokenHandler|\PHPUnit_Framework_MockObject_MockObject */ protected $tokenHandler; /** @var IL10N */ protected $l; @@ -788,4 +788,60 @@ class FederatedShareProviderTest extends \Test\TestCase { $u1->delete(); $u2->delete(); } + + public function testGetAccessList() { + $userManager = \OC::$server->getUserManager(); + $rootFolder = \OC::$server->getRootFolder(); + + $u1 = $userManager->createUser('testFed', md5(time())); + + $folder1 = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo'); + $file1 = $folder1->newFile('bar1'); + + $this->tokenHandler->expects($this->exactly(2)) + ->method('generateToken') + ->willReturnOnConsecutiveCalls('token1', 'token2'); + $this->notifications->expects($this->atLeastOnce()) + ->method('sendRemoteShare') + ->willReturn(true); + + $result = $this->provider->getAccessList([$file1], true); + $this->assertEquals(['remote' => []], $result); + + $result = $this->provider->getAccessList([$file1], false); + $this->assertEquals(['remote' => false], $result); + + $share1 = $this->shareManager->newShare(); + $share1->setSharedWith('user@server.com') + ->setSharedBy($u1->getUID()) + ->setShareOwner($u1->getUID()) + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1); + $this->provider->create($share1); + + $share2 = $this->shareManager->newShare(); + $share2->setSharedWith('foobar@localhost') + ->setSharedBy($u1->getUID()) + ->setShareOwner($u1->getUID()) + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($file1); + $this->provider->create($share2); + + $result = $this->provider->getAccessList([$file1], true); + $this->assertEquals(['remote' => [ + 'user@server.com' => [ + 'token' => 'token1', + 'node_id' => $file1->getId(), + ], + 'foobar@localhost' => [ + 'token' => 'token2', + 'node_id' => $file1->getId(), + ], + ]], $result); + + $result = $this->provider->getAccessList([$file1], false); + $this->assertEquals(['remote' => true], $result); + + $u1->delete(); + } } diff --git a/apps/sharebymail/lib/ShareByMailProvider.php b/apps/sharebymail/lib/ShareByMailProvider.php index 0b959ce4265..83170c5648c 100644 --- a/apps/sharebymail/lib/ShareByMailProvider.php +++ b/apps/sharebymail/lib/ShareByMailProvider.php @@ -834,4 +834,31 @@ class ShareByMailProvider implements IShareProvider { return $shares; } + /** + * @inheritdoc + */ + public function getAccessList($nodes, $currentAccess) { + $ids = []; + foreach ($nodes as $node) { + $ids[] = $node->getId(); + } + + $qb = $this->dbConnection->getQueryBuilder(); + $qb->select('share_with') + ->from('share') + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))) + ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))) + ->andWhere($qb->expr()->orX( + $qb->expr()->eq('item_type', $qb->createNamedParameter('file')), + $qb->expr()->eq('item_type', $qb->createNamedParameter('folder')) + )) + ->setMaxResults(1); + $cursor = $qb->execute(); + + $mail = $cursor->fetch() !== false; + $cursor->closeCursor(); + + return ['public' => $mail]; + } + } diff --git a/apps/sharebymail/tests/ShareByMailProviderTest.php b/apps/sharebymail/tests/ShareByMailProviderTest.php index 288ebb4bb45..13fb5d03bd8 100644 --- a/apps/sharebymail/tests/ShareByMailProviderTest.php +++ b/apps/sharebymail/tests/ShareByMailProviderTest.php @@ -664,4 +664,66 @@ class ShareByMailProviderTest extends TestCase { $u2->delete(); } + public function testGetAccessList() { + $userManager = \OC::$server->getUserManager(); + $rootFolder = \OC::$server->getRootFolder(); + + $provider = $this->getInstance(['sendMailNotification', 'createActivity']); + + $u1 = $userManager->createUser('testFed', md5(time())); + $u2 = $userManager->createUser('testFed2', md5(time())); + + $folder = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo'); + + $accessList = $provider->getAccessList([$folder], true); + $this->assertArrayHasKey('public', $accessList); + $this->assertFalse($accessList['public']); + $accessList = $provider->getAccessList([$folder], false); + $this->assertArrayHasKey('public', $accessList); + $this->assertFalse($accessList['public']); + + $share1 = $this->shareManager->newShare(); + $share1->setSharedWith('user@server.com') + ->setSharedBy($u1->getUID()) + ->setShareOwner($u1->getUID()) + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($folder); + $share1 = $provider->create($share1); + + $share2 = $this->shareManager->newShare(); + $share2->setSharedWith('user2@server.com') + ->setSharedBy($u2->getUID()) + ->setShareOwner($u1->getUID()) + ->setPermissions(\OCP\Constants::PERMISSION_READ) + ->setNode($folder); + $share2 = $provider->create($share2); + + $accessList = $provider->getAccessList([$folder], true); + $this->assertArrayHasKey('public', $accessList); + $this->assertTrue($accessList['public']); + $accessList = $provider->getAccessList([$folder], false); + $this->assertArrayHasKey('public', $accessList); + $this->assertTrue($accessList['public']); + + $provider->delete($share2); + + $accessList = $provider->getAccessList([$folder], true); + $this->assertArrayHasKey('public', $accessList); + $this->assertTrue($accessList['public']); + $accessList = $provider->getAccessList([$folder], false); + $this->assertArrayHasKey('public', $accessList); + $this->assertTrue($accessList['public']); + + $provider->delete($share1); + + $accessList = $provider->getAccessList([$folder], true); + $this->assertArrayHasKey('public', $accessList); + $this->assertFalse($accessList['public']); + $accessList = $provider->getAccessList([$folder], false); + $this->assertArrayHasKey('public', $accessList); + $this->assertFalse($accessList['public']); + + $u1->delete(); + $u2->delete(); + } } |