summaryrefslogtreecommitdiffstats
path: root/apps/files_sharing/lib/API/Share20OCS.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files_sharing/lib/API/Share20OCS.php')
-rw-r--r--apps/files_sharing/lib/API/Share20OCS.php227
1 files changed, 110 insertions, 117 deletions
diff --git a/apps/files_sharing/lib/API/Share20OCS.php b/apps/files_sharing/lib/API/Share20OCS.php
index 593e9d877c7..0cce05c3b17 100644
--- a/apps/files_sharing/lib/API/Share20OCS.php
+++ b/apps/files_sharing/lib/API/Share20OCS.php
@@ -23,6 +23,12 @@
*/
namespace OCA\Files_Sharing\API;
+use OCP\AppFramework\Http\DataResponse;
+use OCP\AppFramework\OCS\OCSBadRequestException;
+use OCP\AppFramework\OCS\OCSException;
+use OCP\AppFramework\OCS\OCSForbiddenException;
+use OCP\AppFramework\OCS\OCSNotFoundException;
+use OCP\AppFramework\OCSController;
use OCP\Files\NotFoundException;
use OCP\IGroupManager;
use OCP\IL10N;
@@ -32,7 +38,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 +48,7 @@ use OCP\Lock\ILockingProvider;
*
* @package OCA\Files_Sharing\API
*/
-class Share20OCS {
+class Share20OCS extends OCSController {
/** @var IManager */
private $shareManager;
@@ -52,7 +57,7 @@ class Share20OCS {
/** @var IUserManager */
private $userManager;
/** @var IRequest */
- private $request;
+ protected $request;
/** @var IRootFolder */
private $rootFolder;
/** @var IURLGenerator */
@@ -61,28 +66,35 @@ class Share20OCS {
private $currentUser;
/** @var IL10N */
private $l;
+ /** @var \OCP\Files\Node */
+ private $lockedNode;
/**
* 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 +145,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,96 +187,93 @@ class Share20OCS {
/**
* Get a specific share by id
*
+ * @NoAdminRequired
+ *
* @param string $id
- * @return \OC_OCS_Result
+ * @return DataResponse
+ * @throws OCSNotFoundException
*/
public function getShare($id) {
- if (!$this->shareManager->shareApiEnabled()) {
- return new \OC_OCS_Result(null, 404, $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'));
+ throw new OCSNotFoundException($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 new DataResponse(['data' => [$share]]);
} catch (NotFoundException $e) {
//Fall trough
}
}
- return new \OC_OCS_Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist'));
+ throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
}
/**
* Delete a share
*
+ * @NoAdminRequired
+ *
* @param string $id
- * @return \OC_OCS_Result
+ * @return DataResponse
+ * @throws OCSNotFoundException
*/
public function deleteShare($id) {
- if (!$this->shareManager->shareApiEnabled()) {
- return new \OC_OCS_Result(null, 404, $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'));
+ throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
}
try {
- $share->getNode()->lock(ILockingProvider::LOCK_SHARED);
+ $this->lock($share->getNode());
} catch (LockedException $e) {
- return new \OC_OCS_Result(null, 404, 'could not delete share');
+ throw new OCSNotFoundException($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'));
+ throw new OCSNotFoundException($this->l->t('Could not delete share'));
}
$this->shareManager->deleteShare($share);
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
-
- return new \OC_OCS_Result();
+ return new DataResponse();
}
/**
- * @return \OC_OCS_Result
+ * @NoAdminRequired
+ *
+ * @return DataResponse
+ * @throws OCSNotFoundException
+ * @throws OCSForbiddenException
+ * @throws OCSBadRequestException
+ * @throws OCSException
*/
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'));
- }
-
// 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'));
+ throw new OCSNotFoundException($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'));
+ throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
}
$share->setNode($path);
try {
- $share->getNode()->lock(ILockingProvider::LOCK_SHARED);
+ $this->lock($share->getNode());
} catch (LockedException $e) {
- return new \OC_OCS_Result(null, 404, 'Could not create share');
+ throw new OCSNotFoundException($this->l->t('Could not create share'));
}
// Parse permissions (if available)
@@ -276,8 +285,7 @@ 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');
+ throw new OCSNotFoundException($this->l->t('invalid permissions'));
}
// Shares always require read permissions
@@ -304,29 +312,25 @@ class Share20OCS {
if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
// 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'));
+ throw new OCSNotFoundException($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'));
+ throw new OCSNotFoundException($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'));
+ throw new OCSNotFoundException($this->l->t('Please specify a valid group'));
}
$share->setSharedWith($shareWith);
$share->setPermissions($permissions);
} else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
//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'));
+ throw new OCSNotFoundException($this->l->t('Public link sharing is disabled by the administrator'));
}
/*
@@ -335,22 +339,19 @@ 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 new DataResponse(['data' => $this->formatShare($existingShares[0])]);
}
$publicUpload = $this->request->getParam('publicUpload', null);
if ($publicUpload === 'true') {
// 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'));
+ throw new OCSForbiddenException($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'));
+ throw new OCSNotFoundException($this->l->t('Public upload is only possible for publicly shared folders'));
}
$share->setPermissions(
@@ -378,22 +379,19 @@ class Share20OCS {
$expireDate = $this->parseDate($expireDate);
$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'));
+ throw new OCSNotFoundException($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]));
+ throw new OCSForbiddenException($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'));
+ throw new OCSBadRequestException($this->l->t('Unknown share type'));
}
$share->setShareType($shareType);
@@ -403,23 +401,19 @@ class Share20OCS {
$share = $this->shareManager->createShare($share);
} 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());
+ throw new OCSException($e->getHint(), $code);
}catch (\Exception $e) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
- return new \OC_OCS_Result(null, 403, $e->getMessage());
+ throw new OCSForbiddenException($e->getMessage());
}
$output = $this->formatShare($share);
- $share->getNode()->unlock(\OCP\Lock\ILockingProvider::LOCK_SHARED);
-
- return new \OC_OCS_Result($output);
+ return new DataResponse(['data' => $output]);
}
/**
* @param \OCP\Files\File|\OCP\Files\Folder $node
- * @return \OC_OCS_Result
+ * @return DataResponse
*/
private function getSharedWithMe($node = null) {
$userShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $node, -1, 0);
@@ -438,16 +432,17 @@ class Share20OCS {
}
}
- return new \OC_OCS_Result($formatted);
+ return new DataResponse(['data' => $formatted]);
}
/**
* @param \OCP\Files\Folder $folder
- * @return \OC_OCS_Result
+ * @return DataResponse
+ * @throws OCSBadRequestException
*/
private function getSharesInDir($folder) {
if (!($folder instanceof \OCP\Files\Folder)) {
- return new \OC_OCS_Result(null, 400, $this->l->t('Not a directory'));
+ throw new OCSBadRequestException($this->l->t('Not a directory'));
}
$nodes = $folder->getDirectoryListing();
@@ -471,25 +466,24 @@ class Share20OCS {
}
}
- return new \OC_OCS_Result($formatted);
+ return new DataResponse(['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 DataResponse
+ * @throws OCSNotFoundException
*/
public function getShares() {
- if (!$this->shareManager->shareApiEnabled()) {
- return new \OC_OCS_Result();
- }
-
$sharedWithMe = $this->request->getParam('shared_with_me', null);
$reshares = $this->request->getParam('reshares', null);
$subfiles = $this->request->getParam('subfiles');
@@ -499,27 +493,21 @@ class Share20OCS {
$userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID());
try {
$path = $userFolder->get($path);
- $path->lock(ILockingProvider::LOCK_SHARED);
+ $this->lock($path);
} catch (\OCP\Files\NotFoundException $e) {
- return new \OC_OCS_Result(null, 404, $this->l->t('Wrong path, file/folder doesn\'t exist'));
+ throw new OCSNotFoundException($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'));
+ throw new OCSNotFoundException($this->l->t('Could not lock path'));
}
}
if ($sharedWithMe === 'true') {
$result = $this->getSharedWithMe($path);
- if ($path !== null) {
- $path->unlock(ILockingProvider::LOCK_SHARED);
- }
return $result;
}
if ($subfiles === 'true') {
$result = $this->getSharesInDir($path);
- if ($path !== null) {
- $path->unlock(ILockingProvider::LOCK_SHARED);
- }
return $result;
}
@@ -549,33 +537,29 @@ class Share20OCS {
}
}
- if ($path !== null) {
- $path->unlock(ILockingProvider::LOCK_SHARED);
- }
-
- return new \OC_OCS_Result($formatted);
+ return new DataResponse(['data' => $formatted]);
}
/**
+ * @NoAdminRequired
+ *
* @param int $id
- * @return \OC_OCS_Result
+ * @return DataResponse
+ * @throws OCSNotFoundException
+ * @throws OCSBadRequestException
+ * @throws OCSForbiddenException
*/
public function updateShare($id) {
- if (!$this->shareManager->shareApiEnabled()) {
- return new \OC_OCS_Result(null, 404, $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'));
+ throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
}
- $share->getNode()->lock(\OCP\Lock\ILockingProvider::LOCK_SHARED);
+ $this->lock($share->getNode());
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'));
+ throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
}
$permissions = $this->request->getParam('permissions', null);
@@ -588,8 +572,7 @@ 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');
+ throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given'));
}
$newPermissions = null;
@@ -611,8 +594,7 @@ class Share20OCS {
\OCP\Constants::PERMISSION_CREATE, // hidden file list
])
) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
- return new \OC_OCS_Result(null, 400, $this->l->t('Can\'t change permissions for public share links'));
+ throw new OCSBadRequestException($this->l->t('Can\'t change permissions for public share links'));
}
if (
@@ -622,13 +604,11 @@ class Share20OCS {
$newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)
) {
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'));
+ throw new OCSForbiddenException($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'));
+ throw new OCSBadRequestException($this->l->t('Public upload is only possible for publicly shared folders'));
}
// normalize to correct public upload permissions
@@ -645,8 +625,7 @@ class Share20OCS {
try {
$expireDate = $this->parseDate($expireDate);
} catch (\Exception $e) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
- return new \OC_OCS_Result(null, 400, $e->getMessage());
+ throw new OCSBadRequestException($e->getMessage());
}
$share->setExpirationDate($expireDate);
}
@@ -660,8 +639,7 @@ class Share20OCS {
} else {
// 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'));
+ throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given'));
} else {
$permissions = (int)$permissions;
$share->setPermissions($permissions);
@@ -673,6 +651,7 @@ class Share20OCS {
$incomingShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0);
$incomingShares = array_merge($incomingShares, $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0));
+ /** @var \OCP\Share\IShare[] $incomingShares */
if (!empty($incomingShares)) {
$maxPermissions = 0;
foreach ($incomingShares as $incomingShare) {
@@ -680,8 +659,7 @@ 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'));
+ throw new OCSNotFoundException($this->l->t('Cannot increase permissions'));
}
}
}
@@ -690,13 +668,10 @@ class Share20OCS {
try {
$share = $this->shareManager->updateShare($share);
} catch (\Exception $e) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
- return new \OC_OCS_Result(null, 400, $e->getMessage());
+ throw new OCSBadRequestException($e->getMessage());
}
- $share->getNode()->unlock(\OCP\Lock\ILockingProvider::LOCK_SHARED);
-
- return new \OC_OCS_Result($this->formatShare($share));
+ return new DataResponse(['data' => $this->formatShare($share)]);
}
/**
@@ -782,4 +757,22 @@ class Share20OCS {
return $share;
}
+
+ /**
+ * Lock a Node
+ * @param \OCP\Files\Node $node
+ */
+ private function lock(\OCP\Files\Node $node) {
+ $node->lock(ILockingProvider::LOCK_SHARED);
+ $this->lockedNode = $node;
+ }
+
+ /**
+ * Cleanup the remaining locks
+ */
+ public function cleanup() {
+ if ($this->lockedNode !== null) {
+ $this->lockedNode->unlock(ILockingProvider::LOCK_SHARED);
+ }
+ }
}