Skip to content

Commit

Permalink
Cleanup ocs routing
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo committed Jul 18, 2018
1 parent 4d925f5 commit 238fe18
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 116 deletions.
14 changes: 14 additions & 0 deletions apps/federatedfilesharing/appinfo/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return [
'ocs' => [
['root' => '/cloud', 'name' => 'RequestHandler#createShare', 'url' => '/shares', 'verb' => 'POST'],
['root' => '/cloud', 'name' => 'RequestHandler#reShare', 'url' => '/shares/{id}/reshare', 'verb' => 'POST'],
['root' => '/cloud', 'name' => 'RequestHandler#updatePermissions', 'url' => '/shares/{id}/permissions', 'verb' => 'POST'],
['root' => '/cloud', 'name' => 'RequestHandler#acceptShare', 'url' => '', 'verb' => 'POST'],
['root' => '/cloud', 'name' => 'RequestHandler#acceptShare', 'url' => '/shares/{id}/accept', 'verb' => 'POST'],
['root' => '/cloud', 'name' => 'RequestHandler#declineShare', 'url' => '/shares/{id}/decline', 'verb' => 'POST'],
['root' => '/cloud', 'name' => 'RequestHandler#unshare', 'url' => '/shares/{id}/unshare', 'verb' => 'POST'],
['root' => '/cloud', 'name' => 'RequestHandler#revoke', 'url' => '/shares/{id}/revoke', 'verb' => 'POST'],
]
];
72 changes: 66 additions & 6 deletions apps/federatedfilesharing/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@

namespace OCA\FederatedFileSharing\AppInfo;

use OCA\FederatedFileSharing\AddressHandler;
use OCA\FederatedFileSharing\DiscoveryManager;
use OCA\FederatedFileSharing\FederatedShareProvider;
use OCA\FederatedFileSharing\FedShareManager;
use OCA\FederatedFileSharing\Notifications;
use OCA\FederatedFileSharing\Controller\RequestHandlerController;
use OCA\FederatedFileSharing\TokenHandler;
use OCP\AppFramework\App;

