diff options
author | Björn Schießle <schiessle@owncloud.com> | 2016-05-11 20:48:27 +0200 |
---|---|---|
committer | Björn Schießle <schiessle@owncloud.com> | 2016-05-20 21:15:15 +0200 |
commit | 7b25839bd51b3b6cd920209c254f014c03437113 (patch) | |
tree | 2e9885bdb068d56ff7bb9fde344c4da0cc95e2d3 | |
parent | 81e3787f9cb4936f186b25e0dccb07b4e6dcce83 (diff) | |
download | nextcloud-server-7b25839bd51b3b6cd920209c254f014c03437113.tar.gz nextcloud-server-7b25839bd51b3b6cd920209c254f014c03437113.zip |
use share initiator as fall back to access the file
in case of federated re-shares the owner can be a remote user.
Therefore we can't always use to owner to access the local file
-rw-r--r-- | apps/federatedfilesharing/lib/AppInfo/Application.php | 3 | ||||
-rw-r--r-- | apps/federatedfilesharing/lib/FederatedShareProvider.php | 13 | ||||
-rw-r--r-- | apps/federatedfilesharing/tests/FederatedShareProviderTest.php | 52 | ||||
-rw-r--r-- | apps/files_sharing/api/share20ocs.php | 12 | ||||
-rw-r--r-- | apps/files_sharing/tests/api/share20ocstest.php | 2 | ||||
-rw-r--r-- | lib/private/Share20/DefaultShareProvider.php | 2 | ||||
-rw-r--r-- | lib/private/Share20/Manager.php | 15 | ||||
-rw-r--r-- | lib/private/Share20/ProviderFactory.php | 3 | ||||
-rw-r--r-- | lib/private/Share20/Share.php | 17 | ||||
-rw-r--r-- | tests/lib/Share20/DefaultShareProviderTest.php | 8 | ||||
-rw-r--r-- | tests/lib/Share20/ManagerTest.php | 5 | ||||
-rw-r--r-- | tests/lib/Share20/ShareTest.php | 3 |
12 files changed, 111 insertions, 24 deletions
diff --git a/apps/federatedfilesharing/lib/AppInfo/Application.php b/apps/federatedfilesharing/lib/AppInfo/Application.php index 5a213aec8e2..d1b0646ba5b 100644 --- a/apps/federatedfilesharing/lib/AppInfo/Application.php +++ b/apps/federatedfilesharing/lib/AppInfo/Application.php @@ -81,7 +81,8 @@ class Application extends App { \OC::$server->getL10N('federatedfilesharing'), \OC::$server->getLogger(), \OC::$server->getLazyRootFolder(), - \OC::$server->getConfig() + \OC::$server->getConfig(), + \OC::$server->getUserManager() ); } diff --git a/apps/federatedfilesharing/lib/FederatedShareProvider.php b/apps/federatedfilesharing/lib/FederatedShareProvider.php index 590c61559bf..762b015d280 100644 --- a/apps/federatedfilesharing/lib/FederatedShareProvider.php +++ b/apps/federatedfilesharing/lib/FederatedShareProvider.php @@ -23,13 +23,12 @@ namespace OCA\FederatedFileSharing; -use OC\Files\View; use OC\Share20\Share; use OCP\Files\IRootFolder; -use OCP\IAppConfig; use OCP\IConfig; use OCP\IL10N; use OCP\ILogger; +use OCP\IUserManager; use OCP\Share\IShare; use OCP\Share\IShareProvider; use OC\Share20\Exception\InvalidShare; @@ -74,6 +73,9 @@ class FederatedShareProvider implements IShareProvider { /** @var string */ private $externalShareTable = 'share_external'; + /** @var IUserManager */ + private $userManager; + /** * DefaultShareProvider constructor. * @@ -85,6 +87,7 @@ class FederatedShareProvider implements IShareProvider { * @param ILogger $logger * @param IRootFolder $rootFolder * @param IConfig $config + * @param IUserManager $userManager */ public function __construct( IDBConnection $connection, @@ -94,7 +97,8 @@ class FederatedShareProvider implements IShareProvider { IL10N $l10n, ILogger $logger, IRootFolder $rootFolder, - IConfig $config + IConfig $config, + IUserManager $userManager ) { $this->dbConnection = $connection; $this->addressHandler = $addressHandler; @@ -104,6 +108,7 @@ class FederatedShareProvider implements IShareProvider { $this->logger = $logger; $this->rootFolder = $rootFolder; $this->config = $config; + $this->userManager = $userManager; } /** @@ -699,7 +704,7 @@ class FederatedShareProvider implements IShareProvider { */ private function createShare($data) { - $share = new Share($this->rootFolder); + $share = new Share($this->rootFolder, $this->userManager); $share->setId((int)$data['id']) ->setShareType((int)$data['share_type']) ->setPermissions((int)$data['permissions']) diff --git a/apps/federatedfilesharing/tests/FederatedShareProviderTest.php b/apps/federatedfilesharing/tests/FederatedShareProviderTest.php index 10770f00af5..9b3edf0398d 100644 --- a/apps/federatedfilesharing/tests/FederatedShareProviderTest.php +++ b/apps/federatedfilesharing/tests/FederatedShareProviderTest.php @@ -30,6 +30,7 @@ use OCP\IConfig; use OCP\IDBConnection; use OCP\IL10N; use OCP\ILogger; +use OCP\IUserManager; use OCP\Share\IManager; /** @@ -56,6 +57,8 @@ class FederatedShareProviderTest extends \Test\TestCase { protected $rootFolder; /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */ protected $config; + /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */ + protected $userManager; /** @var IManager */ protected $shareManager; @@ -81,7 +84,9 @@ class FederatedShareProviderTest extends \Test\TestCase { $this->logger = $this->getMock('OCP\ILogger'); $this->rootFolder = $this->getMock('OCP\Files\IRootFolder'); $this->config = $this->getMock('OCP\IConfig'); - $this->addressHandler = new AddressHandler(\OC::$server->getURLGenerator(), $this->l); + $this->userManager = $this->getMock('OCP\IUserManager'); + //$this->addressHandler = new AddressHandler(\OC::$server->getURLGenerator(), $this->l); + $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')->disableOriginalConstructor()->getMock(); $this->userManager->expects($this->any())->method('userExists')->willReturn(true); @@ -93,7 +98,8 @@ class FederatedShareProviderTest extends \Test\TestCase { $this->l, $this->logger, $this->rootFolder, - $this->config + $this->config, + $this->userManager ); $this->shareManager = \OC::$server->getShareManager(); @@ -120,6 +126,11 @@ class FederatedShareProviderTest extends \Test\TestCase { $this->tokenHandler->method('generateToken')->willReturn('token'); + $this->addressHandler->expects($this->any())->method('generateRemoteURL') + ->willReturn('http://localhost/'); + $this->addressHandler->expects($this->any())->method('splitUserRemote') + ->willReturn(['user', 'server.com']); + $this->notifications->expects($this->once()) ->method('sendRemoteShare') ->with( @@ -186,6 +197,11 @@ class FederatedShareProviderTest extends \Test\TestCase { $this->tokenHandler->method('generateToken')->willReturn('token'); + $this->addressHandler->expects($this->any())->method('generateRemoteURL') + ->willReturn('http://localhost/'); + $this->addressHandler->expects($this->any())->method('splitUserRemote') + ->willReturn(['user', 'server.com']); + $this->notifications->expects($this->once()) ->method('sendRemoteShare') ->with( @@ -233,7 +249,10 @@ class FederatedShareProviderTest extends \Test\TestCase { $node->method('getId')->willReturn(42); $node->method('getName')->willReturn('myFile'); - $shareWith = 'sharedBy@' . $this->addressHandler->generateRemoteURL(); + $this->addressHandler->expects($this->any())->method('compareAddresses') + ->willReturn(true); + + $shareWith = 'sharedBy@localhost'; $share->setSharedWith($shareWith) ->setSharedBy('sharedBy') @@ -269,6 +288,10 @@ class FederatedShareProviderTest extends \Test\TestCase { $node->method('getId')->willReturn(42); $node->method('getName')->willReturn('myFile'); + + $this->addressHandler->expects($this->any())->method('splitUserRemote') + ->willReturn(['user', 'server.com']); + $share->setSharedWith('user@server.com') ->setSharedBy('sharedBy') ->setShareOwner('shareOwner') @@ -277,6 +300,9 @@ class FederatedShareProviderTest extends \Test\TestCase { $this->tokenHandler->method('generateToken')->willReturn('token'); + $this->addressHandler->expects($this->any())->method('generateRemoteURL') + ->willReturn('http://localhost/'); + $this->notifications->expects($this->once()) ->method('sendRemoteShare') ->with( @@ -328,6 +354,10 @@ class FederatedShareProviderTest extends \Test\TestCase { $node->method('getId')->willReturn(42); $node->method('getName')->willReturn('myFile'); + + $this->addressHandler->expects($this->any())->method('splitUserRemote') + ->willReturn(['user', 'server.com']); + $share->setSharedWith('user@server.com') ->setSharedBy($sharedBy) ->setShareOwner($owner) @@ -335,6 +365,8 @@ class FederatedShareProviderTest extends \Test\TestCase { ->setNode($node); $this->tokenHandler->method('generateToken')->willReturn('token'); + $this->addressHandler->expects($this->any())->method('generateRemoteURL') + ->willReturn('http://localhost/'); $this->notifications->expects($this->once()) ->method('sendRemoteShare') @@ -379,6 +411,12 @@ class FederatedShareProviderTest extends \Test\TestCase { $node->method('getId')->willReturn(42); $node->method('getName')->willReturn('myFile'); + $this->addressHandler->expects($this->at(0))->method('splitUserRemote') + ->willReturn(['user', 'server.com']); + + $this->addressHandler->expects($this->at(1))->method('splitUserRemote') + ->willReturn(['user2', 'server.com']); + $this->tokenHandler->method('generateToken')->willReturn('token'); $this->notifications ->method('sendRemoteShare') @@ -485,6 +523,14 @@ class FederatedShareProviderTest extends \Test\TestCase { $node->method('getId')->willReturn(42); $node->method('getName')->willReturn('myFile'); + $this->addressHandler->expects($this->any())->method('splitUserRemote') + ->willReturnCallback(function ($uid) { + if ($uid === 'user@server.com') { + return ['user', 'server.com']; + } + return ['user2', 'server.com']; + }); + $this->tokenHandler->method('generateToken')->willReturn('token'); $this->notifications ->method('sendRemoteShare') diff --git a/apps/files_sharing/api/share20ocs.php b/apps/files_sharing/api/share20ocs.php index af762845326..28166b943b8 100644 --- a/apps/files_sharing/api/share20ocs.php +++ b/apps/files_sharing/api/share20ocs.php @@ -99,7 +99,15 @@ class Share20OCS { */ protected function formatShare(\OCP\Share\IShare $share) { $sharedBy = $this->userManager->get($share->getSharedBy()); - $shareOwner = $this->userManager->get($share->getShareOwner()); + // for federated shares the owner can be a remote user, in this + // case we use the initiator + if ($this->userManager->userExists($share->getShareOwner())) { + $shareOwner = $this->userManager->get($share->getShareOwner()); + $localUser = $share->getShareOwner(); + } else { + $shareOwner = $this->userManager->get($share->getSharedBy()); + $localUser = $share->getSharedBy(); + } $result = [ 'id' => $share->getId(), 'share_type' => $share->getShareType(), @@ -115,7 +123,7 @@ class Share20OCS { ]; $node = $share->getNode(); - $result['path'] = $this->rootFolder->getUserFolder($share->getShareOwner())->getRelativePath($node->getPath()); + $result['path'] = $this->rootFolder->getUserFolder($localUser)->getRelativePath($node->getPath()); if ($node instanceOf \OCP\Files\Folder) { $result['item_type'] = 'folder'; } else { diff --git a/apps/files_sharing/tests/api/share20ocstest.php b/apps/files_sharing/tests/api/share20ocstest.php index ffb74da2af7..96ce34f963c 100644 --- a/apps/files_sharing/tests/api/share20ocstest.php +++ b/apps/files_sharing/tests/api/share20ocstest.php @@ -82,6 +82,8 @@ class Share20OCSTest extends \Test\TestCase { $this->currentUser = $this->getMock('OCP\IUser'); $this->currentUser->method('getUID')->willReturn('currentUser'); + $this->userManager->expects($this->any())->method('userExists')->willReturn(true); + $this->l = $this->getMock('\OCP\IL10N'); $this->l->method('t') ->will($this->returnCallback(function($text, $parameters = []) { diff --git a/lib/private/Share20/DefaultShareProvider.php b/lib/private/Share20/DefaultShareProvider.php index f0de39fdad3..8a39c18a495 100644 --- a/lib/private/Share20/DefaultShareProvider.php +++ b/lib/private/Share20/DefaultShareProvider.php @@ -733,7 +733,7 @@ class DefaultShareProvider implements IShareProvider { * @throws InvalidShare */ private function createShare($data) { - $share = new Share($this->rootFolder); + $share = new Share($this->rootFolder, $this->userManager); $share->setId((int)$data['id']) ->setShareType((int)$data['share_type']) ->setPermissions((int)$data['permissions']) diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index dee9e0cdd21..3568995472a 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -201,7 +201,12 @@ class Manager implements IManager { } // And you can't share your rootfolder - if ($this->rootFolder->getUserFolder($share->getSharedBy())->getPath() === $share->getNode()->getPath()) { + if ($this->userManager->userExists($share->getSharedBy())) { + $sharedPath = $this->rootFolder->getUserFolder($share->getSharedBy())->getPath(); + } else { + $sharedPath = $this->rootFolder->getUserFolder($share->getShareOwner())->getPath(); + } + if ($sharedPath === $share->getNode()->getPath()) { throw new \InvalidArgumentException('You can\'t share your root folder'); } @@ -713,7 +718,11 @@ class Manager implements IManager { } if ($share->getPermissions() !== $originalShare->getPermissions()) { - $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); + if ($this->userManager->userExists($share->getShareOwner())) { + $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); + } else { + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); + } \OC_Hook::emit('OCP\Share', 'post_update_permissions', array( 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', 'itemSource' => $share->getNode()->getId(), @@ -1107,7 +1116,7 @@ class Manager implements IManager { * @return \OCP\Share\IShare; */ public function newShare() { - return new \OC\Share20\Share($this->rootFolder); + return new \OC\Share20\Share($this->rootFolder, $this->userManager); } /** diff --git a/lib/private/Share20/ProviderFactory.php b/lib/private/Share20/ProviderFactory.php index 0bedfb84fc7..b436a7bc5f3 100644 --- a/lib/private/Share20/ProviderFactory.php +++ b/lib/private/Share20/ProviderFactory.php @@ -115,7 +115,8 @@ class ProviderFactory implements IProviderFactory { $l, $this->serverContainer->getLogger(), $this->serverContainer->getLazyRootFolder(), - $this->serverContainer->getConfig() + $this->serverContainer->getConfig(), + $this->serverContainer->getUserManager() ); } diff --git a/lib/private/Share20/Share.php b/lib/private/Share20/Share.php index c361f01216f..f56fd94b409 100644 --- a/lib/private/Share20/Share.php +++ b/lib/private/Share20/Share.php @@ -24,8 +24,7 @@ use OCP\Files\File; use OCP\Files\IRootFolder; use OCP\Files\Node; use OCP\Files\NotFoundException; -use OCP\IUser; -use OCP\IGroup; +use OCP\IUserManager; use OCP\Share\Exceptions\IllegalIDChangeException; class Share implements \OCP\Share\IShare { @@ -68,8 +67,12 @@ class Share implements \OCP\Share\IShare { /** @var IRootFolder */ private $rootFolder; - public function __construct(IRootFolder $rootFolder) { + /** @var IUserManager */ + private $userManager; + + public function __construct(IRootFolder $rootFolder, IUserManager $userManager) { $this->rootFolder = $rootFolder; + $this->userManager = $userManager; } /** @@ -145,7 +148,13 @@ class Share implements \OCP\Share\IShare { throw new NotFoundException(); } - $userFolder = $this->rootFolder->getUserFolder($this->shareOwner); + // for federated shares the owner can be a remote user, in this + // case we use the initiator + if($this->userManager->userExists($this->shareOwner)) { + $userFolder = $this->rootFolder->getUserFolder($this->shareOwner); + } else { + $userFolder = $this->rootFolder->getUserFolder($this->sharedBy); + } $nodes = $userFolder->getById($this->fileId); if (empty($nodes)) { diff --git a/tests/lib/Share20/DefaultShareProviderTest.php b/tests/lib/Share20/DefaultShareProviderTest.php index 44a48535b9b..6ef00721a70 100644 --- a/tests/lib/Share20/DefaultShareProviderTest.php +++ b/tests/lib/Share20/DefaultShareProviderTest.php @@ -57,6 +57,8 @@ class DefaultShareProviderTest extends \Test\TestCase { $this->groupManager = $this->getMock('OCP\IGroupManager'); $this->rootFolder = $this->getMock('OCP\Files\IRootFolder'); + $this->userManager->expects($this->any())->method('userExists')->willReturn(true); + //Empty share table $this->dbConn->getQueryBuilder()->delete('share')->execute(); @@ -587,7 +589,7 @@ class DefaultShareProviderTest extends \Test\TestCase { } public function testCreateUserShare() { - $share = new \OC\Share20\Share($this->rootFolder); + $share = new \OC\Share20\Share($this->rootFolder, $this->userManager); $shareOwner = $this->getMock('OCP\IUser'); $shareOwner->method('getUID')->WillReturn('shareOwner'); @@ -635,7 +637,7 @@ class DefaultShareProviderTest extends \Test\TestCase { } public function testCreateGroupShare() { - $share = new \OC\Share20\Share($this->rootFolder); + $share = new \OC\Share20\Share($this->rootFolder, $this->userManager); $shareOwner = $this->getMock('\OCP\IUser'); $shareOwner->method('getUID')->willReturn('shareOwner'); @@ -683,7 +685,7 @@ class DefaultShareProviderTest extends \Test\TestCase { } public function testCreateLinkShare() { - $share = new \OC\Share20\Share($this->rootFolder); + $share = new \OC\Share20\Share($this->rootFolder, $this->userManager); $shareOwner = $this->getMock('\OCP\IUser'); $shareOwner->method('getUID')->willReturn('shareOwner'); diff --git a/tests/lib/Share20/ManagerTest.php b/tests/lib/Share20/ManagerTest.php index 7d79150449c..a50ea6c892a 100644 --- a/tests/lib/Share20/ManagerTest.php +++ b/tests/lib/Share20/ManagerTest.php @@ -2283,6 +2283,9 @@ class ManagerTest extends \Test\TestCase { } public function testUpdateShareUser() { + + $this->userManager->expects($this->any())->method('userExists')->willReturn(true); + $manager = $this->createManagerMock() ->setMethods([ 'canShare', @@ -2567,4 +2570,4 @@ class DummyFactory implements IProviderFactory { public function getProviderForType($shareType) { return $this->provider; } -}
\ No newline at end of file +} diff --git a/tests/lib/Share20/ShareTest.php b/tests/lib/Share20/ShareTest.php index fdfc69f6577..91bd2fe84b6 100644 --- a/tests/lib/Share20/ShareTest.php +++ b/tests/lib/Share20/ShareTest.php @@ -36,7 +36,8 @@ class ShareTest extends \Test\TestCase { public function setUp() { $this->rootFolder = $this->getMock('\OCP\Files\IRootFolder'); - $this->share = new \OC\Share20\Share($this->rootFolder); + $this->userManager = $this->getMock('OCP\IUserManager'); + $this->share = new \OC\Share20\Share($this->rootFolder, $this->userManager); } /** |