Skip to content

Commit

Permalink
move some stuff over to the federatedfilesharing app
Browse files Browse the repository at this point in the history
  • Loading branch information
schiessle committed Jul 13, 2016
1 parent 3d3ce42 commit 664129b
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 106 deletions.
13 changes: 11 additions & 2 deletions apps/federatedfilesharing/appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -39,3 +39,12 @@
'name' => $l->t('Federated sharing'),
];
});

$eventDispatcher->addListener(
'OCA\Files::loadAdditionalScripts',
function() {
if (\OC::$server->getConfig()->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes') === 'yes') {
\OCP\Util::addScript('federatedfilesharing', 'external');
}
}
);
1 change: 1 addition & 0 deletions apps/federatedfilesharing/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
return [
'routes' => [
['name' => 'SaveToNextcloud#saveToNextcloud', 'url' => '/saveToNextcloud', 'verb' => 'POST'],
['name' => 'SaveToNextcloud#askForFederatedShare', 'url' => '/askForFederatedShare', 'verb' => 'POST'],
]
];
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,32 @@
* 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) {

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);
}
);
}
};

Expand Down Expand Up @@ -161,4 +170,3 @@
})();

OC.Plugins.register('OCA.Files.App', OCA.Sharing.ExternalShareDialogPlugin);

Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
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 {
Expand All @@ -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.
*
Expand All @@ -55,20 +67,29 @@ 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);

$this->federatedShareProvider = $federatedShareProvider;
$this->shareManager = $shareManager;
$this->addressHandler = $addressHandler;
$this->session = $session;
$this->l = $l;
$this->userSession = $userSession;
$this->clientService = $clientService;
}

/**
Expand Down Expand Up @@ -111,4 +132,48 @@ public function saveToNextcloud($shareWith, $token, $password = '') {
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.')]);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
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;
Expand Down Expand Up @@ -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
);
}

Expand Down
File renamed without changes.
74 changes: 0 additions & 74 deletions apps/files_sharing/ajax/external.php

This file was deleted.

3 changes: 0 additions & 3 deletions apps/files_sharing/appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@
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');
}
);
Expand Down
2 changes: 0 additions & 2 deletions apps/files_sharing/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ function() {
->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

Expand Down
4 changes: 1 addition & 3 deletions apps/files_sharing/js/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'));
}
Expand Down

0 comments on commit 664129b

Please sign in to comment.