From f5d0b464a718a32d78226836417c7ab35203f275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Schie=C3=9Fle?= Date: Mon, 23 May 2016 11:48:14 +0200 Subject: [PATCH 01/11] create a real federated share if a user add a public link to his ownCloud this way the owner sees all mounted public links and control them individually --- apps/federatedfilesharing/appinfo/app.php | 2 +- apps/federatedfilesharing/appinfo/routes.php | 26 ++++++ .../lib/AppInfo/Application.php | 4 + .../Controller/SaveToOwnCloudController.php | 88 +++++++++++++++++++ .../lib/FederatedShareProvider.php | 8 +- apps/files_sharing/js/public.js | 75 +++++++--------- apps/files_sharing/templates/public.php | 2 +- 7 files changed, 157 insertions(+), 48 deletions(-) create mode 100644 apps/federatedfilesharing/appinfo/routes.php create mode 100644 apps/federatedfilesharing/lib/Controller/SaveToOwnCloudController.php diff --git a/apps/federatedfilesharing/appinfo/app.php b/apps/federatedfilesharing/appinfo/app.php index 5891b9cbbc1..8abdf8cac19 100644 --- a/apps/federatedfilesharing/appinfo/app.php +++ b/apps/federatedfilesharing/appinfo/app.php @@ -20,7 +20,7 @@ * */ -$app = new \OCA\FederatedFileSharing\AppInfo\Application('federatedfilesharing'); +$app = new \OCA\FederatedFileSharing\AppInfo\Application(); use OCA\FederatedFileSharing\Notifier; diff --git a/apps/federatedfilesharing/appinfo/routes.php b/apps/federatedfilesharing/appinfo/routes.php new file mode 100644 index 00000000000..534733e001f --- /dev/null +++ b/apps/federatedfilesharing/appinfo/routes.php @@ -0,0 +1,26 @@ + + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +return [ + 'routes' => [ + ['name' => 'SaveToOwnCloud#saveToOwnCloud', 'url' => '/saveToOwnCloud', 'verb' => 'POST'], + ] +]; diff --git a/apps/federatedfilesharing/lib/AppInfo/Application.php b/apps/federatedfilesharing/lib/AppInfo/Application.php index 4faebea7b0a..ea5018e6a90 100644 --- a/apps/federatedfilesharing/lib/AppInfo/Application.php +++ b/apps/federatedfilesharing/lib/AppInfo/Application.php @@ -31,6 +31,10 @@ class Application extends App { /** @var FederatedShareProvider */ protected $federatedShareProvider; + public function __construct() { + parent::__construct('federatedfilesharing'); + } + /** * register personal and admin settings page */ diff --git a/apps/federatedfilesharing/lib/Controller/SaveToOwnCloudController.php b/apps/federatedfilesharing/lib/Controller/SaveToOwnCloudController.php new file mode 100644 index 00000000000..a20806e6abb --- /dev/null +++ b/apps/federatedfilesharing/lib/Controller/SaveToOwnCloudController.php @@ -0,0 +1,88 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + + +namespace OCA\FederatedFileSharing\Controller; + +use OC\HintException; +use OCA\FederatedFileSharing\AddressHandler; +use OCA\FederatedFileSharing\FederatedShareProvider; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\JSONResponse; +use OCP\IRequest; +use OCP\Share\IManager; + +class SaveToOwnCloudController extends Controller { + + /** @var FederatedShareProvider */ + private $federatedShareProvider; + + /** @var AddressHandler */ + private $addressHandler; + + /** @var IManager */ + private $shareManager; + + public function __construct($appName, + IRequest $request, + FederatedShareProvider $federatedShareProvider, + IManager $shareManager, + AddressHandler $addressHandler) { + parent::__construct($appName, $request); + + $this->federatedShareProvider = $federatedShareProvider; + $this->shareManager = $shareManager; + $this->addressHandler = $addressHandler; + } + + /** + * save public link to my ownCloud by asking the owner to create a federated + * share with me + * + * @NoCSRFRequired + * @PublicPage + * + * @param string $shareWith + * @param string $token + * @return JSONResponse + */ + public function saveToOwnCloud($shareWith, $token) { + + try { + list(, $server) = $this->addressHandler->splitUserRemote($shareWith); + $share = $this->shareManager->getShareByToken($token); + } catch (HintException $e) { + return new JSONResponse(['message' => $e->getHint()], Http::STATUS_BAD_REQUEST); + } + + $share->setSharedWith($shareWith); + + try { + $this->federatedShareProvider->create($share); + } catch (\Exception $e) { + return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST); + } + + return new JSONResponse(['remoteUrl' => $server]); + } + +} diff --git a/apps/federatedfilesharing/lib/FederatedShareProvider.php b/apps/federatedfilesharing/lib/FederatedShareProvider.php index 1ea31f2dbc0..7b64b56a16d 100644 --- a/apps/federatedfilesharing/lib/FederatedShareProvider.php +++ b/apps/federatedfilesharing/lib/FederatedShareProvider.php @@ -135,7 +135,7 @@ class FederatedShareProvider implements IShareProvider { $itemType = $share->getNodeType(); $permissions = $share->getPermissions(); $sharedBy = $share->getSharedBy(); - + /* * Check if file is not already shared with the remote user */ @@ -626,7 +626,7 @@ class FederatedShareProvider implements IShareProvider { ->from('share') ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))); - + $cursor = $qb->execute(); $data = $cursor->fetch(); $cursor->closeCursor(); @@ -727,13 +727,13 @@ class FederatedShareProvider implements IShareProvider { $data = $cursor->fetch(); if ($data === false) { - throw new ShareNotFound(); + throw new ShareNotFound('Share not found', $this->l->t('Could not find share')); } try { $share = $this->createShareObject($data); } catch (InvalidShare $e) { - throw new ShareNotFound(); + throw new ShareNotFound('Share not found', $this->l->t('Could not find share')); } return $share; diff --git a/apps/files_sharing/js/public.js b/apps/files_sharing/js/public.js index 3befeaeda1f..38dc40eeb1e 100644 --- a/apps/files_sharing/js/public.js +++ b/apps/files_sharing/js/public.js @@ -244,10 +244,8 @@ OCA.Sharing.PublicApp = { var remote = $(this).find('input[type="text"]').val(); var token = $('#sharingToken').val(); var owner = $('#save').data('owner'); - var ownerDisplayName = $('#save').data('owner-display-name'); var name = $('#save').data('name'); - var isProtected = $('#save').data('protected') ? 1 : 0; - OCA.Sharing.PublicApp._saveToOwnCloud(remote, token, owner, ownerDisplayName, name, isProtected); + OCA.Sharing.PublicApp._saveToOwnCloud(remote, token); }); $('#remote_address').on("keyup paste", function() { @@ -294,50 +292,43 @@ OCA.Sharing.PublicApp = { this.fileList.changeDirectory(params.path || params.dir, false, true); }, - _saveToOwnCloud: function (remote, token, owner, ownerDisplayName, name, isProtected) { - var toggleLoading = function() { - var iconClass = $('#save-button-confirm').attr('class'); - var loading = iconClass.indexOf('icon-loading-small') !== -1; - if(loading) { - $('#save-button-confirm') - .removeClass("icon-loading-small") - .addClass("icon-confirm"); + _saveToOwnCloud: function (remote, token) { + + $.post( + OC.generateUrl('/apps/federatedfilesharing/saveToOwnCloud'), + { + 'shareWith': remote, + 'token': token } - else { - $('#save-button-confirm') - .removeClass("icon-confirm") - .addClass("icon-loading-small"); + ).done( + function (data) { + var url = data.remoteUrl; - } - }; - - toggleLoading(); - var location = window.location.protocol + '//' + window.location.host + OC.webroot; - - if(remote.substr(-1) !== '/') { - remote += '/' - }; - - var url = remote + 'index.php/apps/files#' + 'remote=' + encodeURIComponent(location) // our location is the remote for the other server - + "&token=" + encodeURIComponent(token) + "&owner=" + encodeURIComponent(owner) +"&ownerDisplayName=" + encodeURIComponent(ownerDisplayName) + "&name=" + encodeURIComponent(name) + "&protected=" + isProtected; - - - if (remote.indexOf('://') > 0) { - OC.redirect(url); - } else { - // if no protocol is specified, we automatically detect it by testing https and http - // this check needs to happen on the server due to the Content Security Policy directive - $.get(OC.generateUrl('apps/files_sharing/testremote'), {remote: remote}).then(function (protocol) { - if (protocol !== 'http' && protocol !== 'https') { - toggleLoading(); - OC.dialogs.alert(t('files_sharing', 'No compatible server found at {remote}', {remote: remote}), - t('files_sharing', 'Invalid server URL')); + if (url.indexOf('://') > 0) { + OC.redirect(url); } else { - OC.redirect(protocol + '://' + url); + // if no protocol is specified, we automatically detect it by testing https and http + // this check needs to happen on the server due to the Content Security Policy directive + $.get(OC.generateUrl('apps/files_sharing/testremote'), {remote: remote}).then(function (protocol) { + if (protocol !== 'http' && protocol !== 'https') { + toggleLoading(); + OC.dialogs.alert(t('files_sharing', 'No ownCloud installation (7 or higher) found at {remote}', {remote: remote}), + t('files_sharing', 'Invalid ownCloud url')); + } else { + OC.redirect(protocol + '://' + url); + } + }); } - }); - } + } + ).fail( + function (jqXHR) { + console.log("ERROR!"); + console.log(jqXHR); + OC.dialogs.alert(JSON.parse(jqXHR.responseText).message, + t('files_sharing', 'Failed to add the public link to your ownCloud')); + } + ); } }; diff --git a/apps/files_sharing/templates/public.php b/apps/files_sharing/templates/public.php index c15001ad24b..8b8267a4e3f 100644 --- a/apps/files_sharing/templates/public.php +++ b/apps/files_sharing/templates/public.php @@ -84,7 +84,7 @@ $maxUploadFilesize = min($upload_max_filesize, $post_max_size); data-owner-display-name="" data-owner="" data-name=""> From a0ae16aa1ab717613a11083868a4856795413991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Schie=C3=9Fle?= Date: Mon, 23 May 2016 14:57:22 +0200 Subject: [PATCH 02/11] add unit tests --- .../SaveToOwnCloudControllerTest.php | 161 ++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 apps/federatedfilesharing/tests/Controller/SaveToOwnCloudControllerTest.php diff --git a/apps/federatedfilesharing/tests/Controller/SaveToOwnCloudControllerTest.php b/apps/federatedfilesharing/tests/Controller/SaveToOwnCloudControllerTest.php new file mode 100644 index 00000000000..24bc18d5644 --- /dev/null +++ b/apps/federatedfilesharing/tests/Controller/SaveToOwnCloudControllerTest.php @@ -0,0 +1,161 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + + +namespace OCA\FederatedFileSharing\Tests\Controller; + +use OC\HintException; +use OCA\FederatedFileSharing\AddressHandler; +use OCA\FederatedFileSharing\Controller\SaveToOwnCloudController; +use OCA\FederatedFileSharing\FederatedShareProvider; +use OCP\AppFramework\Http; +use OCP\Files\IRootFolder; +use OCP\IUserManager; +use OCP\Share; +use OCP\Share\IManager; +use OCP\Share\IShare; + +class SaveToOwnCloudControllerTest extends \Test\TestCase { + + /** @var SaveToOwnCloudController */ + private $controller; + + /** @var \OCP\IRequest | \PHPUnit_Framework_MockObject_MockObject */ + private $request; + + /** @var FederatedShareProvider | \PHPUnit_Framework_MockObject_MockObject */ + private $federatedShareProvider; + + /** @var IManager | \PHPUnit_Framework_MockObject_MockObject */ + private $shareManager; + + /** @var AddressHandler | \PHPUnit_Framework_MockObject_MockObject */ + private $addressHandler; + + /** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */ + private $rootFolder; + + /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */ + private $userManager; + + /** @var IShare */ + private $share; + + public function setUp() { + parent::setUp(); + + $this->request = $this->getMock('OCP\IRequest'); + $this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider') + ->disableOriginalConstructor()->getMock(); + $this->shareManager = $this->getMock('OCP\Share\IManager'); + $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler') + ->disableOriginalConstructor()->getMock(); + $this->rootFolder = $this->getMock('OCP\Files\IRootFolder'); + $this->userManager = $this->getMock('OCP\IUserManager'); + $this->share = new \OC\Share20\Share($this->rootFolder, $this->userManager); + + $this->controller = new SaveToOwnCloudController( + 'federatedfilesharing', $this->request, + $this->federatedShareProvider, + $this->shareManager, + $this->addressHandler + ); + } + + /** + * @dataProvider dataTestSaveToOwnCloud + * + * @param string $shareWith + * @param bool $validShareWith + * @param string $token + * @param bool $validToken + * @param bool $createSuccessful + * @param string $expectedReturnData + */ + public function testSaveToOwnCloud($shareWith, $validShareWith, $token, $validToken, $createSuccessful, $expectedReturnData) { + $this->addressHandler->expects($this->any())->method('splitUserRemote') + ->with($shareWith) + ->willReturnCallback( + function($shareWith) use ($validShareWith, $expectedReturnData) { + if ($validShareWith) { + return ['user', 'server']; + } + throw new HintException($expectedReturnData, $expectedReturnData); + } + ); + + $share = $this->share; + + $this->shareManager->expects($this->any())->method('getShareByToken') + ->with($token) + ->willReturnCallback( + function ($token) use ($validToken, $share, $expectedReturnData) { + if ($validToken) { + return $share; + } + throw new HintException($expectedReturnData, $expectedReturnData); + } + ); + + $this->federatedShareProvider->expects($this->any())->method('create') + ->with($share) + ->willReturnCallback( + function (IShare $share) use ($createSuccessful, $shareWith, $expectedReturnData) { + $this->assertEquals($shareWith, $share->getSharedWith()); + if ($createSuccessful) { + return $share; + } + throw new HintException($expectedReturnData, $expectedReturnData); + } + ); + + $result = $this->controller->saveToOwnCloud($shareWith, $token); + + $errorCase = !$validShareWith || !$validToken || !$createSuccessful; + + if ($errorCase) { + $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus()); + $this->assertTrue(isset($result->getData()['message'])); + $this->assertSame($expectedReturnData, $result->getData()['message']); + } else { + $this->assertSame(Http::STATUS_OK, $result->getStatus()); + $this->assertTrue(isset($result->getData()['remoteUrl'])); + $this->assertSame($expectedReturnData, $result->getData()['remoteUrl']); + + } + + } + + public function dataTestSaveToOwnCloud() { + return [ + //shareWith, validShareWith, token, validToken, createSuccessful, expectedReturnData + ['user@server', true, 'token', true, true, 'server'], + ['user@server', false, 'token', true, true, 'invalid federated cloud id'], + ['user@server', false, 'token', false, true, 'invalid federated cloud id'], + ['user@server', false, 'token', false, false, 'invalid federated cloud id'], + ['user@server', false, 'token', true, false, 'invalid federated cloud id'], + ['user@server', true, 'token', false, true, 'invalid token'], + ['user@server', true, 'token', false, false, 'invalid token'], + ['user@server', true, 'token', true, false, 'can not create share'] + ]; + } + +} From 1394b0afb9b1d3d7c8952faea85873e731ccb801 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 12 Jul 2016 12:04:41 +0200 Subject: [PATCH 03/11] replace 'ownCloud' with 'Nextcloud' --- apps/files_sharing/js/public.js | 2 +- apps/files_sharing/templates/public.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/files_sharing/js/public.js b/apps/files_sharing/js/public.js index 38dc40eeb1e..d4f3bd36a63 100644 --- a/apps/files_sharing/js/public.js +++ b/apps/files_sharing/js/public.js @@ -313,7 +313,7 @@ OCA.Sharing.PublicApp = { $.get(OC.generateUrl('apps/files_sharing/testremote'), {remote: remote}).then(function (protocol) { if (protocol !== 'http' && protocol !== 'https') { toggleLoading(); - OC.dialogs.alert(t('files_sharing', 'No ownCloud installation (7 or higher) found at {remote}', {remote: remote}), + OC.dialogs.alert(t('files_sharing', 'No Nextcloud installation (7 or higher) found at {remote}', {remote: remote}), t('files_sharing', 'Invalid ownCloud url')); } else { OC.redirect(protocol + '://' + url); diff --git a/apps/files_sharing/templates/public.php b/apps/files_sharing/templates/public.php index 8b8267a4e3f..8a906f02a7e 100644 --- a/apps/files_sharing/templates/public.php +++ b/apps/files_sharing/templates/public.php @@ -82,9 +82,9 @@ $maxUploadFilesize = min($upload_max_filesize, $post_max_size); ?> - + From 33a685bc41628e1236015bd79cc8f82b9cb6cabf Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 12 Jul 2016 14:03:29 +0200 Subject: [PATCH 04/11] continue to accept the URL of the remote server instead of the federated cloud id --- .../Controller/SaveToOwnCloudController.php | 40 ++++-- .../SaveToOwnCloudControllerTest.php | 16 ++- apps/files_sharing/ajax/external.php | 119 ++++-------------- apps/files_sharing/js/external.js | 6 +- apps/files_sharing/js/public.js | 86 ++++++++++--- 5 files changed, 138 insertions(+), 129 deletions(-) diff --git a/apps/federatedfilesharing/lib/Controller/SaveToOwnCloudController.php b/apps/federatedfilesharing/lib/Controller/SaveToOwnCloudController.php index a20806e6abb..2318d21afb5 100644 --- a/apps/federatedfilesharing/lib/Controller/SaveToOwnCloudController.php +++ b/apps/federatedfilesharing/lib/Controller/SaveToOwnCloudController.php @@ -29,6 +29,7 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; +use OCP\ISession; use OCP\Share\IManager; class SaveToOwnCloudController extends Controller { @@ -42,16 +43,32 @@ class SaveToOwnCloudController extends Controller { /** @var IManager */ private $shareManager; + /** @var ISession */ + private $session; + + /** + * SaveToOwnCloudController constructor. + * + * @param string $appName + * @param IRequest $request + * @param FederatedShareProvider $federatedShareProvider + * @param IManager $shareManager + * @param AddressHandler $addressHandler + * @param ISession $session + */ public function __construct($appName, - IRequest $request, - FederatedShareProvider $federatedShareProvider, - IManager $shareManager, - AddressHandler $addressHandler) { + IRequest $request, + FederatedShareProvider $federatedShareProvider, + IManager $shareManager, + AddressHandler $addressHandler, + ISession $session + ) { parent::__construct($appName, $request); $this->federatedShareProvider = $federatedShareProvider; $this->shareManager = $shareManager; $this->addressHandler = $addressHandler; + $this->session = $session; } /** @@ -63,9 +80,10 @@ class SaveToOwnCloudController extends Controller { * * @param string $shareWith * @param string $token + * @param string $password * @return JSONResponse */ - public function saveToOwnCloud($shareWith, $token) { + public function saveToOwnCloud($shareWith, $token, $password = '') { try { list(, $server) = $this->addressHandler->splitUserRemote($shareWith); @@ -74,6 +92,14 @@ class SaveToOwnCloudController extends Controller { return new JSONResponse(['message' => $e->getHint()], Http::STATUS_BAD_REQUEST); } + // make sure that user is authenticated in case of a password protected link + $storedPassword = $share->getPassword(); + $authenticated = $this->session->get('public_link_authenticated') === $share->getId() || + $this->shareManager->checkPassword($share, $password); + if (!empty($storedPassword) && !$authenticated ) { + return new JSONResponse(['message' => 'No permission to access the share'], Http::STATUS_BAD_REQUEST); + } + $share->setSharedWith($shareWith); try { @@ -81,8 +107,8 @@ class SaveToOwnCloudController extends Controller { } catch (\Exception $e) { return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST); } - + return new JSONResponse(['remoteUrl' => $server]); } - + } diff --git a/apps/federatedfilesharing/tests/Controller/SaveToOwnCloudControllerTest.php b/apps/federatedfilesharing/tests/Controller/SaveToOwnCloudControllerTest.php index 24bc18d5644..9189ac76601 100644 --- a/apps/federatedfilesharing/tests/Controller/SaveToOwnCloudControllerTest.php +++ b/apps/federatedfilesharing/tests/Controller/SaveToOwnCloudControllerTest.php @@ -28,6 +28,7 @@ use OCA\FederatedFileSharing\Controller\SaveToOwnCloudController; use OCA\FederatedFileSharing\FederatedShareProvider; use OCP\AppFramework\Http; use OCP\Files\IRootFolder; +use OCP\ISession; use OCP\IUserManager; use OCP\Share; use OCP\Share\IManager; @@ -56,6 +57,9 @@ class SaveToOwnCloudControllerTest extends \Test\TestCase { /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */ private $userManager; + /** @var ISession | \PHPUnit_Framework_MockObject_MockObject */ + private $session; + /** @var IShare */ private $share; @@ -71,12 +75,14 @@ class SaveToOwnCloudControllerTest extends \Test\TestCase { $this->rootFolder = $this->getMock('OCP\Files\IRootFolder'); $this->userManager = $this->getMock('OCP\IUserManager'); $this->share = new \OC\Share20\Share($this->rootFolder, $this->userManager); + $this->session = $this->getMock('OCP\ISession'); $this->controller = new SaveToOwnCloudController( 'federatedfilesharing', $this->request, $this->federatedShareProvider, $this->shareManager, - $this->addressHandler + $this->addressHandler, + $this->session ); } @@ -101,9 +107,9 @@ class SaveToOwnCloudControllerTest extends \Test\TestCase { throw new HintException($expectedReturnData, $expectedReturnData); } ); - + $share = $this->share; - + $this->shareManager->expects($this->any())->method('getShareByToken') ->with($token) ->willReturnCallback( @@ -114,7 +120,7 @@ class SaveToOwnCloudControllerTest extends \Test\TestCase { throw new HintException($expectedReturnData, $expectedReturnData); } ); - + $this->federatedShareProvider->expects($this->any())->method('create') ->with($share) ->willReturnCallback( @@ -141,7 +147,7 @@ class SaveToOwnCloudControllerTest extends \Test\TestCase { $this->assertSame($expectedReturnData, $result->getData()['remoteUrl']); } - + } public function dataTestSaveToOwnCloud() { diff --git a/apps/files_sharing/ajax/external.php b/apps/files_sharing/ajax/external.php index 4a7a6096c91..d57d333c7ee 100644 --- a/apps/files_sharing/ajax/external.php +++ b/apps/files_sharing/ajax/external.php @@ -42,112 +42,35 @@ if ($federatedShareProvider->isIncomingServer2serverShareEnabled() === false) { $token = $_POST['token']; $remote = $_POST['remote']; -$owner = $_POST['owner']; -$ownerDisplayName = $_POST['ownerDisplayName']; -$name = $_POST['name']; -$password = $_POST['password']; +$password = isset($_POST['password']) ? $_POST['password'] : ''; -// Check for invalid name -if(!\OCP\Util::isValidFileName($name)) { - \OCP\JSON::error(array('data' => array('message' => $l->t('The mountpoint name contains invalid characters.')))); - exit(); -} +$urlGenerator = \OC::$server->getURLGenerator(); -$currentUser = \OC::$server->getUserSession()->getUser()->getUID(); -$currentServer = \OC::$server->getURLGenerator()->getAbsoluteURL('/'); -if (\OC\Share\Helper::isSameUserOnSameServer($owner, $remote, $currentUser, $currentServer )) { - \OCP\JSON::error(array('data' => array('message' => $l->t('Not allowed to create a federated share with the same user server')))); - exit(); -} +$shareWith = \OCP\User::getUser() . '@' . $urlGenerator->getAbsoluteURL('/'); -$discoveryManager = new \OCA\FederatedFileSharing\DiscoveryManager( - \OC::$server->getMemCacheFactory(), - \OC::$server->getHTTPClientService() -); -$externalManager = new \OCA\Files_Sharing\External\Manager( - \OC::$server->getDatabaseConnection(), - \OC\Files\Filesystem::getMountManager(), - \OC\Files\Filesystem::getLoader(), - \OC::$server->getHTTPHelper(), - \OC::$server->getNotificationManager(), - $discoveryManager, - \OC::$server->getUserSession()->getUser()->getUID() -); +$httpClient = \OC::$server->getHTTPClientService()->newClient(); -// check for ssl cert -if (substr($remote, 0, 5) === 'https') { - try { - \OC::$server->getHTTPClientService()->newClient()->get($remote, [ - 'timeout' => 10, - 'connect_timeout' => 10, - ])->getBody(); - } catch (\Exception $e) { - \OCP\JSON::error(array('data' => array('message' => $l->t('Invalid or untrusted SSL certificate')))); - exit; - } -} +error_log("do th post"); -$mount = $externalManager->addShare($remote, $token, $password, $name, $ownerDisplayName, true); - -/** - * @var \OCA\Files_Sharing\External\Storage $storage - */ -$storage = $mount->getStorage(); try { - // check if storage exists - $storage->checkStorageAvailability(); -} catch (\OCP\Files\StorageInvalidException $e) { - // note: checkStorageAvailability will already remove the invalid share - \OCP\Util::writeLog( - 'files_sharing', - 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(), - \OCP\Util::DEBUG + $response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/saveToOwnCloud', + [ + 'body' => + [ + 'token' => $token, + 'shareWith' => rtrim($shareWith, '/'), + 'password' => $password + ] + ] ); - \OCP\JSON::error( - array( - 'data' => array( - 'message' => $l->t('Could not authenticate to remote share, password might be wrong') - ) - ) - ); - exit(); } catch (\Exception $e) { - \OCP\Util::writeLog( - 'files_sharing', - 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(), - \OCP\Util::DEBUG - ); - $externalManager->removeShare($mount->getMountPoint()); - \OCP\JSON::error(array('data' => array('message' => $l->t('Storage not valid')))); - exit(); -} -$result = $storage->file_exists(''); -if ($result) { - try { - $storage->getScanner()->scanAll(); - \OCP\JSON::success(); - } catch (\OCP\Files\StorageInvalidException $e) { - \OCP\Util::writeLog( - 'files_sharing', - 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(), - \OCP\Util::DEBUG - ); - \OCP\JSON::error(array('data' => array('message' => $l->t('Storage not valid')))); - } catch (\Exception $e) { - \OCP\Util::writeLog( - 'files_sharing', - 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(), - \OCP\Util::DEBUG - ); - \OCP\JSON::error(array('data' => array('message' => $l->t('Couldn\'t add remote share')))); + if (empty($password)) { + $message = $l->t("Couldn't establish a federated share."); + } else { + $message = $l->t("Couldn't establish a federated share, maybe the password was wrong."); } -} else { - $externalManager->removeShare($mount->getMountPoint()); - \OCP\Util::writeLog( - 'files_sharing', - 'Couldn\'t add remote share', - \OCP\Util::DEBUG - ); - \OCP\JSON::error(array('data' => array('message' => $l->t('Couldn\'t add remote share')))); + \OCP\JSON::error(array('data' => array('message' => $message))); + exit(); } +\OCP\JSON::success(array('data' => array('message' => $l->t('Federated Share request was successful, you will receive a invitation. Check your notifications.')))); diff --git a/apps/files_sharing/js/external.js b/apps/files_sharing/js/external.js index 0ca8213168e..be384818c3c 100644 --- a/apps/files_sharing/js/external.js +++ b/apps/files_sharing/js/external.js @@ -107,11 +107,7 @@ ownerDisplayName: share.ownerDisplayName || share.owner, name: share.name, password: password}, function(result) { - if (result.status === 'error') { - OC.Notification.showTemporary(result.data.message); - } else { - fileList.reload(); - } + OC.Notification.showTemporary(result.data.message); }); } }; diff --git a/apps/files_sharing/js/public.js b/apps/files_sharing/js/public.js index d4f3bd36a63..4293c7b310b 100644 --- a/apps/files_sharing/js/public.js +++ b/apps/files_sharing/js/public.js @@ -244,8 +244,10 @@ OCA.Sharing.PublicApp = { var remote = $(this).find('input[type="text"]').val(); var token = $('#sharingToken').val(); var owner = $('#save').data('owner'); + var ownerDisplayName = $('#save').data('owner-display-name'); var name = $('#save').data('name'); - OCA.Sharing.PublicApp._saveToOwnCloud(remote, token); + var isProtected = $('#save').data('protected') ? 1 : 0; + OCA.Sharing.PublicApp._saveToOwnCloud(remote, token, owner, ownerDisplayName, name, isProtected); }); $('#remote_address').on("keyup paste", function() { @@ -293,7 +295,72 @@ OCA.Sharing.PublicApp = { }, - _saveToOwnCloud: function (remote, token) { + /** + * fall back to old behaviour where we redirect the user to his server to mount + * the public link instead of creating a dedicated federated share + * + * @param remote + * @param token + * @param owner + * @param ownerDisplayName + * @param name + * @param isProtected + * @private + */ + _legacySaveToOwnCloud: function (remote, token, owner, ownerDisplayName, name, isProtected) { + + var location = window.location.protocol + '//' + window.location.host + OC.webroot; + + if(remote.substr(-1) !== '/') { + remote += '/' + } + + var url = remote + 'index.php/apps/files#' + 'remote=' + encodeURIComponent(location) // our location is the remote for the other server + + "&token=" + encodeURIComponent(token) + "&owner=" + encodeURIComponent(owner) +"&ownerDisplayName=" + encodeURIComponent(ownerDisplayName) + "&name=" + encodeURIComponent(name) + "&protected=" + isProtected; + + + if (remote.indexOf('://') > 0) { + OC.redirect(url); + } else { + // if no protocol is specified, we automatically detect it by testing https and http + // this check needs to happen on the server due to the Content Security Policy directive + $.get(OC.generateUrl('apps/files_sharing/testremote'), {remote: remote}).then(function (protocol) { + if (protocol !== 'http' && protocol !== 'https') { + OC.dialogs.alert(t('files_sharing', 'No compatible server found at {remote}', {remote: remote}), + t('files_sharing', 'Invalid server URL')); + } else { + OC.redirect(protocol + '://' + url); + } + }); + } + }, + + _saveToOwnCloud: function (remote, token, owner, ownerDisplayName, name, isProtected) { + + var toggleLoading = function() { + var iconClass = $('#save-button-confirm').attr('class'); + var loading = iconClass.indexOf('icon-loading-small') !== -1; + if(loading) { + $('#save-button-confirm') + .removeClass("icon-loading-small") + .addClass("icon-confirm"); + + } + else { + $('#save-button-confirm') + .removeClass("icon-confirm") + .addClass("icon-loading-small"); + + } + }; + + toggleLoading(); + + if (remote.indexOf('@') == -1) { + this._legacySaveToOwnCloud(remote, token, owner, ownerDisplayName, name, isProtected); + toggleLoading(); + return; + } $.post( OC.generateUrl('/apps/federatedfilesharing/saveToOwnCloud'), @@ -308,17 +375,7 @@ OCA.Sharing.PublicApp = { if (url.indexOf('://') > 0) { OC.redirect(url); } else { - // if no protocol is specified, we automatically detect it by testing https and http - // this check needs to happen on the server due to the Content Security Policy directive - $.get(OC.generateUrl('apps/files_sharing/testremote'), {remote: remote}).then(function (protocol) { - if (protocol !== 'http' && protocol !== 'https') { - toggleLoading(); - OC.dialogs.alert(t('files_sharing', 'No Nextcloud installation (7 or higher) found at {remote}', {remote: remote}), - t('files_sharing', 'Invalid ownCloud url')); - } else { - OC.redirect(protocol + '://' + url); - } - }); + OC.redirect('http://' + url); } } ).fail( @@ -326,7 +383,8 @@ OCA.Sharing.PublicApp = { console.log("ERROR!"); console.log(jqXHR); OC.dialogs.alert(JSON.parse(jqXHR.responseText).message, - t('files_sharing', 'Failed to add the public link to your ownCloud')); + t('files_sharing', 'Failed to add the public link to your Nextcloud')); + toggleLoading(); } ); } From 2f23054b0126ec3497a7b632ff2399f6f3843a0f Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 12 Jul 2016 16:30:09 +0200 Subject: [PATCH 05/11] s/ownCloud/Nextcloud --- apps/federatedfilesharing/appinfo/routes.php | 2 +- ...troller.php => SaveToNextcloudController.php} | 8 ++++---- ...est.php => SaveToNextcloudControllerTest.php} | 16 ++++++++-------- apps/files_sharing/ajax/external.php | 4 +--- apps/files_sharing/js/external.js | 1 - apps/files_sharing/js/public.js | 10 +++++----- 6 files changed, 19 insertions(+), 22 deletions(-) rename apps/federatedfilesharing/lib/Controller/{SaveToOwnCloudController.php => SaveToNextcloudController.php} (92%) rename apps/federatedfilesharing/tests/Controller/{SaveToOwnCloudControllerTest.php => SaveToNextcloudControllerTest.php} (91%) diff --git a/apps/federatedfilesharing/appinfo/routes.php b/apps/federatedfilesharing/appinfo/routes.php index 534733e001f..2a90ef5b59b 100644 --- a/apps/federatedfilesharing/appinfo/routes.php +++ b/apps/federatedfilesharing/appinfo/routes.php @@ -21,6 +21,6 @@ return [ 'routes' => [ - ['name' => 'SaveToOwnCloud#saveToOwnCloud', 'url' => '/saveToOwnCloud', 'verb' => 'POST'], + ['name' => 'SaveToNextcloud#saveToNextcloud', 'url' => '/saveToNextcloud', 'verb' => 'POST'], ] ]; diff --git a/apps/federatedfilesharing/lib/Controller/SaveToOwnCloudController.php b/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php similarity index 92% rename from apps/federatedfilesharing/lib/Controller/SaveToOwnCloudController.php rename to apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php index 2318d21afb5..f2788cf1ccd 100644 --- a/apps/federatedfilesharing/lib/Controller/SaveToOwnCloudController.php +++ b/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php @@ -32,7 +32,7 @@ use OCP\IRequest; use OCP\ISession; use OCP\Share\IManager; -class SaveToOwnCloudController extends Controller { +class SaveToNextcloudController extends Controller { /** @var FederatedShareProvider */ private $federatedShareProvider; @@ -47,7 +47,7 @@ class SaveToOwnCloudController extends Controller { private $session; /** - * SaveToOwnCloudController constructor. + * SaveToNextcloudController constructor. * * @param string $appName * @param IRequest $request @@ -72,7 +72,7 @@ class SaveToOwnCloudController extends Controller { } /** - * save public link to my ownCloud by asking the owner to create a federated + * save public link to my Nextcloud by asking the owner to create a federated * share with me * * @NoCSRFRequired @@ -83,7 +83,7 @@ class SaveToOwnCloudController extends Controller { * @param string $password * @return JSONResponse */ - public function saveToOwnCloud($shareWith, $token, $password = '') { + public function saveToNextcloud($shareWith, $token, $password = '') { try { list(, $server) = $this->addressHandler->splitUserRemote($shareWith); diff --git a/apps/federatedfilesharing/tests/Controller/SaveToOwnCloudControllerTest.php b/apps/federatedfilesharing/tests/Controller/SaveToNextcloudControllerTest.php similarity index 91% rename from apps/federatedfilesharing/tests/Controller/SaveToOwnCloudControllerTest.php rename to apps/federatedfilesharing/tests/Controller/SaveToNextcloudControllerTest.php index 9189ac76601..60136212c31 100644 --- a/apps/federatedfilesharing/tests/Controller/SaveToOwnCloudControllerTest.php +++ b/apps/federatedfilesharing/tests/Controller/SaveToNextcloudControllerTest.php @@ -24,7 +24,7 @@ namespace OCA\FederatedFileSharing\Tests\Controller; use OC\HintException; use OCA\FederatedFileSharing\AddressHandler; -use OCA\FederatedFileSharing\Controller\SaveToOwnCloudController; +use OCA\FederatedFileSharing\Controller\SaveToNextcloudController; use OCA\FederatedFileSharing\FederatedShareProvider; use OCP\AppFramework\Http; use OCP\Files\IRootFolder; @@ -34,9 +34,9 @@ use OCP\Share; use OCP\Share\IManager; use OCP\Share\IShare; -class SaveToOwnCloudControllerTest extends \Test\TestCase { +class SaveToNextcloudControllerTest extends \Test\TestCase { - /** @var SaveToOwnCloudController */ + /** @var SaveToNextcloudController */ private $controller; /** @var \OCP\IRequest | \PHPUnit_Framework_MockObject_MockObject */ @@ -77,7 +77,7 @@ class SaveToOwnCloudControllerTest extends \Test\TestCase { $this->share = new \OC\Share20\Share($this->rootFolder, $this->userManager); $this->session = $this->getMock('OCP\ISession'); - $this->controller = new SaveToOwnCloudController( + $this->controller = new SaveToNextcloudController( 'federatedfilesharing', $this->request, $this->federatedShareProvider, $this->shareManager, @@ -87,7 +87,7 @@ class SaveToOwnCloudControllerTest extends \Test\TestCase { } /** - * @dataProvider dataTestSaveToOwnCloud + * @dataProvider dataTestSaveToNextcloud * * @param string $shareWith * @param bool $validShareWith @@ -96,7 +96,7 @@ class SaveToOwnCloudControllerTest extends \Test\TestCase { * @param bool $createSuccessful * @param string $expectedReturnData */ - public function testSaveToOwnCloud($shareWith, $validShareWith, $token, $validToken, $createSuccessful, $expectedReturnData) { + public function testSaveToNextcloud($shareWith, $validShareWith, $token, $validToken, $createSuccessful, $expectedReturnData) { $this->addressHandler->expects($this->any())->method('splitUserRemote') ->with($shareWith) ->willReturnCallback( @@ -133,7 +133,7 @@ class SaveToOwnCloudControllerTest extends \Test\TestCase { } ); - $result = $this->controller->saveToOwnCloud($shareWith, $token); + $result = $this->controller->saveToNextcloud($shareWith, $token); $errorCase = !$validShareWith || !$validToken || !$createSuccessful; @@ -150,7 +150,7 @@ class SaveToOwnCloudControllerTest extends \Test\TestCase { } - public function dataTestSaveToOwnCloud() { + public function dataTestSaveToNextcloud() { return [ //shareWith, validShareWith, token, validToken, createSuccessful, expectedReturnData ['user@server', true, 'token', true, true, 'server'], diff --git a/apps/files_sharing/ajax/external.php b/apps/files_sharing/ajax/external.php index d57d333c7ee..20c3706c12d 100644 --- a/apps/files_sharing/ajax/external.php +++ b/apps/files_sharing/ajax/external.php @@ -50,10 +50,8 @@ $shareWith = \OCP\User::getUser() . '@' . $urlGenerator->getAbsoluteURL('/'); $httpClient = \OC::$server->getHTTPClientService()->newClient(); -error_log("do th post"); - try { - $response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/saveToOwnCloud', + $response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/saveToNextcloud', [ 'body' => [ diff --git a/apps/files_sharing/js/external.js b/apps/files_sharing/js/external.js index be384818c3c..a04a5721f37 100644 --- a/apps/files_sharing/js/external.js +++ b/apps/files_sharing/js/external.js @@ -99,7 +99,6 @@ var callbackAddShare = function(result, share) { var password = share.password || ''; if (result) { - //$.post(OC.generateUrl('/apps/files_sharing/api/externalShares'), {id: share.id}); $.post(OC.generateUrl('apps/files_sharing/external'), { remote: share.remote, token: share.token, diff --git a/apps/files_sharing/js/public.js b/apps/files_sharing/js/public.js index 4293c7b310b..6bc31879d0f 100644 --- a/apps/files_sharing/js/public.js +++ b/apps/files_sharing/js/public.js @@ -247,7 +247,7 @@ OCA.Sharing.PublicApp = { var ownerDisplayName = $('#save').data('owner-display-name'); var name = $('#save').data('name'); var isProtected = $('#save').data('protected') ? 1 : 0; - OCA.Sharing.PublicApp._saveToOwnCloud(remote, token, owner, ownerDisplayName, name, isProtected); + OCA.Sharing.PublicApp._saveToNextcloud(remote, token, owner, ownerDisplayName, name, isProtected); }); $('#remote_address').on("keyup paste", function() { @@ -307,7 +307,7 @@ OCA.Sharing.PublicApp = { * @param isProtected * @private */ - _legacySaveToOwnCloud: function (remote, token, owner, ownerDisplayName, name, isProtected) { + _legacySaveToNextcloud: function (remote, token, owner, ownerDisplayName, name, isProtected) { var location = window.location.protocol + '//' + window.location.host + OC.webroot; @@ -335,7 +335,7 @@ OCA.Sharing.PublicApp = { } }, - _saveToOwnCloud: function (remote, token, owner, ownerDisplayName, name, isProtected) { + _saveToNextcloud: function (remote, token, owner, ownerDisplayName, name, isProtected) { var toggleLoading = function() { var iconClass = $('#save-button-confirm').attr('class'); @@ -357,13 +357,13 @@ OCA.Sharing.PublicApp = { toggleLoading(); if (remote.indexOf('@') == -1) { - this._legacySaveToOwnCloud(remote, token, owner, ownerDisplayName, name, isProtected); + this._legacySaveToNextcloud(remote, token, owner, ownerDisplayName, name, isProtected); toggleLoading(); return; } $.post( - OC.generateUrl('/apps/federatedfilesharing/saveToOwnCloud'), + OC.generateUrl('/apps/federatedfilesharing/saveToNextcloud'), { 'shareWith': remote, 'token': token From 5bbba490c4444f7977e7ad0ce4266acbd67eee86 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 12 Jul 2016 18:48:00 +0200 Subject: [PATCH 06/11] move some stuff over to the federatedfilesharing app --- apps/federatedfilesharing/appinfo/app.php | 15 +++- apps/federatedfilesharing/appinfo/routes.php | 1 + .../js/external.js | 30 +++++--- .../Controller/SaveToNextcloudController.php | 75 +++++++++++++++++-- .../SaveToNextcloudControllerTest.php | 30 ++++++-- .../tests/js/externalSpec.js | 0 apps/files_sharing/ajax/external.php | 74 ------------------ apps/files_sharing/appinfo/app.php | 3 - apps/files_sharing/appinfo/routes.php | 2 - apps/files_sharing/js/public.js | 4 +- 10 files changed, 128 insertions(+), 106 deletions(-) rename apps/{files_sharing => federatedfilesharing}/js/external.js (88%) rename apps/{files_sharing => federatedfilesharing}/tests/js/externalSpec.js (100%) delete mode 100644 apps/files_sharing/ajax/external.php diff --git a/apps/federatedfilesharing/appinfo/app.php b/apps/federatedfilesharing/appinfo/app.php index 8abdf8cac19..e6fbe615e7f 100644 --- a/apps/federatedfilesharing/appinfo/app.php +++ b/apps/federatedfilesharing/appinfo/app.php @@ -20,11 +20,11 @@ * */ -$app = new \OCA\FederatedFileSharing\AppInfo\Application(); - use OCA\FederatedFileSharing\Notifier; +$app = new \OCA\FederatedFileSharing\AppInfo\Application(); $l = \OC::$server->getL10N('files_sharing'); +$eventDispatcher = \OC::$server->getEventDispatcher(); $app->registerSettings(); @@ -39,3 +39,14 @@ $manager->registerNotifier(function() { 'name' => $l->t('Federated sharing'), ]; }); + +$federatedShareProvider = $app->getFederatedShareProvider(); + +$eventDispatcher->addListener( + 'OCA\Files::loadAdditionalScripts', + function() use ($federatedShareProvider) { + if ($federatedShareProvider->isIncomingServer2serverShareEnabled()) { + \OCP\Util::addScript('federatedfilesharing', 'external'); + } + } +); diff --git a/apps/federatedfilesharing/appinfo/routes.php b/apps/federatedfilesharing/appinfo/routes.php index 2a90ef5b59b..c5822c6b653 100644 --- a/apps/federatedfilesharing/appinfo/routes.php +++ b/apps/federatedfilesharing/appinfo/routes.php @@ -22,5 +22,6 @@ return [ 'routes' => [ ['name' => 'SaveToNextcloud#saveToNextcloud', 'url' => '/saveToNextcloud', 'verb' => 'POST'], + ['name' => 'SaveToNextcloud#askForFederatedShare', 'url' => '/askForFederatedShare', 'verb' => 'POST'], ] ]; diff --git a/apps/files_sharing/js/external.js b/apps/federatedfilesharing/js/external.js similarity index 88% rename from apps/files_sharing/js/external.js rename to apps/federatedfilesharing/js/external.js index a04a5721f37..e8262092ee8 100644 --- a/apps/files_sharing/js/external.js +++ b/apps/federatedfilesharing/js/external.js @@ -91,7 +91,6 @@ * through the URL */ processIncomingShareFromUrl: function() { - var fileList = this.filesApp.fileList; var params = OC.Util.History.parseUrlQuery(); //manually add server-to-server share if (params.remote && params.token && params.owner && params.name) { @@ -99,15 +98,25 @@ var callbackAddShare = function(result, share) { var password = share.password || ''; if (result) { - $.post(OC.generateUrl('apps/files_sharing/external'), { - remote: share.remote, - token: share.token, - owner: share.owner, - ownerDisplayName: share.ownerDisplayName || share.owner, - name: share.name, - password: password}, function(result) { - OC.Notification.showTemporary(result.data.message); - }); + $.post( + OC.generateUrl('apps/federatedfilesharing/askForFederatedShare'), + { + remote: share.remote, + token: share.token, + owner: share.owner, + ownerDisplayName: share.ownerDisplayName || share.owner, + name: share.name, + password: password + } + ).done( + function(data) { + OC.Notification.showTemporary(data.message); + } + ).fail( + function(data) { + OC.Notification.showTemporary(JSON.parse(data.responseText).message); + } + ); } }; @@ -161,4 +170,3 @@ })(); OC.Plugins.register('OCA.Files.App', OCA.Sharing.ExternalShareDialogPlugin); - diff --git a/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php b/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php index f2788cf1ccd..8801d7af4e4 100644 --- a/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php +++ b/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php @@ -28,8 +28,11 @@ use OCA\FederatedFileSharing\FederatedShareProvider; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; +use OCP\Http\Client\IClientService; +use OCP\IL10N; use OCP\IRequest; use OCP\ISession; +use OCP\IUserSession; use OCP\Share\IManager; class SaveToNextcloudController extends Controller { @@ -46,6 +49,15 @@ class SaveToNextcloudController extends Controller { /** @var ISession */ private $session; + /** @var IL10N */ + private $l; + + /** @var IUserSession */ + private $userSession; + + /** @var IClientService */ + private $clientService; + /** * SaveToNextcloudController constructor. * @@ -55,13 +67,19 @@ class SaveToNextcloudController extends Controller { * @param IManager $shareManager * @param AddressHandler $addressHandler * @param ISession $session + * @param IL10N $l + * @param IUserSession $userSession + * @param IClientService $clientService */ public function __construct($appName, - IRequest $request, - FederatedShareProvider $federatedShareProvider, - IManager $shareManager, - AddressHandler $addressHandler, - ISession $session + IRequest $request, + FederatedShareProvider $federatedShareProvider, + IManager $shareManager, + AddressHandler $addressHandler, + ISession $session, + IL10N $l, + IUserSession $userSession, + IClientService $clientService ) { parent::__construct($appName, $request); @@ -69,6 +87,9 @@ class SaveToNextcloudController extends Controller { $this->shareManager = $shareManager; $this->addressHandler = $addressHandler; $this->session = $session; + $this->l = $l; + $this->userSession = $userSession; + $this->clientService = $clientService; } /** @@ -111,4 +132,48 @@ class SaveToNextcloudController extends Controller { return new JSONResponse(['remoteUrl' => $server]); } + /** + * ask other server to get a federated share + * + * @NoAdminRequired + * + * @param string $token + * @param string $remote + * @param string $password + * @return JSONResponse + */ + public function askForFederatedShare($token, $remote, $password = '') { + // check if server admin allows to mount public links from other servers + if ($this->federatedShareProvider->isIncomingServer2serverShareEnabled() === false) { + return new JSONResponse(['message' => $this->l->t('Server to server sharing is not enabled on this server')], Http::STATUS_BAD_REQUEST); + } + + $shareWith = $this->userSession->getUser()->getUID() . '@' . $this->addressHandler->generateRemoteURL(); + + $httpClient = $this->clientService->newClient(); + + try { + $httpClient->post($remote . '/index.php/apps/federatedfilesharing/saveToNextcloud', + [ + 'body' => + [ + 'token' => $token, + 'shareWith' => rtrim($shareWith, '/'), + 'password' => $password + ] + ] + ); + } catch (\Exception $e) { + if (empty($password)) { + $message = $this->l->t("Couldn't establish a federated share."); + } else { + $message = $this->l->t("Couldn't establish a federated share, maybe the password was wrong."); + } + return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST); + } + + return new JSONResponse(['message' => $this->l->t('Federated Share request was successful, you will receive a invitation. Check your notifications.')]); + + } + } diff --git a/apps/federatedfilesharing/tests/Controller/SaveToNextcloudControllerTest.php b/apps/federatedfilesharing/tests/Controller/SaveToNextcloudControllerTest.php index 60136212c31..0e36df9b25a 100644 --- a/apps/federatedfilesharing/tests/Controller/SaveToNextcloudControllerTest.php +++ b/apps/federatedfilesharing/tests/Controller/SaveToNextcloudControllerTest.php @@ -28,8 +28,11 @@ use OCA\FederatedFileSharing\Controller\SaveToNextcloudController; use OCA\FederatedFileSharing\FederatedShareProvider; use OCP\AppFramework\Http; use OCP\Files\IRootFolder; +use OCP\Http\Client\IClientService; +use OCP\IL10N; use OCP\ISession; use OCP\IUserManager; +use OCP\IUserSession; use OCP\Share; use OCP\Share\IManager; use OCP\Share\IShare; @@ -60,29 +63,44 @@ class SaveToNextcloudControllerTest extends \Test\TestCase { /** @var ISession | \PHPUnit_Framework_MockObject_MockObject */ private $session; + /** @var IL10N | \PHPUnit_Framework_MockObject_MockObject */ + private $l10n; + + /** @var IUserSession | \PHPUnit_Framework_MockObject_MockObject */ + private $userSession; + + /** @var IClientService | \PHPUnit_Framework_MockObject_MockObject */ + private $clientService; + /** @var IShare */ private $share; public function setUp() { parent::setUp(); - $this->request = $this->getMock('OCP\IRequest'); + $this->request = $this->getMockBuilder('OCP\IRequest')->disableOriginalConstructor()->getMock(); $this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider') ->disableOriginalConstructor()->getMock(); - $this->shareManager = $this->getMock('OCP\Share\IManager'); + $this->shareManager = $this->getMockBuilder('OCP\Share\IManager')->disableOriginalConstructor()->getMock(); $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler') ->disableOriginalConstructor()->getMock(); - $this->rootFolder = $this->getMock('OCP\Files\IRootFolder'); - $this->userManager = $this->getMock('OCP\IUserManager'); + $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->disableOriginalConstructor()->getMock(); + $this->userManager = $this->getMockBuilder('OCP\IUserManager')->disableOriginalConstructor()->getMock(); $this->share = new \OC\Share20\Share($this->rootFolder, $this->userManager); - $this->session = $this->getMock('OCP\ISession'); + $this->session = $this->getMockBuilder('OCP\ISession')->disableOriginalConstructor()->getMock(); + $this->l10n = $this->getMockBuilder('OCP\IL10N')->disableOriginalConstructor()->getMock(); + $this->userSession = $this->getMockBuilder('OCP\IUserSession')->disableOriginalConstructor()->getMock(); + $this->clientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->disableOriginalConstructor()->getMock(); $this->controller = new SaveToNextcloudController( 'federatedfilesharing', $this->request, $this->federatedShareProvider, $this->shareManager, $this->addressHandler, - $this->session + $this->session, + $this->l10n, + $this->userSession, + $this->clientService ); } diff --git a/apps/files_sharing/tests/js/externalSpec.js b/apps/federatedfilesharing/tests/js/externalSpec.js similarity index 100% rename from apps/files_sharing/tests/js/externalSpec.js rename to apps/federatedfilesharing/tests/js/externalSpec.js diff --git a/apps/files_sharing/ajax/external.php b/apps/files_sharing/ajax/external.php deleted file mode 100644 index 20c3706c12d..00000000000 --- a/apps/files_sharing/ajax/external.php +++ /dev/null @@ -1,74 +0,0 @@ - - * @author Joas Schilling - * @author Lukas Reschke - * @author Morris Jobke - * @author Robin Appelman - * @author Roeland Jago Douma - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -OCP\JSON::callCheck(); -OCP\JSON::checkLoggedIn(); -OCP\JSON::checkAppEnabled('files_sharing'); - -$l = \OC::$server->getL10N('files_sharing'); - -$federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application('federatedfilesharing'); -$federatedShareProvider = $federatedSharingApp->getFederatedShareProvider(); - -// check if server admin allows to mount public links from other servers -if ($federatedShareProvider->isIncomingServer2serverShareEnabled() === false) { - \OCP\JSON::error(array('data' => array('message' => $l->t('Server to server sharing is not enabled on this server')))); - exit(); -} - -$token = $_POST['token']; -$remote = $_POST['remote']; -$password = isset($_POST['password']) ? $_POST['password'] : ''; - -$urlGenerator = \OC::$server->getURLGenerator(); - -$shareWith = \OCP\User::getUser() . '@' . $urlGenerator->getAbsoluteURL('/'); - -$httpClient = \OC::$server->getHTTPClientService()->newClient(); - -try { - $response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/saveToNextcloud', - [ - 'body' => - [ - 'token' => $token, - 'shareWith' => rtrim($shareWith, '/'), - 'password' => $password - ] - ] - ); -} catch (\Exception $e) { - if (empty($password)) { - $message = $l->t("Couldn't establish a federated share."); - } else { - $message = $l->t("Couldn't establish a federated share, maybe the password was wrong."); - } - \OCP\JSON::error(array('data' => array('message' => $message))); - exit(); -} - -\OCP\JSON::success(array('data' => array('message' => $l->t('Federated Share request was successful, you will receive a invitation. Check your notifications.')))); diff --git a/apps/files_sharing/appinfo/app.php b/apps/files_sharing/appinfo/app.php index c6ae6903eec..219fa298d51 100644 --- a/apps/files_sharing/appinfo/app.php +++ b/apps/files_sharing/appinfo/app.php @@ -46,9 +46,6 @@ $eventDispatcher->addListener( function() { \OCP\Util::addScript('files_sharing', 'share'); \OCP\Util::addScript('files_sharing', 'sharetabview'); - if (\OC::$server->getConfig()->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes') === 'yes') { - \OCP\Util::addScript('files_sharing', 'external'); - } \OCP\Util::addStyle('files_sharing', 'sharetabview'); } ); diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index 0e72934d094..26d32c3a990 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -53,8 +53,6 @@ $this->create('files_sharing_ajax_publicpreview', 'ajax/publicpreview.php') ->actionInclude('files_sharing/ajax/publicpreview.php'); $this->create('sharing_external_shareinfo', '/shareinfo') ->actionInclude('files_sharing/ajax/shareinfo.php'); -$this->create('sharing_external_add', '/external') - ->actionInclude('files_sharing/ajax/external.php'); // OCS API diff --git a/apps/files_sharing/js/public.js b/apps/files_sharing/js/public.js index 6bc31879d0f..a7d0c15a01a 100644 --- a/apps/files_sharing/js/public.js +++ b/apps/files_sharing/js/public.js @@ -356,7 +356,7 @@ OCA.Sharing.PublicApp = { toggleLoading(); - if (remote.indexOf('@') == -1) { + if (remote.indexOf('@') === -1) { this._legacySaveToNextcloud(remote, token, owner, ownerDisplayName, name, isProtected); toggleLoading(); return; @@ -380,8 +380,6 @@ OCA.Sharing.PublicApp = { } ).fail( function (jqXHR) { - console.log("ERROR!"); - console.log(jqXHR); OC.dialogs.alert(JSON.parse(jqXHR.responseText).message, t('files_sharing', 'Failed to add the public link to your Nextcloud')); toggleLoading(); From 55b990b544c3db1c8814bb541aababf4a23324f8 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 13 Jul 2016 11:32:27 +0200 Subject: [PATCH 07/11] allow to mount public link from Nextclouds <= 9 --- apps/federatedfilesharing/js/external.js | 7 +- .../Controller/SaveToNextcloudController.php | 134 +++++++++++++++++- 2 files changed, 136 insertions(+), 5 deletions(-) diff --git a/apps/federatedfilesharing/js/external.js b/apps/federatedfilesharing/js/external.js index e8262092ee8..1daecd2e744 100644 --- a/apps/federatedfilesharing/js/external.js +++ b/apps/federatedfilesharing/js/external.js @@ -91,6 +91,7 @@ * through the URL */ processIncomingShareFromUrl: function() { + var fileList = this.filesApp.fileList; var params = OC.Util.History.parseUrlQuery(); //manually add server-to-server share if (params.remote && params.token && params.owner && params.name) { @@ -110,7 +111,11 @@ } ).done( function(data) { - OC.Notification.showTemporary(data.message); + if (data.hasOwnProperty('legacyMount')) { + fileList.reload(); + } else { + OC.Notification.showTemporary(data.message); + } } ).fail( function(data) { diff --git a/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php b/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php index 8801d7af4e4..5b05d196e26 100644 --- a/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php +++ b/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php @@ -1,6 +1,6 @@ + * @author Björn Schießle * * @copyright Copyright (c) 2016, ownCloud, Inc. * @license AGPL-3.0 @@ -140,9 +140,12 @@ class SaveToNextcloudController extends Controller { * @param string $token * @param string $remote * @param string $password + * @param string $owner (only for legacy reasons, can be removed with legacyMountPublicLink()) + * @param string $ownerDisplayName (only for legacy reasons, can be removed with legacyMountPublicLink()) + * @param string $name (only for legacy reasons, can be removed with legacyMountPublicLink()) * @return JSONResponse */ - public function askForFederatedShare($token, $remote, $password = '') { + public function askForFederatedShare($token, $remote, $password = '', $owner='', $ownerDisplayName = '', $name = '') { // check if server admin allows to mount public links from other servers if ($this->federatedShareProvider->isIncomingServer2serverShareEnabled() === false) { return new JSONResponse(['message' => $this->l->t('Server to server sharing is not enabled on this server')], Http::STATUS_BAD_REQUEST); @@ -153,7 +156,7 @@ class SaveToNextcloudController extends Controller { $httpClient = $this->clientService->newClient(); try { - $httpClient->post($remote . '/index.php/apps/federatedfilesharing/saveToNextcloud', + $response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/saveToNextcloud', [ 'body' => [ @@ -172,7 +175,130 @@ class SaveToNextcloudController extends Controller { return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST); } - return new JSONResponse(['message' => $this->l->t('Federated Share request was successful, you will receive a invitation. Check your notifications.')]); + $body = $response->getBody(); + $result = json_decode($body, true); + + if (is_array($result) && isset($result['remoteUrl'])) { + return new JSONResponse(['message' => $this->l->t('Federated Share request was successful, you will receive a invitation. Check your notifications.')]); + } + + // if we doesn't get the expected response we assume that we try to add + // a federated share from a Nextcloud <= 9 server + return $this->legacyMountPublicLink($token, $remote, $password, $name, $owner, $ownerDisplayName); + } + + /** + * Allow Nextcloud to mount a public link directly + * + * This code was copied from the apps/files_sharing/ajax/external.php with + * minimal changes, just to guarantee backward compatibility + * + * ToDo: Remove this method once Nextcloud 9 reaches end of life + * + * @param string $token + * @param string $remote + * @param string $password + * @param string $name + * @param string $owner + * @param string $ownerDisplayName + * @return JSONResponse + */ + private function legacyMountPublicLink($token, $remote, $password, $name, $owner, $ownerDisplayName) { + + // Check for invalid name + if (!\OCP\Util::isValidFileName($name)) { + return new JSONResponse(['message' => $this->l->t('The mountpoint name contains invalid characters.')], Http::STATUS_BAD_REQUEST); + } + $currentUser = $this->userSession->getUser()->getUID(); + $currentServer = $this->addressHandler->generateRemoteURL(); + if (\OC\Share\Helper::isSameUserOnSameServer($owner, $remote, $currentUser, $currentServer)) { + return new JSONResponse(['message' => $this->l->t('Not allowed to create a federated share with the owner.')], Http::STATUS_BAD_REQUEST); + } + $discoveryManager = new \OCA\FederatedFileSharing\DiscoveryManager( + \OC::$server->getMemCacheFactory(), + \OC::$server->getHTTPClientService() + ); + $externalManager = new \OCA\Files_Sharing\External\Manager( + \OC::$server->getDatabaseConnection(), + \OC\Files\Filesystem::getMountManager(), + \OC\Files\Filesystem::getLoader(), + \OC::$server->getHTTPHelper(), + \OC::$server->getNotificationManager(), + $discoveryManager, + \OC::$server->getUserSession()->getUser()->getUID() + ); + + // check for ssl cert + if (substr($remote, 0, 5) === 'https') { + try { + $client = $this->clientService->newClient(); + $client->get($remote, [ + 'timeout' => 10, + 'connect_timeout' => 10, + ])->getBody(); + } catch (\Exception $e) { + return new JSONResponse(['message' => $this->l->t('Invalid or untrusted SSL certificate')], Http::STATUS_BAD_REQUEST); + } + } + $mount = $externalManager->addShare($remote, $token, $password, $name, $ownerDisplayName, true); + /** + * @var \OCA\Files_Sharing\External\Storage $storage + */ + $storage = $mount->getStorage(); + try { + // check if storage exists + $storage->checkStorageAvailability(); + } catch (\OCP\Files\StorageInvalidException $e) { + // note: checkStorageAvailability will already remove the invalid share + \OCP\Util::writeLog( + 'federatedfilesharing', + 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(), + \OCP\Util::DEBUG + ); + return new JSONResponse(['message' => $this->l->t('Could not authenticate to remote share, password might be wrong')], Http::STATUS_BAD_REQUEST); + } catch (\Exception $e) { + \OCP\Util::writeLog( + 'federatedfilesharing', + 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(), + \OCP\Util::DEBUG + ); + $externalManager->removeShare($mount->getMountPoint()); + return new JSONResponse(['message' => $this->l->t('Storage not valid')], Http::STATUS_BAD_REQUEST); + } + $result = $storage->file_exists(''); + if ($result) { + try { + $storage->getScanner()->scanAll(); + return new JSONResponse( + [ + 'message' => $this->l->t('Federated Share successfully added'), + 'legacyMount' => '1' + ] + ); + } catch (\OCP\Files\StorageInvalidException $e) { + \OCP\Util::writeLog( + 'federatedfilesharing', + 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(), + \OCP\Util::DEBUG + ); + return new JSONResponse(['message' => $this->l->t('Storage not valid')], Http::STATUS_BAD_REQUEST); + } catch (\Exception $e) { + \OCP\Util::writeLog( + 'federatedfilesharing', + 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(), + \OCP\Util::DEBUG + ); + return new JSONResponse(['message' => $this->l->t('Couldn\'t add remote share')], Http::STATUS_BAD_REQUEST); + } + } else { + $externalManager->removeShare($mount->getMountPoint()); + \OCP\Util::writeLog( + 'federatedfilesharing', + 'Couldn\'t add remote share', + \OCP\Util::DEBUG + ); + return new JSONResponse(['message' => $this->l->t('Couldn\'t add remote share')], Http::STATUS_BAD_REQUEST); + } } From dc53788711316fc5d812dc059567813f656a0fa5 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 13 Jul 2016 12:39:20 +0200 Subject: [PATCH 08/11] remove unused parameter --- apps/dav/appinfo/v1/publicwebdav.php | 2 +- apps/federatedfilesharing/settings-admin.php | 2 +- apps/federatedfilesharing/settings-personal.php | 2 +- apps/files_sharing/ajax/shareinfo.php | 2 +- apps/files_sharing/lib/AppInfo/Application.php | 2 +- apps/files_sharing/lib/share/file.php | 2 +- ocs/routes.php | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/apps/dav/appinfo/v1/publicwebdav.php b/apps/dav/appinfo/v1/publicwebdav.php index 261a4d4b96d..2202cbb977c 100644 --- a/apps/dav/appinfo/v1/publicwebdav.php +++ b/apps/dav/appinfo/v1/publicwebdav.php @@ -57,7 +57,7 @@ $linkCheckPlugin = new \OCA\DAV\Files\Sharing\PublicLinkCheckPlugin(); $server = $serverFactory->createServer($baseuri, $requestUri, $authBackend, function (\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin) { $isAjax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'); - $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application('federatedfilesharing'); + $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); $federatedShareProvider = $federatedSharingApp->getFederatedShareProvider(); if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false && !$isAjax) { // this is what is thrown when trying to access a non-existing share diff --git a/apps/federatedfilesharing/settings-admin.php b/apps/federatedfilesharing/settings-admin.php index 9875dcf3b5b..dc5a4d6826c 100644 --- a/apps/federatedfilesharing/settings-admin.php +++ b/apps/federatedfilesharing/settings-admin.php @@ -22,7 +22,7 @@ use OCA\FederatedFileSharing\AppInfo\Application; -$app = new Application('federatedfilesharing'); +$app = new Application(); $federatedShareProvider = $app->getFederatedShareProvider(); $tmpl = new OCP\Template('federatedfilesharing', 'settings-admin'); diff --git a/apps/federatedfilesharing/settings-personal.php b/apps/federatedfilesharing/settings-personal.php index a36cdd712c4..261d540f48d 100644 --- a/apps/federatedfilesharing/settings-personal.php +++ b/apps/federatedfilesharing/settings-personal.php @@ -27,7 +27,7 @@ use OCA\FederatedFileSharing\AppInfo\Application; $l = \OC::$server->getL10N('federatedfilesharing'); -$app = new Application('federatedfilesharing'); +$app = new Application(); $federatedShareProvider = $app->getFederatedShareProvider(); $isIE8 = false; diff --git a/apps/files_sharing/ajax/shareinfo.php b/apps/files_sharing/ajax/shareinfo.php index 0b93e3d2ee9..31b8e00f496 100644 --- a/apps/files_sharing/ajax/shareinfo.php +++ b/apps/files_sharing/ajax/shareinfo.php @@ -31,7 +31,7 @@ if (!isset($_GET['t'])) { exit; } -$federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application('federatedfilesharing'); +$federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); $federatedShareProvider = $federatedSharingApp->getFederatedShareProvider(); if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false) { diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index fda16c7acac..5ed8d886e2e 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -46,7 +46,7 @@ class Application extends App { * Controllers */ $container->registerService('ShareController', function (SimpleContainer $c) use ($server) { - $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application('federatedfilesharing'); + $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); return new ShareController( $c->query('AppName'), $c->query('Request'), diff --git a/apps/files_sharing/lib/share/file.php b/apps/files_sharing/lib/share/file.php index 4643e46eeb2..e7588a589e8 100644 --- a/apps/files_sharing/lib/share/file.php +++ b/apps/files_sharing/lib/share/file.php @@ -77,7 +77,7 @@ class OC_Share_Backend_File implements OCP\Share_Backend_File_Dependent { if ($federatedShareProvider) { $this->federatedShareProvider = $federatedShareProvider; } else { - $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application('federatedfilesharing'); + $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); $this->federatedShareProvider = $federatedSharingApp->getFederatedShareProvider(); } } diff --git a/ocs/routes.php b/ocs/routes.php index d9c78a4a16a..c465e08e877 100644 --- a/ocs/routes.php +++ b/ocs/routes.php @@ -99,7 +99,7 @@ API::register( // Server-to-Server Sharing if (\OC::$server->getAppManager()->isEnabledForUser('files_sharing')) { - $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application('federatedfilesharing'); + $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); $addressHandler = new \OCA\FederatedFileSharing\AddressHandler( \OC::$server->getURLGenerator(), \OC::$server->getL10N('federatedfilesharing') From 56d3100f44e5541c2b19ee24fa37df372a809372 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 13 Jul 2016 17:50:06 +0200 Subject: [PATCH 09/11] adjust copyright notices --- .../lib/Controller/SaveToNextcloudController.php | 2 ++ .../tests/Controller/SaveToNextcloudControllerTest.php | 2 ++ apps/files_sharing/js/public.js | 1 + 3 files changed, 5 insertions(+) diff --git a/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php b/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php index 5b05d196e26..c95b50ecf46 100644 --- a/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php +++ b/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php @@ -3,6 +3,8 @@ * @author Björn Schießle * * @copyright Copyright (c) 2016, ownCloud, Inc. + * @copyright Copyright (c) 2016, Björn Schießle + * * @license AGPL-3.0 * * This code is free software: you can redistribute it and/or modify diff --git a/apps/federatedfilesharing/tests/Controller/SaveToNextcloudControllerTest.php b/apps/federatedfilesharing/tests/Controller/SaveToNextcloudControllerTest.php index 0e36df9b25a..a98e4e4d09e 100644 --- a/apps/federatedfilesharing/tests/Controller/SaveToNextcloudControllerTest.php +++ b/apps/federatedfilesharing/tests/Controller/SaveToNextcloudControllerTest.php @@ -3,6 +3,8 @@ * @author Björn Schießle * * @copyright Copyright (c) 2016, ownCloud, Inc. + * @copyright Copyright (c) 2016, Björn Schießle + * * @license AGPL-3.0 * * This code is free software: you can redistribute it and/or modify diff --git a/apps/files_sharing/js/public.js b/apps/files_sharing/js/public.js index a7d0c15a01a..5746f4dbb8c 100644 --- a/apps/files_sharing/js/public.js +++ b/apps/files_sharing/js/public.js @@ -1,5 +1,6 @@ /* * Copyright (c) 2014 + * @copyright Copyright (c) 2016, Björn Schießle * * This file is licensed under the Affero General Public License version 3 * or later. From c7f6461c53e1d432f268305e63eb9a604e12cba4 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 14 Jul 2016 11:01:25 +0200 Subject: [PATCH 10/11] rename SaveToNextcloudController to MountPublicLinkController --- apps/federatedfilesharing/appinfo/routes.php | 4 ++-- ...ller.php => MountPublicLinkController.php} | 23 ++++++++++++------- ....php => MountPublicLinkControllerTest.php} | 19 ++++++++------- apps/files_sharing/js/public.js | 10 ++++---- 4 files changed, 31 insertions(+), 25 deletions(-) rename apps/federatedfilesharing/lib/Controller/{SaveToNextcloudController.php => MountPublicLinkController.php} (95%) rename apps/federatedfilesharing/tests/Controller/{SaveToNextcloudControllerTest.php => MountPublicLinkControllerTest.php} (92%) diff --git a/apps/federatedfilesharing/appinfo/routes.php b/apps/federatedfilesharing/appinfo/routes.php index c5822c6b653..ec7b662686f 100644 --- a/apps/federatedfilesharing/appinfo/routes.php +++ b/apps/federatedfilesharing/appinfo/routes.php @@ -21,7 +21,7 @@ return [ 'routes' => [ - ['name' => 'SaveToNextcloud#saveToNextcloud', 'url' => '/saveToNextcloud', 'verb' => 'POST'], - ['name' => 'SaveToNextcloud#askForFederatedShare', 'url' => '/askForFederatedShare', 'verb' => 'POST'], + ['name' => 'MountPublicLink#createFederatedShare', 'url' => '/createFederatedShare', 'verb' => 'POST'], + ['name' => 'MountPublicLink#askForFederatedShare', 'url' => '/askForFederatedShare', 'verb' => 'POST'], ] ]; diff --git a/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php b/apps/federatedfilesharing/lib/Controller/MountPublicLinkController.php similarity index 95% rename from apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php rename to apps/federatedfilesharing/lib/Controller/MountPublicLinkController.php index c95b50ecf46..ec70090f97a 100644 --- a/apps/federatedfilesharing/lib/Controller/SaveToNextcloudController.php +++ b/apps/federatedfilesharing/lib/Controller/MountPublicLinkController.php @@ -37,7 +37,14 @@ use OCP\ISession; use OCP\IUserSession; use OCP\Share\IManager; -class SaveToNextcloudController extends Controller { +/** + * Class MountPublicLinkController + * + * convert public links to federated shares + * + * @package OCA\FederatedFileSharing\Controller + */ +class MountPublicLinkController extends Controller { /** @var FederatedShareProvider */ private $federatedShareProvider; @@ -61,7 +68,7 @@ class SaveToNextcloudController extends Controller { private $clientService; /** - * SaveToNextcloudController constructor. + * MountPublicLinkController constructor. * * @param string $appName * @param IRequest $request @@ -95,8 +102,7 @@ class SaveToNextcloudController extends Controller { } /** - * save public link to my Nextcloud by asking the owner to create a federated - * share with me + * send federated share to a user of a public link * * @NoCSRFRequired * @PublicPage @@ -106,7 +112,7 @@ class SaveToNextcloudController extends Controller { * @param string $password * @return JSONResponse */ - public function saveToNextcloud($shareWith, $token, $password = '') { + public function createFederatedShare($shareWith, $token, $password = '') { try { list(, $server) = $this->addressHandler->splitUserRemote($shareWith); @@ -147,7 +153,7 @@ class SaveToNextcloudController extends Controller { * @param string $name (only for legacy reasons, can be removed with legacyMountPublicLink()) * @return JSONResponse */ - public function askForFederatedShare($token, $remote, $password = '', $owner='', $ownerDisplayName = '', $name = '') { + public function askForFederatedShare($token, $remote, $password = '', $owner = '', $ownerDisplayName = '', $name = '') { // check if server admin allows to mount public links from other servers if ($this->federatedShareProvider->isIncomingServer2serverShareEnabled() === false) { return new JSONResponse(['message' => $this->l->t('Server to server sharing is not enabled on this server')], Http::STATUS_BAD_REQUEST); @@ -158,14 +164,15 @@ class SaveToNextcloudController extends Controller { $httpClient = $this->clientService->newClient(); try { - $response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/saveToNextcloud', + $response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/createFederatedShare', [ 'body' => [ 'token' => $token, 'shareWith' => rtrim($shareWith, '/'), 'password' => $password - ] + ], + 'connect_timeout' => 10, ] ); } catch (\Exception $e) { diff --git a/apps/federatedfilesharing/tests/Controller/SaveToNextcloudControllerTest.php b/apps/federatedfilesharing/tests/Controller/MountPublicLinkControllerTest.php similarity index 92% rename from apps/federatedfilesharing/tests/Controller/SaveToNextcloudControllerTest.php rename to apps/federatedfilesharing/tests/Controller/MountPublicLinkControllerTest.php index a98e4e4d09e..816fc90c4e1 100644 --- a/apps/federatedfilesharing/tests/Controller/SaveToNextcloudControllerTest.php +++ b/apps/federatedfilesharing/tests/Controller/MountPublicLinkControllerTest.php @@ -4,7 +4,7 @@ * * @copyright Copyright (c) 2016, ownCloud, Inc. * @copyright Copyright (c) 2016, Björn Schießle - * + * * @license AGPL-3.0 * * This code is free software: you can redistribute it and/or modify @@ -26,7 +26,7 @@ namespace OCA\FederatedFileSharing\Tests\Controller; use OC\HintException; use OCA\FederatedFileSharing\AddressHandler; -use OCA\FederatedFileSharing\Controller\SaveToNextcloudController; +use OCA\FederatedFileSharing\Controller\MountPublicLinkController; use OCA\FederatedFileSharing\FederatedShareProvider; use OCP\AppFramework\Http; use OCP\Files\IRootFolder; @@ -35,13 +35,12 @@ use OCP\IL10N; use OCP\ISession; use OCP\IUserManager; use OCP\IUserSession; -use OCP\Share; use OCP\Share\IManager; use OCP\Share\IShare; -class SaveToNextcloudControllerTest extends \Test\TestCase { +class MountPublicLinkControllerTest extends \Test\TestCase { - /** @var SaveToNextcloudController */ + /** @var MountPublicLinkController */ private $controller; /** @var \OCP\IRequest | \PHPUnit_Framework_MockObject_MockObject */ @@ -94,7 +93,7 @@ class SaveToNextcloudControllerTest extends \Test\TestCase { $this->userSession = $this->getMockBuilder('OCP\IUserSession')->disableOriginalConstructor()->getMock(); $this->clientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->disableOriginalConstructor()->getMock(); - $this->controller = new SaveToNextcloudController( + $this->controller = new MountPublicLinkController( 'federatedfilesharing', $this->request, $this->federatedShareProvider, $this->shareManager, @@ -107,7 +106,7 @@ class SaveToNextcloudControllerTest extends \Test\TestCase { } /** - * @dataProvider dataTestSaveToNextcloud + * @dataProvider dataTestCreateFederatedShare * * @param string $shareWith * @param bool $validShareWith @@ -116,7 +115,7 @@ class SaveToNextcloudControllerTest extends \Test\TestCase { * @param bool $createSuccessful * @param string $expectedReturnData */ - public function testSaveToNextcloud($shareWith, $validShareWith, $token, $validToken, $createSuccessful, $expectedReturnData) { + public function testCreateFederatedShare($shareWith, $validShareWith, $token, $validToken, $createSuccessful, $expectedReturnData) { $this->addressHandler->expects($this->any())->method('splitUserRemote') ->with($shareWith) ->willReturnCallback( @@ -153,7 +152,7 @@ class SaveToNextcloudControllerTest extends \Test\TestCase { } ); - $result = $this->controller->saveToNextcloud($shareWith, $token); + $result = $this->controller->createFederatedShare($shareWith, $token); $errorCase = !$validShareWith || !$validToken || !$createSuccessful; @@ -170,7 +169,7 @@ class SaveToNextcloudControllerTest extends \Test\TestCase { } - public function dataTestSaveToNextcloud() { + public function dataTestCreateFederatedShare() { return [ //shareWith, validShareWith, token, validToken, createSuccessful, expectedReturnData ['user@server', true, 'token', true, true, 'server'], diff --git a/apps/files_sharing/js/public.js b/apps/files_sharing/js/public.js index 5746f4dbb8c..0dfff235998 100644 --- a/apps/files_sharing/js/public.js +++ b/apps/files_sharing/js/public.js @@ -248,7 +248,7 @@ OCA.Sharing.PublicApp = { var ownerDisplayName = $('#save').data('owner-display-name'); var name = $('#save').data('name'); var isProtected = $('#save').data('protected') ? 1 : 0; - OCA.Sharing.PublicApp._saveToNextcloud(remote, token, owner, ownerDisplayName, name, isProtected); + OCA.Sharing.PublicApp._createFederatedShare(remote, token, owner, ownerDisplayName, name, isProtected); }); $('#remote_address').on("keyup paste", function() { @@ -308,7 +308,7 @@ OCA.Sharing.PublicApp = { * @param isProtected * @private */ - _legacySaveToNextcloud: function (remote, token, owner, ownerDisplayName, name, isProtected) { + _legacyCreateFederatedShare: function (remote, token, owner, ownerDisplayName, name, isProtected) { var location = window.location.protocol + '//' + window.location.host + OC.webroot; @@ -336,7 +336,7 @@ OCA.Sharing.PublicApp = { } }, - _saveToNextcloud: function (remote, token, owner, ownerDisplayName, name, isProtected) { + _createFederatedShare: function (remote, token, owner, ownerDisplayName, name, isProtected) { var toggleLoading = function() { var iconClass = $('#save-button-confirm').attr('class'); @@ -358,13 +358,13 @@ OCA.Sharing.PublicApp = { toggleLoading(); if (remote.indexOf('@') === -1) { - this._legacySaveToNextcloud(remote, token, owner, ownerDisplayName, name, isProtected); + this._legacyCreateFederatedShare(remote, token, owner, ownerDisplayName, name, isProtected); toggleLoading(); return; } $.post( - OC.generateUrl('/apps/federatedfilesharing/saveToNextcloud'), + OC.generateUrl('/apps/federatedfilesharing/createFederatedShare'), { 'shareWith': remote, 'token': token From f8a531c06c8095be4edcbd70063437e67833668e Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 14 Jul 2016 20:41:10 +0200 Subject: [PATCH 11/11] don't convert link share to federated share if outgoing federated shares are forbidden --- .../Controller/MountPublicLinkController.php | 12 ++++++- .../MountPublicLinkControllerTest.php | 36 +++++++++++++------ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/apps/federatedfilesharing/lib/Controller/MountPublicLinkController.php b/apps/federatedfilesharing/lib/Controller/MountPublicLinkController.php index ec70090f97a..3e61d355b63 100644 --- a/apps/federatedfilesharing/lib/Controller/MountPublicLinkController.php +++ b/apps/federatedfilesharing/lib/Controller/MountPublicLinkController.php @@ -114,6 +114,13 @@ class MountPublicLinkController extends Controller { */ public function createFederatedShare($shareWith, $token, $password = '') { + if (!$this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) { + return new JSONResponse( + ['message' => 'This server doesn\'t support outgoing federated shares'], + Http::STATUS_BAD_REQUEST + ); + } + try { list(, $server) = $this->addressHandler->splitUserRemote($shareWith); $share = $this->shareManager->getShareByToken($token); @@ -126,7 +133,10 @@ class MountPublicLinkController extends Controller { $authenticated = $this->session->get('public_link_authenticated') === $share->getId() || $this->shareManager->checkPassword($share, $password); if (!empty($storedPassword) && !$authenticated ) { - return new JSONResponse(['message' => 'No permission to access the share'], Http::STATUS_BAD_REQUEST); + return new JSONResponse( + ['message' => 'No permission to access the share'], + Http::STATUS_BAD_REQUEST + ); } $share->setSharedWith($shareWith); diff --git a/apps/federatedfilesharing/tests/Controller/MountPublicLinkControllerTest.php b/apps/federatedfilesharing/tests/Controller/MountPublicLinkControllerTest.php index 816fc90c4e1..3fe05e72cf4 100644 --- a/apps/federatedfilesharing/tests/Controller/MountPublicLinkControllerTest.php +++ b/apps/federatedfilesharing/tests/Controller/MountPublicLinkControllerTest.php @@ -109,13 +109,26 @@ class MountPublicLinkControllerTest extends \Test\TestCase { * @dataProvider dataTestCreateFederatedShare * * @param string $shareWith + * @param bool $outgoingSharesAllowed * @param bool $validShareWith * @param string $token * @param bool $validToken * @param bool $createSuccessful * @param string $expectedReturnData */ - public function testCreateFederatedShare($shareWith, $validShareWith, $token, $validToken, $createSuccessful, $expectedReturnData) { + public function testCreateFederatedShare($shareWith, + $outgoingSharesAllowed, + $validShareWith, + $token, + $validToken, + $createSuccessful, + $expectedReturnData + ) { + + $this->federatedShareProvider->expects($this->any()) + ->method('isOutgoingServer2serverShareEnabled') + ->willReturn($outgoingSharesAllowed); + $this->addressHandler->expects($this->any())->method('splitUserRemote') ->with($shareWith) ->willReturnCallback( @@ -154,7 +167,7 @@ class MountPublicLinkControllerTest extends \Test\TestCase { $result = $this->controller->createFederatedShare($shareWith, $token); - $errorCase = !$validShareWith || !$validToken || !$createSuccessful; + $errorCase = !$validShareWith || !$validToken || !$createSuccessful || !$outgoingSharesAllowed; if ($errorCase) { $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus()); @@ -171,15 +184,16 @@ class MountPublicLinkControllerTest extends \Test\TestCase { public function dataTestCreateFederatedShare() { return [ - //shareWith, validShareWith, token, validToken, createSuccessful, expectedReturnData - ['user@server', true, 'token', true, true, 'server'], - ['user@server', false, 'token', true, true, 'invalid federated cloud id'], - ['user@server', false, 'token', false, true, 'invalid federated cloud id'], - ['user@server', false, 'token', false, false, 'invalid federated cloud id'], - ['user@server', false, 'token', true, false, 'invalid federated cloud id'], - ['user@server', true, 'token', false, true, 'invalid token'], - ['user@server', true, 'token', false, false, 'invalid token'], - ['user@server', true, 'token', true, false, 'can not create share'] + //shareWith, outgoingSharesAllowed, validShareWith, token, validToken, createSuccessful, expectedReturnData + ['user@server', true, true, 'token', true, true, 'server'], + ['user@server', true, false, 'token', true, true, 'invalid federated cloud id'], + ['user@server', true, false, 'token', false, true, 'invalid federated cloud id'], + ['user@server', true, false, 'token', false, false, 'invalid federated cloud id'], + ['user@server', true, false, 'token', true, false, 'invalid federated cloud id'], + ['user@server', true, true, 'token', false, true, 'invalid token'], + ['user@server', true, true, 'token', false, false, 'invalid token'], + ['user@server', true, true, 'token', true, false, 'can not create share'], + ['user@server', false, true, 'token', true, true, 'This server doesn\'t support outgoing federated shares'], ]; }