class Application extends App {
Expand All @@ -33,6 +38,61 @@ class Application extends App {
/** @var FedShareManager */
protected $federatedShareManager;

public function __construct() {
parent::__construct('federatedfilesharing');
$container = $this->getContainer();
$server = $container->getServer();

$container->registerService(
'AddressHandler',
function ($c) use ($server) {
return new AddressHandler(
$server->getURLGenerator(),
$server->getL10N('federatedfilesharing')
);
}
);

$container->registerService(
'DiscoveryManager',
function ($c) use ($server) {
return new DiscoveryManager(
$server->getMemCacheFactory(),
$server->getHTTPClientService()
);
}
);

$container->registerService(
'Notifications',
function ($c) use ($server) {
return new Notifications(
$c->query('AddressHandler'),
$server->getHTTPClientService(),
$c->query('DiscoveryManager'),
$server->getJobList(),
$server->getConfig()
);
}
);

$container->registerService(
'RequestHandlerController',
function ($c) use ($server) {
return new RequestHandlerController(
$c->query('AppName'),
$c->query('Request'),
$this->getFederatedShareProvider(),
$server->getDatabaseConnection(),
$c->query('Notifications'),
$c->query('AddressHandler'),
$this->getFederatedShareManager(),
$server->getEventDispatcher()
);
}
);
}

/**
* get instance of federated share provider
*
Expand Down Expand Up @@ -60,7 +120,7 @@ public function getFederatedShareManager() {
/**
* initialize federated share manager
*/
protected function initFederatedSharemanager() {
protected function initFederatedShareManager() {
$this->federatedShareManager = new FedShareManager(
$this->getFederatedShareProvider(),
\OC::$server->getUserManager(),
Expand All @@ -73,26 +133,26 @@ protected function initFederatedSharemanager() {
* initialize federated share provider
*/
protected function initFederatedShareProvider() {
$addressHandler = new \OCA\FederatedFileSharing\AddressHandler(
$addressHandler = new AddressHandler(
\OC::$server->getURLGenerator(),
\OC::$server->getL10N('federatedfilesharing')
);
$discoveryManager = new \OCA\FederatedFileSharing\DiscoveryManager(
$discoveryManager = new DiscoveryManager(
\OC::$server->getMemCacheFactory(),
\OC::$server->getHTTPClientService()
);
$notifications = new \OCA\FederatedFileSharing\Notifications(
$notifications = new Notifications(
$addressHandler,
\OC::$server->getHTTPClientService(),
$discoveryManager,
\OC::$server->getJobList(),
\OC::$server->getConfig()
);
$tokenHandler = new \OCA\FederatedFileSharing\TokenHandler(
$tokenHandler = new TokenHandler(
\OC::$server->getSecureRandom()
);

$this->federatedShareProvider = new \OCA\FederatedFileSharing\FederatedShareProvider(
$this->federatedShareProvider = new FederatedShareProvider(
\OC::$server->getDatabaseConnection(),
$addressHandler,
$notifications,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@
*
*/

namespace OCA\FederatedFileSharing;
namespace OCA\FederatedFileSharing\Controller;

use OC\OCS\Result;
use OCA\FederatedFileSharing\AddressHandler;
use OCA\FederatedFileSharing\FederatedShareProvider;
use OCA\FederatedFileSharing\FedShareManager;
use OCA\FederatedFileSharing\Notifications;
use OCA\Files_Sharing\Activity;
use OCP\AppFramework\Http;
use OCP\AppFramework\OCSController;
use OCP\Constants;
use OCP\IDBConnection;
use OCP\IRequest;
Expand All @@ -38,26 +43,20 @@
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class RequestHandler
* Class RequestHandlerController
*
* handles OCS Request to the federated share API
* Handles OCS Request to the federated share API
*
* @package OCA\FederatedFileSharing\API
*/
class RequestHandler {
class RequestHandlerController extends OCSController {

/** @var FederatedShareProvider */
private $federatedShareProvider;

/** @var IDBConnection */
private $connection;

/** @var Share\IManager */
private $shareManager;

/** @var IRequest */
private $request;

/** @var Notifications */
private $notifications;

Expand All @@ -76,41 +75,43 @@ class RequestHandler {
/**
* Server2Server constructor.
*
* @param string $appName
* @param IRequest $request
* @param FederatedShareProvider $federatedShareProvider
* @param IDBConnection $connection
* @param Share\IManager $shareManager
* @param IRequest $request
* @param Notifications $notifications
* @param AddressHandler $addressHandler
* @param FedShareManager $fedShareManager
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(FederatedShareProvider $federatedShareProvider,
IDBConnection $connection,
Share\IManager $shareManager,
public function __construct($appName,
IRequest $request,
FederatedShareProvider $federatedShareProvider,
IDBConnection $connection,
Notifications $notifications,
AddressHandler $addressHandler,
FedShareManager $fedShareManager,
EventDispatcherInterface $eventDispatcher
) {
parent::__construct($appName, $request);

$this->federatedShareProvider = $federatedShareProvider;
$this->connection = $connection;
$this->shareManager = $shareManager;
$this->request = $request;
$this->notifications = $notifications;
$this->addressHandler = $addressHandler;
$this->fedShareManager = $fedShareManager;
$this->eventDispatcher = $eventDispatcher;
}

/**
* @NoCSRFRequired
* @PublicPage
*
* create a new share
*
* @param array $params
* @return Result
*/
public function createShare($params) {
public function createShare() {
if (!$this->isS2SEnabled(true)) {
return new Result(null, 503, 'Server does not support federated cloud sharing');
}
Expand Down Expand Up @@ -221,14 +222,16 @@ public function createShare($params) {
}

/**
* @NoCSRFRequired
* @PublicPage
*
* create re-share on behalf of another user
*
* @param $params
* @param int $id
*
* @return Result
*/
public function reShare($params) {
$id = isset($params['id']) ? (int)$params['id'] : null;
public function reShare($id) {
$token = $this->request->getParam('token', null);
$shareWith = $this->request->getParam('shareWith', null);
$permission = (int)$this->request->getParam('permission', null);
Expand Down Expand Up @@ -272,17 +275,20 @@ public function reShare($params) {
}

/**
* @NoCSRFRequired
* @PublicPage
*
* accept server-to-server share
*
* @param array $params
* @param int $id
*
* @return Result
*/
public function acceptShare($params) {
public function acceptShare($id) {
if (!$this->isS2SEnabled()) {
return new Result(null, 503, 'Server does not support federated cloud sharing');
}

$id = $params['id'];
$token = isset($_POST['token']) ? $_POST['token'] : null;

try {
Expand All @@ -302,17 +308,20 @@ public function acceptShare($params) {
}

/**
* @NoCSRFRequired
* @PublicPage
*
* decline server-to-server share
*
* @param array $params
* @param int $id
*
* @return Result
*/
public function declineShare($params) {
public function declineShare($id) {
if (!$this->isS2SEnabled()) {
return new Result(null, 503, 'Server does not support federated cloud sharing');
}

$id = (int)$params['id'];
$token = isset($_POST['token']) ? $_POST['token'] : null;

try {
Expand All @@ -333,17 +342,20 @@ public function declineShare($params) {
}

/**
* @NoCSRFRequired
* @PublicPage
*
* remove server-to-server share if it was unshared by the owner
*
* @param array $params
* @param int $id
*
* @return Result
*/
public function unshare($params) {
public function unshare($id) {
if (!$this->isS2SEnabled()) {
return new Result(null, 503, 'Server does not support federated cloud sharing');
}

$id = $params['id'];
$token = isset($_POST['token']) ? $_POST['token'] : null;

$query = \OCP\DB::prepare('SELECT * FROM `*PREFIX*share_external` WHERE `remote_id` = ? AND `share_token` = ?');
Expand Down Expand Up @@ -388,14 +400,16 @@ private function cleanupRemote($remote) {
}

/**
* @NoCSRFRequired
* @PublicPage
*
* federated share was revoked, either by the owner or the re-sharer
*
* @param $params
* @param int $id
*
* @return Result
*/
public function revoke($params) {
$id = (int)$params['id'];
public function revoke($id) {
$token = $this->request->getParam('token');

$share = $this->federatedShareProvider->getShareById($id);
Expand Down Expand Up @@ -470,6 +484,9 @@ protected function verifyShare(Share\IShare $share, $token) {
}

/**
* @NoCSRFRequired
* @PublicPage
*
* update share information to keep federated re-shares in sync
*
* @param array $params
Expand Down
5 changes: 4 additions & 1 deletion lib/private/AppFramework/Routing/RouteConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ private function processOCS(array $routes) {
$postFix = $ocsRoute['postfix'];
}

$url = $ocsRoute['url'];
$root = (isset($ocsRoute['root']))
? $ocsRoute['root']
: '/apps/' . $this->appName;
$url = $root . $ocsRoute['url'];
$verb = isset($ocsRoute['verb']) ? \strtoupper($ocsRoute['verb']) : 'GET';

$split = \explode('#', $name, 2);
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Route/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function loadRoutes($app = null) {

// Also add the OCS collection
$collection = $this->getCollection($app.'.ocs');
$collection->addPrefix('/ocsapp/apps/' . $app);
$collection->addPrefix('/ocsapp/');
$this->root->addCollection($collection);
}
}
Expand Down
Loading

0 comments on commit 238fe18

Please sign in to comment.