Convert Share API to use the AppFramework

This commit is contained in:
Roeland Jago Douma 2016-07-19 14:13:27 +02:00
parent 84e6b8d9d0
commit a32caa4904
No known key found for this signature in database
GPG Key ID: 1E152838F164D13B
3 changed files with 231 additions and 87 deletions

View File

@ -40,6 +40,36 @@ $application->registerRoutes($this, [
'verb' => 'GET'
],
],
'ocs' => [
/*
* OCS Share API
*/
[
'name' => 'ShareAPI#getShares',
'url' => '/api/v1/shares',
'verb' => 'GET',
],
[
'name' => 'ShareAPI#createShare',
'url' => '/api/v1/shares',
'verb' => 'POST',
],
[
'name' => 'ShareAPI#getShare',
'url' => '/api/v1/shares/{id}',
'verb' => 'GET',
],
[
'name' => 'ShareAPI#updateShare',
'url' => '/api/v1/shares/{id}',
'verb' => 'PUT',
],
[
'name' => 'ShareAPI#deleteShare',
'url' => '/api/v1/shares/{id}',
'verb' => 'DELETE',
],
],
]);
/** @var $this \OCP\Route\IRouter */
@ -59,33 +89,6 @@ $this->create('sharing_external_shareinfo', '/shareinfo')
//TODO: SET: mail notification, waiting for PR #4689 to be accepted
$OCSShare = new \OCA\Files_Sharing\API\OCSShareWrapper();
API::register('get',
'/apps/files_sharing/api/v1/shares',
[$OCSShare, 'getAllShares'],
'files_sharing');
API::register('post',
'/apps/files_sharing/api/v1/shares',
[$OCSShare, 'createShare'],
'files_sharing');
API::register('get',
'/apps/files_sharing/api/v1/shares/{id}',
[$OCSShare, 'getShare'],
'files_sharing');
API::register('put',
'/apps/files_sharing/api/v1/shares/{id}',
[$OCSShare, 'updateShare'],
'files_sharing');
API::register('delete',
'/apps/files_sharing/api/v1/shares/{id}',
[$OCSShare, 'deleteShare'],
'files_sharing');
API::register('get',
'/apps/files_sharing/api/v1/remote_shares',
array('\OCA\Files_Sharing\API\Remote', 'getShares'),

View File

@ -23,6 +23,7 @@
*/
namespace OCA\Files_Sharing\API;
use OCP\AppFramework\OCSController;
use OCP\Files\NotFoundException;
use OCP\IGroupManager;
use OCP\IL10N;
@ -32,7 +33,6 @@ use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Files\IRootFolder;
use OCP\Lock\LockedException;
use OCP\Share;
use OCP\Share\IManager;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\Exceptions\GenericShareException;
@ -43,7 +43,7 @@ use OCP\Lock\ILockingProvider;
*
* @package OCA\Files_Sharing\API
*/
class Share20OCS {
class Share20OCS extends OCSController {
/** @var IManager */
private $shareManager;
@ -52,7 +52,7 @@ class Share20OCS {
/** @var IUserManager */
private $userManager;
/** @var IRequest */
private $request;
protected $request;
/** @var IRootFolder */
private $rootFolder;
/** @var IURLGenerator */
@ -65,24 +65,29 @@ class Share20OCS {
/**
* Share20OCS constructor.
*
* @param string $appName
* @param IRequest $request
* @param IManager $shareManager
* @param IGroupManager $groupManager
* @param IUserManager $userManager
* @param IRequest $request
* @param IRootFolder $rootFolder
* @param IURLGenerator $urlGenerator
* @param IUser $currentUser
* @param IL10N $l10n
*/
public function __construct(
$appName,
IRequest $request,
IManager $shareManager,
IGroupManager $groupManager,
IUserManager $userManager,
IRequest $request,
IRootFolder $rootFolder,
IURLGenerator $urlGenerator,
IUser $currentUser,
IL10N $l10n
) {
parent::__construct($appName, $request);
$this->shareManager = $shareManager;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
@ -133,7 +138,7 @@ class Share20OCS {
} else {
$result['item_type'] = 'file';
}
$result['mimetype'] = $node->getMimeType();
$result['mimetype'] = $node->getMimetype();
$result['storage_id'] = $node->getStorage()->getId();
$result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
$result['item_source'] = $node->getId();
@ -175,88 +180,126 @@ class Share20OCS {
/**
* Get a specific share by id
*
* @NoAdminRequired
*
* @param string $id
* @return \OC_OCS_Result
* @return array
*/
public function getShare($id) {
if (!$this->shareManager->shareApiEnabled()) {
return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled'));
return [
'statuscode' => 404,
'message' => $this->l->t('Share API is disabled')
];
}
try {
$share = $this->getShareById($id);
} catch (ShareNotFound $e) {
return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist'));
return [
'statuscode' => 404,
'message' => $this->l->t('Wrong share ID, share doesn\'t exist'),
];
}
if ($this->canAccessShare($share)) {
try {
$share = $this->formatShare($share);
return new \OC_OCS_Result([$share]);
return [
'data' => [$share],
];
} catch (NotFoundException $e) {
//Fall trough
}
}
return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist'));
return [
'statuscode' => 404,
'message' => $this->l->t('Wrong share ID, share doesn\'t exist')
];
}
/**
* Delete a share
*
* @NoAdminRequired
*
* @param string $id
* @return \OC_OCS_Result
* @return \OC\OCS\Result
*/
public function deleteShare($id) {
if (!$this->shareManager->shareApiEnabled()) {
return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled'));
return [
'statuscode' => 404,
'message' => $this->l->t('Share API is disabled')
];
}
try {
$share = $this->getShareById($id);
} catch (ShareNotFound $e) {
return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist'));
return [
'statuscode' => 404,
'message' => $this->l->t('Wrong share ID, share doesn\'t exist')
];
}
try {
$share->getNode()->lock(ILockingProvider::LOCK_SHARED);
} catch (LockedException $e) {
return new \OC_OCS_Result(null, 404, 'could not delete share');
return [
'statuscode' => 404,
'message' => $this->l->t('could not delete share')
];
}
if (!$this->canAccessShare($share, false)) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 404, $this->l->t('Could not delete share'));
return [
'statuscode' => 404,
'message' => $this->l->t('Could not delete share')
];
}
$this->shareManager->deleteShare($share);
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result();
return [];
}
/**
* @return \OC_OCS_Result
* @NoAdminRequired
*
* @return array
*/
public function createShare() {
$share = $this->shareManager->newShare();
if (!$this->shareManager->shareApiEnabled()) {
return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled'));
return [
'statuscode' => 404,
'message' => $this->l->t('Share API is disabled')
];
}
// Verify path
$path = $this->request->getParam('path', null);
if ($path === null) {
return new \OC_OCS_Result(null, 404, $this->l->t('Please specify a file or folder path'));
return [
'statuscode' => 404,
'message' => $this->l->t('Please specify a file or folder path')
];
}
$userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID());
try {
$path = $userFolder->get($path);
} catch (NotFoundException $e) {
return new \OC_OCS_Result(null, 404, $this->l->t('Wrong path, file/folder doesn\'t exist'));
return [
'statuscode' => 404,
'messagecode' => $this->l->t('Wrong path, file/folder doesn\'t exist')
];
}
$share->setNode($path);
@ -264,7 +307,10 @@ class Share20OCS {
try {
$share->getNode()->lock(ILockingProvider::LOCK_SHARED);
} catch (LockedException $e) {
return new \OC_OCS_Result(null, 404, 'Could not create share');
return [
'statuscode' => 404,
'message' => $this->l->t('Could not create share')
];
}
// Parse permissions (if available)
@ -277,7 +323,10 @@ class Share20OCS {
if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 404, 'invalid permissions');
return [
'statuscode' => 404,
'message' => 'invalid permissions'
];
}
// Shares always require read permissions
@ -305,20 +354,29 @@ class Share20OCS {
// Valid user is required to share
if ($shareWith === null || !$this->userManager->userExists($shareWith)) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 404, $this->l->t('Please specify a valid user'));
return [
'statuscode' => 404,
'message' => $this->l->t('Please specify a valid user')
];
}
$share->setSharedWith($shareWith);
$share->setPermissions($permissions);
} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
if (!$this->shareManager->allowGroupSharing()) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 404, $this->l->t('Group sharing is disabled by the administrator'));
return [
'statuscode' => 404,
'message' => $this->l->t('Group sharing is disabled by the administrator')
];
}
// Valid group is required to share
if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 404, $this->l->t('Please specify a valid group'));
return [
'statuscode' => 404,
'message' => $this->l->t('Please specify a valid group')
];
}
$share->setSharedWith($shareWith);
$share->setPermissions($permissions);
@ -326,7 +384,10 @@ class Share20OCS {
//Can we even share links?
if (!$this->shareManager->shareApiAllowLinks()) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 404, $this->l->t('Public link sharing is disabled by the administrator'));
return [
'statuscode' => 404,
'message' => $this->l->t('Public link sharing is disabled by the administrator')
];
}
/*
@ -336,7 +397,7 @@ class Share20OCS {
$existingShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_LINK, $path, false, 1, 0);
if (!empty($existingShares)) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result($this->formatShare($existingShares[0]));
return ['data' => $this->formatShare($existingShares[0])];
}
$publicUpload = $this->request->getParam('publicUpload', null);
@ -344,13 +405,19 @@ class Share20OCS {
// Check if public upload is allowed
if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 403, $this->l->t('Public upload disabled by the administrator'));
return [
'statuscode' => 403,
'message' => $this->l->t('Public upload disabled by the administrator')
];
}
// Public upload can only be set for folders
if ($path instanceof \OCP\Files\File) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 404, $this->l->t('Public upload is only possible for publicly shared folders'));
return [
'statuscode' => 404,
'message' => $this->l->t('Public upload is only possible for publicly shared folders')
];
}
$share->setPermissions(
@ -379,21 +446,30 @@ class Share20OCS {
$share->setExpirationDate($expireDate);
} catch (\Exception $e) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 404, $this->l->t('Invalid date, date format must be YYYY-MM-DD'));
return [
'statuscode' => 404,
'message' => $this->l->t('Invalid date, date format must be YYYY-MM-DD')
];
}
}
} else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 403, $this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType]));
return [
'statuscode' => 403,
'message' => $this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType])
];
}
$share->setSharedWith($shareWith);
$share->setPermissions($permissions);
} else {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 400, $this->l->t('Unknown share type'));
return [
'statuscode' => 400,
$this->l->t('Unknown share type')
];
}
$share->setShareType($shareType);
@ -404,22 +480,28 @@ class Share20OCS {
} catch (GenericShareException $e) {
$code = $e->getCode() === 0 ? 403 : $e->getCode();
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, $code, $e->getHint());
return [
'statuscode' => $code,
'message' => $e->getHint()
];
}catch (\Exception $e) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 403, $e->getMessage());
return [
'statuscode' => 403,
'message' => $e->getMessage()
];
}
$output = $this->formatShare($share);
$share->getNode()->unlock(\OCP\Lock\ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result($output);
return ['data' => $output];
}
/**
* @param \OCP\Files\File|\OCP\Files\Folder $node
* @return \OC_OCS_Result
* @return array
*/
private function getSharedWithMe($node = null) {
$userShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $node, -1, 0);
@ -438,16 +520,19 @@ class Share20OCS {
}
}
return new \OC_OCS_Result($formatted);
return ['data' => $formatted];
}
/**
* @param \OCP\Files\Folder $folder
* @return \OC_OCS_Result
* @return array
*/
private function getSharesInDir($folder) {
if (!($folder instanceof \OCP\Files\Folder)) {
return new \OC_OCS_Result(null, 400, $this->l->t('Not a directory'));
return [
'statuscode' => 400,
'message' => $this->l->t('Not a directory')
];
}
$nodes = $folder->getDirectoryListing();
@ -471,23 +556,25 @@ class Share20OCS {
}
}
return new \OC_OCS_Result($formatted);
return ['data' => $formatted];
}
/**
* The getShares function.
*
* @NoAdminRequired
*
* - Get shares by the current user
* - Get shares by the current user and reshares (?reshares=true)
* - Get shares with the current user (?shared_with_me=true)
* - Get shares for a specific path (?path=...)
* - Get all shares in a folder (?subfiles=true&path=..)
*
* @return \OC_OCS_Result
* @return array
*/
public function getShares() {
if (!$this->shareManager->shareApiEnabled()) {
return new \OC_OCS_Result();
return [];
}
$sharedWithMe = $this->request->getParam('shared_with_me', null);
@ -501,9 +588,15 @@ class Share20OCS {
$path = $userFolder->get($path);
$path->lock(ILockingProvider::LOCK_SHARED);
} catch (\OCP\Files\NotFoundException $e) {
return new \OC_OCS_Result(null, 404, $this->l->t('Wrong path, file/folder doesn\'t exist'));
return [
'statuscode' => 404,
'message' => $this->l->t('Wrong path, file/folder doesn\'t exist')
];
} catch (LockedException $e) {
return new \OC_OCS_Result(null, 404, $this->l->t('Could not lock path'));
return [
'statuscode' => 404,
'message' => $this->l->t('Could not lock path')
];
}
}
@ -553,29 +646,40 @@ class Share20OCS {
$path->unlock(ILockingProvider::LOCK_SHARED);
}
return new \OC_OCS_Result($formatted);
return ['data' => $formatted];
}
/**
* @NoAdminRequired
*
* @param int $id
* @return \OC_OCS_Result
* @return array
*/
public function updateShare($id) {
if (!$this->shareManager->shareApiEnabled()) {
return new \OC_OCS_Result(null, 404, $this->l->t('Share API is disabled'));
return [
'statuscode' => 404,
'message' => $this->l->t('Share API is disabled')
];
}
try {
$share = $this->getShareById($id);
} catch (ShareNotFound $e) {
return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist'));
return [
'statuscode' => 404,
'message' => $this->l->t('Wrong share ID, share doesn\'t exist')
];
}
$share->getNode()->lock(\OCP\Lock\ILockingProvider::LOCK_SHARED);
if (!$this->canAccessShare($share, false)) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist'));
return [
'statuscode' => 404,
'message' => $this->l->t('Wrong share ID, share doesn\'t exist')
];
}
$permissions = $this->request->getParam('permissions', null);
@ -589,7 +693,10 @@ class Share20OCS {
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
if ($permissions === null && $password === null && $publicUpload === null && $expireDate === null) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 400, 'Wrong or no update parameter given');
return [
'statuscode' => 400,
'message' => 'Wrong or no update parameter given'
];
}
$newPermissions = null;
@ -612,7 +719,10 @@ class Share20OCS {
])
) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 400, $this->l->t('Can\'t change permissions for public share links'));
return [
'statuscode' => 400,
'message' => $this->l->t('Can\'t change permissions for public share links')
];
}
if (
@ -623,12 +733,18 @@ class Share20OCS {
) {
if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 403, $this->l->t('Public upload disabled by the administrator'));
return [
'statuscode' => 403,
'message' => $this->l->t('Public upload disabled by the administrator')
];
}
if (!($share->getNode() instanceof \OCP\Files\Folder)) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 400, $this->l->t('Public upload is only possible for publicly shared folders'));
return [
'statuscode' => 400,
'message' => $this->l->t('Public upload is only possible for publicly shared folders')
];
}
// normalize to correct public upload permissions
@ -646,7 +762,10 @@ class Share20OCS {
$expireDate = $this->parseDate($expireDate);
} catch (\Exception $e) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 400, $e->getMessage());
return [
'statuscode' => 400,
'message' => $e->getMessage()
];
}
$share->setExpirationDate($expireDate);
}
@ -661,7 +780,10 @@ class Share20OCS {
// For other shares only permissions is valid.
if ($permissions === null) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 400, $this->l->t('Wrong or no update parameter given'));
return [
'statuscode' => 400,
'message' => $this->l->t('Wrong or no update parameter given')
];
} else {
$permissions = (int)$permissions;
$share->setPermissions($permissions);
@ -681,7 +803,10 @@ class Share20OCS {
if ($share->getPermissions() & ~$maxPermissions) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 404, $this->l->t('Cannot increase permissions'));
return [
'statuscode' => 404,
'message' => $this->l->t('Cannot increase permissions')
];
}
}
}
@ -691,12 +816,15 @@ class Share20OCS {
$share = $this->shareManager->updateShare($share);
} catch (\Exception $e) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result(null, 400, $e->getMessage());
return [
'statuscode' => 400,
'message' => $e->getMessage()
];
}
$share->getNode()->unlock(\OCP\Lock\ILockingProvider::LOCK_SHARED);
return new \OC_OCS_Result($this->formatShare($share));
return ['data' => $this->formatShare($share)];
}
/**

View File

@ -28,6 +28,7 @@
namespace OCA\Files_Sharing\AppInfo;
use OCA\FederatedFileSharing\DiscoveryManager;
use OCA\Files_Sharing\API\Share20OCS;
use OCA\Files_Sharing\MountProvider;
use OCP\AppFramework\App;
use OC\AppFramework\Utility\SimpleContainer;
@ -35,7 +36,6 @@ 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 = array()) {
@ -73,6 +73,19 @@ class Application extends App {
$c->query('HttpClientService')
);
});
$container->registerService('ShareAPIController', function (SimpleContainer $c) use ($server) {
return new Share20OCS(
$c->query('AppName'),
$c->query('Request'),
$server->getShareManager(),
$server->getGroupManager(),
$server->getUserManager(),
$server->getRootFolder(),
$server->getURLGenerator(),
$server->getUserSession()->getUser(),
$server->getL10N($c->query('AppName'))
);
});
/**
* Core class wrappers