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
This commit is contained in:
Björn Schießle 2016-05-23 11:48:14 +02:00 committed by Bjoern Schiessle
parent 762d76f0c3
commit f5d0b464a7
No known key found for this signature in database
GPG Key ID: 2378A753E2BF04F6
7 changed files with 157 additions and 48 deletions

View File

@ -20,7 +20,7 @@
*
*/
$app = new \OCA\FederatedFileSharing\AppInfo\Application('federatedfilesharing');
$app = new \OCA\FederatedFileSharing\AppInfo\Application();
use OCA\FederatedFileSharing\Notifier;

View File

@ -0,0 +1,26 @@
<?php
/**
* @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
return [
'routes' => [
['name' => 'SaveToOwnCloud#saveToOwnCloud', 'url' => '/saveToOwnCloud', 'verb' => 'POST'],
]
];

View File

@ -31,6 +31,10 @@ class Application extends App {
/** @var FederatedShareProvider */
protected $federatedShareProvider;
public function __construct() {
parent::__construct('federatedfilesharing');
}
/**
* register personal and admin settings page
*/

View File

@ -0,0 +1,88 @@
<?php
/**
* @author Björn Schießle <schiessle@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/>
*
*/
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]);
}
}

View File

@ -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;

View File

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

View File

@ -84,7 +84,7 @@ $maxUploadFilesize = min($upload_max_filesize, $post_max_size);
data-owner-display-name="<?php p($_['displayName']) ?>" data-owner="<?php p($_['owner']) ?>" data-name="<?php p($_['filename']) ?>">
<button id="save-button"><?php p($l->t('Add to your ownCloud')) ?></button>
<form class="save-form hidden" action="#">
<input type="text" id="remote_address" placeholder="example.com/owncloud"/>
<input type="text" id="remote_address" placeholder="user@example.com/owncloud"/>
<button id="save-button-confirm" class="icon-confirm svg" disabled></button>
</form>
</span>