From 5bbba490c4444f7977e7ad0ce4266acbd67eee86 Mon Sep 17 00:00:00 2001
From: Bjoern Schiessle <bjoern@schiessle.org>
Date: Tue, 12 Jul 2016 18:48:00 +0200
Subject: [PATCH] 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 @@
-<?php
-/**
- * @author Björn Schießle <bjoern@schiessle.org>
- * @author Joas Schilling <nickvergessen@owncloud.com>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Robin Appelman <icewind@owncloud.com>
- * @author Roeland Jago Douma <rullzer@owncloud.com>
- * @author Vincent Petry <pvince81@owncloud.com>
- *
- * @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 <http://www.gnu.org/licenses/>
- *
- */
-
-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();
-- 
GitLab