Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
get rid of DiscoveryManager dependency
  • Loading branch information
butonic authored and tomneedham committed Aug 30, 2017
commit 059a0e7c3e7329d2c38ce5b42f6e1d7b12e02f79
30 changes: 30 additions & 0 deletions apps/federatedfilesharing/appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
$app = new \OCA\FederatedFileSharing\AppInfo\Application('federatedfilesharing');

use OCA\FederatedFileSharing\Notifier;
use OCP\Share\Events\AcceptShare;
use OCP\Share\Events\DeclineShare;

$manager = \OC::$server->getNotificationManager();
$manager->registerNotifier(function() {
Expand All @@ -42,3 +44,31 @@
// FIXME the OCA\Files::loadAdditionalScripts event is only fired by the ViewController of the files app ... but we are nowadays using webdav.
// FIXME versions, comments, tags and sharing ui still uses it https://github.com/owncloud/core/search?utf8=%E2%9C%93&q=loadAdditionalScripts&type=
OCP\Util::connectHook('OCP\Share', 'share_link_access', 'OCA\FederatedFileSharing\HookHandler', 'loadPublicJS');

// react to accept and decline share events
$eventDispatcher = \OC::$server->getEventDispatcher();
$eventDispatcher->addListener(
AcceptShare::class,
function(AcceptShare $event) use ($app) {
/** @var \OCA\FederatedFileSharing\Notifications $notifications */
$notifications = $app->getContainer()->query('OCA\FederatedFileSharing\Notifications');
$notifications->sendAcceptShare(
$event->getRemote(),
$event->getRemoteId(),
$event->getShareToken()
);
}
);

$eventDispatcher->addListener(
DeclineShare::class,
function(DeclineShare $event) use ($app) {
/** @var \OCA\FederatedFileSharing\Notifications $notifications */
$notifications = $app->getContainer()->query('OCA\FederatedFileSharing\Notifications');
$notifications->sendDeclineShare(
$event->getRemote(),
$event->getRemoteId(),
$event->getShareToken()
);
}
);
7 changes: 3 additions & 4 deletions apps/federatedfilesharing/lib/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function requestReShare($token, $id, $shareId, $remote, $shareWith, $perm
* @return bool
*/
public function sendRemoteUnShare($remote, $id, $token) {
$this->sendUpdateToRemote($remote, $id, $token, 'unshare');
return $this->sendUpdateToRemote($remote, $id, $token, 'unshare');
}

/**
Expand All @@ -174,7 +174,7 @@ public function sendRemoteUnShare($remote, $id, $token) {
* @return bool
*/
public function sendRevokeShare($remote, $id, $token) {
$this->sendUpdateToRemote($remote, $id, $token, 'revoke');
return $this->sendUpdateToRemote($remote, $id, $token, 'revoke');
}

/**
Expand All @@ -187,7 +187,7 @@ public function sendRevokeShare($remote, $id, $token) {
* @return bool
*/
public function sendPermissionChange($remote, $remoteId, $token, $permissions) {
$this->sendUpdateToRemote($remote, $remoteId, $token, 'permissions', ['permissions' => $permissions]);
return $this->sendUpdateToRemote($remote, $remoteId, $token, 'permissions', ['permissions' => $permissions]);
}

/**
Expand Down Expand Up @@ -258,7 +258,6 @@ public function sendUpdateToRemote($remote, $remoteId, $token, $action, $data =
return false;
}


/**
* return current timestamp
*
Expand Down
14 changes: 3 additions & 11 deletions apps/files_sharing/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/**
* @author Björn Schießle <[email protected]>
* @author Joas Schilling <[email protected]>
* @author Jörn Friedrich Dreyer <[email protected]>
* @author Lukas Reschke <[email protected]>
* @author Robin Appelman <[email protected]>
* @author Roeland Jago Douma <[email protected]>
Expand All @@ -26,15 +27,13 @@

namespace OCA\Files_Sharing\AppInfo;

use OCA\FederatedFileSharing\DiscoveryManager;
use OCA\Files_Sharing\MountProvider;
use OCP\AppFramework\App;
use OC\AppFramework\Utility\SimpleContainer;
use OCA\Files_Sharing\Controllers\ExternalSharesController;
use OCA\Files_Sharing\Controllers\ShareController;
use OCA\Files_Sharing\Middleware\SharingCheckMiddleware;
use \OCP\IContainer;
use OCA\Files_Sharing\Capabilities;

class Application extends App {
public function __construct(array $urlParams = []) {
Expand All @@ -47,7 +46,6 @@ public function __construct(array $urlParams = []) {
* Controllers
*/
$container->registerService('ShareController', function (SimpleContainer $c) use ($server) {
$federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application('federatedfilesharing');
return new ShareController(
$c->query('AppName'),
$c->query('Request'),
Expand All @@ -59,8 +57,7 @@ public function __construct(array $urlParams = []) {
$server->getShareManager(),
$server->getSession(),
$server->getPreviewManager(),
$server->getRootFolder(),
$federatedSharingApp->getFederatedShareProvider()
$server->getRootFolder()
);
});
$container->registerService('ExternalSharesController', function (SimpleContainer $c) {
Expand All @@ -81,17 +78,12 @@ public function __construct(array $urlParams = []) {
$container->registerService('ExternalManager', function (SimpleContainer $c) use ($server) {
$user = $server->getUserSession()->getUser();
$uid = $user ? $user->getUID() : null;
$discoveryManager = new DiscoveryManager(
\OC::$server->getMemCacheFactory(),
\OC::$server->getHTTPClientService()
);
return new \OCA\Files_Sharing\External\Manager(
$server->getDatabaseConnection(),
\OC\Files\Filesystem::getMountManager(),
\OC\Files\Filesystem::getLoader(),
$server->getHTTPHelper(),
$server->getNotificationManager(),
$discoveryManager,
$server->getEventDispatcher(),
$uid
);
});
Expand Down
64 changes: 27 additions & 37 deletions apps/files_sharing/lib/External/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
namespace OCA\Files_Sharing\External;

use OC\Files\Filesystem;
use OCA\FederatedFileSharing\DiscoveryManager;
use OCP\Files;
use OCP\Notification\IManager;
use OCP\Share\Events\AcceptShare;
use OCP\Share\Events\DeclineShare;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class Manager {
const STORAGE = '\OCA\Files_Sharing\External\Storage';
Expand All @@ -58,40 +60,35 @@ class Manager {
private $storageLoader;

/**
* @var \OC\HTTPHelper
* @var IManager
*/
private $httpHelper;
private $notificationManager;

/**
* @var IManager
* @var EventDispatcherInterface
*/
private $notificationManager;
/** @var DiscoveryManager */
private $discoveryManager;
private $eventDispatcher;

/**
* @param \OCP\IDBConnection $connection
* @param \OC\Files\Mount\Manager $mountManager
* @param \OCP\Files\Storage\IStorageFactory $storageLoader
* @param \OC\HTTPHelper $httpHelper
* @param IManager $notificationManager
* @param DiscoveryManager $discoveryManager
* @param EventDispatcherInterface $eventDispatcher
* @param string $uid
*/
public function __construct(\OCP\IDBConnection $connection,
\OC\Files\Mount\Manager $mountManager,
\OCP\Files\Storage\IStorageFactory $storageLoader,
\OC\HTTPHelper $httpHelper,
IManager $notificationManager,
DiscoveryManager $discoveryManager,
EventDispatcherInterface $eventDispatcher,
$uid) {
$this->connection = $connection;
$this->mountManager = $mountManager;
$this->storageLoader = $storageLoader;
$this->httpHelper = $httpHelper;
$this->uid = $uid;
$this->notificationManager = $notificationManager;
$this->discoveryManager = $discoveryManager;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand Down Expand Up @@ -203,7 +200,10 @@ public function acceptShare($id) {
`mountpoint_hash` = ?
WHERE `id` = ? AND `user` = ?');
$acceptShare->execute([1, $mountPoint, $hash, $id, $this->uid]);
$this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'accept');

$this->eventDispatcher->dispatch(
AcceptShare::class, new AcceptShare($share)
);

\OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $share['remote']]);

Expand All @@ -228,7 +228,11 @@ public function declineShare($id) {
$removeShare = $this->connection->prepare('
DELETE FROM `*PREFIX*share_external` WHERE `id` = ? AND `user` = ?');
$removeShare->execute([$id, $this->uid]);
$this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline');

$this->eventDispatcher->dispatch(
DeclineShare::class,
new DeclineShare($share)
);

$this->processNotification($id);
return true;
Expand All @@ -248,26 +252,6 @@ public function processNotification($remoteShare) {
$this->notificationManager->markProcessed($filter);
}

/**
* inform remote server whether server-to-server share was accepted/declined
*
* @param string $remote
* @param string $token
* @param int $remoteId Share id on the remote host
* @param string $feedback
* @return boolean
*/
private function sendFeedbackToRemote($remote, $token, $remoteId, $feedback) {

$url = rtrim($remote, '/') . $this->discoveryManager->getShareEndpoint($remote) . '/' . $remoteId . '/' . $feedback . '?format=' . \OCP\Share::RESPONSE_FORMAT;
$fields = ['token' => $token];

$result = $this->httpHelper->post($url, $fields);
$status = json_decode($result['result'], true);

return ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200));
}

/**
* remove '/user/files' from the path and trailing slashes
*
Expand Down Expand Up @@ -342,7 +326,10 @@ public function removeShare($mountPoint) {

if ($result) {
$share = $getShare->fetch();
$this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline');
$this->eventDispatcher->dispatch(
DeclineShare::class,
new DeclineShare($share)
);
}
$getShare->closeCursor();

Expand Down Expand Up @@ -399,7 +386,10 @@ public function removeUserShares($uid) {
if ($result) {
$shares = $getShare->fetchAll();
foreach($shares as $share) {
$this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline');
$this->eventDispatcher->dispatch(
DeclineShare::class,
new DeclineShare($share)
);
}
}

Expand Down
25 changes: 25 additions & 0 deletions lib/public/Share/Events/AcceptShare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* @author Jörn Friedrich Dreyer <[email protected]>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
* @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 OCP\Share\Events;


class AcceptShare extends ShareEvent {}
24 changes: 24 additions & 0 deletions lib/public/Share/Events/DeclineShare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @author Jörn Friedrich Dreyer <[email protected]>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
* @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 OCP\Share\Events;

class DeclineShare extends ShareEvent {}
64 changes: 64 additions & 0 deletions lib/public/Share/Events/ShareEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* @author Jörn Friedrich Dreyer <[email protected]>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
* @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 OCP\Share\Events;


use Symfony\Component\EventDispatcher\Event;

class ShareEvent extends Event {

// TODO when the sharing code uses a Share entity use that instead of an array
/** @var array */
private $share;

public function __construct($share) {
$this->share = $share;
}

/**
* @return array
*/
public function getShare() {
return $this->share;
}

/**
* @return string url
*/
public function getRemote() {
return $this->share['remote'];
}

/**
* @return int
*/
public function getRemoteId() {
return (int)$this->share['remote_id'];
}

/**
* @return string
*/
public function getShareToken() {
return $this->share['share_token'];
}
}