Throw OCSNotFoundExceptions

This commit is contained in:
Roeland Jago Douma 2016-07-20 09:55:43 +02:00
parent 54f21bccdf
commit 8b160077f1
No known key found for this signature in database
GPG Key ID: 1E152838F164D13B
1 changed files with 40 additions and 91 deletions

View File

@ -23,6 +23,8 @@
*/ */
namespace OCA\Files_Sharing\API; namespace OCA\Files_Sharing\API;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController; use OCP\AppFramework\OCSController;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\IGroupManager; use OCP\IGroupManager;
@ -183,7 +185,8 @@ class Share20OCS extends OCSController {
* @NoAdminRequired * @NoAdminRequired
* *
* @param string $id * @param string $id
* @return array * @return DataResponse
* @throws OCSNotFoundException
*/ */
public function getShare($id) { public function getShare($id) {
if (!$this->shareManager->shareApiEnabled()) { if (!$this->shareManager->shareApiEnabled()) {
@ -196,27 +199,19 @@ class Share20OCS extends OCSController {
try { try {
$share = $this->getShareById($id); $share = $this->getShareById($id);
} catch (ShareNotFound $e) { } catch (ShareNotFound $e) {
return [ throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
'statuscode' => 404,
'message' => $this->l->t('Wrong share ID, share doesn\'t exist'),
];
} }
if ($this->canAccessShare($share)) { if ($this->canAccessShare($share)) {
try { try {
$share = $this->formatShare($share); $share = $this->formatShare($share);
return [ return new DataResponse([$share]);
'data' => [$share],
];
} catch (NotFoundException $e) { } catch (NotFoundException $e) {
//Fall trough //Fall trough
} }
} }
return [ throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
'statuscode' => 404,
'message' => $this->l->t('Wrong share ID, share doesn\'t exist')
];
} }
/** /**
@ -226,6 +221,7 @@ class Share20OCS extends OCSController {
* *
* @param string $id * @param string $id
* @return \OC\OCS\Result * @return \OC\OCS\Result
* @throws OCSNotFoundException
*/ */
public function deleteShare($id) { public function deleteShare($id) {
if (!$this->shareManager->shareApiEnabled()) { if (!$this->shareManager->shareApiEnabled()) {
@ -238,40 +234,32 @@ class Share20OCS extends OCSController {
try { try {
$share = $this->getShareById($id); $share = $this->getShareById($id);
} catch (ShareNotFound $e) { } catch (ShareNotFound $e) {
return [ throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
'statuscode' => 404,
'message' => $this->l->t('Wrong share ID, share doesn\'t exist')
];
} }
try { try {
$share->getNode()->lock(ILockingProvider::LOCK_SHARED); $share->getNode()->lock(ILockingProvider::LOCK_SHARED);
} catch (LockedException $e) { } catch (LockedException $e) {
return [ throw new OCSNotFoundException($this->l->t('could not delete share'));
'statuscode' => 404,
'message' => $this->l->t('could not delete share')
];
} }
if (!$this->canAccessShare($share, false)) { if (!$this->canAccessShare($share, false)) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED); $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [ throw new OCSNotFoundException($this->l->t('Could not delete share'));
'statuscode' => 404,
'message' => $this->l->t('Could not delete share')
];
} }
$this->shareManager->deleteShare($share); $this->shareManager->deleteShare($share);
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED); $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return []; return new DataResponse([]);
} }
/** /**
* @NoAdminRequired * @NoAdminRequired
* *
* @return array * @return DataResponse
* @throws OCSNotFoundException
*/ */
public function createShare() { public function createShare() {
$share = $this->shareManager->newShare(); $share = $this->shareManager->newShare();
@ -286,20 +274,14 @@ class Share20OCS extends OCSController {
// Verify path // Verify path
$path = $this->request->getParam('path', null); $path = $this->request->getParam('path', null);
if ($path === null) { if ($path === null) {
return [ throw new OCSNotFoundException($this->l->t('Please specify a file or folder path'));
'statuscode' => 404,
'message' => $this->l->t('Please specify a file or folder path')
];
} }
$userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID());
try { try {
$path = $userFolder->get($path); $path = $userFolder->get($path);
} catch (NotFoundException $e) { } catch (NotFoundException $e) {
return [ throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
'statuscode' => 404,
'messagecode' => $this->l->t('Wrong path, file/folder doesn\'t exist')
];
} }
$share->setNode($path); $share->setNode($path);
@ -307,10 +289,7 @@ class Share20OCS extends OCSController {
try { try {
$share->getNode()->lock(ILockingProvider::LOCK_SHARED); $share->getNode()->lock(ILockingProvider::LOCK_SHARED);
} catch (LockedException $e) { } catch (LockedException $e) {
return [ throw new OCSNotFoundException($this->l->t('Could not create share'));
'statuscode' => 404,
'message' => $this->l->t('Could not create share')
];
} }
// Parse permissions (if available) // Parse permissions (if available)
@ -323,10 +302,7 @@ class Share20OCS extends OCSController {
if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) { if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED); $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [ throw new OCSNotFoundException($this->l->t('invalid permissions'));
'statuscode' => 404,
'message' => 'invalid permissions'
];
} }
// Shares always require read permissions // Shares always require read permissions
@ -354,29 +330,20 @@ class Share20OCS extends OCSController {
// Valid user is required to share // Valid user is required to share
if ($shareWith === null || !$this->userManager->userExists($shareWith)) { if ($shareWith === null || !$this->userManager->userExists($shareWith)) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED); $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [ throw new OCSNotFoundException($this->l->t('Please specify a valid user'));
'statuscode' => 404,
'message' => $this->l->t('Please specify a valid user')
];
} }
$share->setSharedWith($shareWith); $share->setSharedWith($shareWith);
$share->setPermissions($permissions); $share->setPermissions($permissions);
} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
if (!$this->shareManager->allowGroupSharing()) { if (!$this->shareManager->allowGroupSharing()) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED); $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [ throw new OCSNotFoundException($this->l->t('Group sharing is disabled by the administrator'));
'statuscode' => 404,
'message' => $this->l->t('Group sharing is disabled by the administrator')
];
} }
// Valid group is required to share // Valid group is required to share
if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) { if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED); $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [ throw new OCSNotFoundException($this->l->t('Please specify a valid group'));
'statuscode' => 404,
'message' => $this->l->t('Please specify a valid group')
];
} }
$share->setSharedWith($shareWith); $share->setSharedWith($shareWith);
$share->setPermissions($permissions); $share->setPermissions($permissions);
@ -384,10 +351,7 @@ class Share20OCS extends OCSController {
//Can we even share links? //Can we even share links?
if (!$this->shareManager->shareApiAllowLinks()) { if (!$this->shareManager->shareApiAllowLinks()) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED); $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [ throw new OCSNotFoundException($this->l->t('Public link sharing is disabled by the administrator'));
'statuscode' => 404,
'message' => $this->l->t('Public link sharing is disabled by the administrator')
];
} }
/* /*
@ -397,7 +361,7 @@ class Share20OCS extends OCSController {
$existingShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_LINK, $path, false, 1, 0); $existingShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_LINK, $path, false, 1, 0);
if (!empty($existingShares)) { if (!empty($existingShares)) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED); $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return ['data' => $this->formatShare($existingShares[0])]; return new DataResponse($this->formatShare($existingShares[0]));
} }
$publicUpload = $this->request->getParam('publicUpload', null); $publicUpload = $this->request->getParam('publicUpload', null);
@ -414,10 +378,7 @@ class Share20OCS extends OCSController {
// Public upload can only be set for folders // Public upload can only be set for folders
if ($path instanceof \OCP\Files\File) { if ($path instanceof \OCP\Files\File) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED); $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [ throw new OCSNotFoundException($this->l->t('Public upload is only possible for publicly shared folders'));
'statuscode' => 404,
'message' => $this->l->t('Public upload is only possible for publicly shared folders')
];
} }
$share->setPermissions( $share->setPermissions(
@ -446,10 +407,7 @@ class Share20OCS extends OCSController {
$share->setExpirationDate($expireDate); $share->setExpirationDate($expireDate);
} catch (\Exception $e) { } catch (\Exception $e) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED); $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [ throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
'statuscode' => 404,
'message' => $this->l->t('Invalid date, date format must be YYYY-MM-DD')
];
} }
} }
@ -501,7 +459,7 @@ class Share20OCS extends OCSController {
/** /**
* @param \OCP\Files\File|\OCP\Files\Folder $node * @param \OCP\Files\File|\OCP\Files\Folder $node
* @return array * @return DataResponse
*/ */
private function getSharedWithMe($node = null) { private function getSharedWithMe($node = null) {
$userShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $node, -1, 0); $userShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $node, -1, 0);
@ -520,12 +478,12 @@ class Share20OCS extends OCSController {
} }
} }
return ['data' => $formatted]; return new DataResponse($formatted);
} }
/** /**
* @param \OCP\Files\Folder $folder * @param \OCP\Files\Folder $folder
* @return array * @return DataResponse
*/ */
private function getSharesInDir($folder) { private function getSharesInDir($folder) {
if (!($folder instanceof \OCP\Files\Folder)) { if (!($folder instanceof \OCP\Files\Folder)) {
@ -556,7 +514,7 @@ class Share20OCS extends OCSController {
} }
} }
return ['data' => $formatted]; return new DataResponse($formatted);
} }
/** /**
@ -570,7 +528,8 @@ class Share20OCS extends OCSController {
* - Get shares for a specific path (?path=...) * - Get shares for a specific path (?path=...)
* - Get all shares in a folder (?subfiles=true&path=..) * - Get all shares in a folder (?subfiles=true&path=..)
* *
* @return array * @return DataResponse
* @throws OCSNotFoundException
*/ */
public function getShares() { public function getShares() {
if (!$this->shareManager->shareApiEnabled()) { if (!$this->shareManager->shareApiEnabled()) {
@ -588,15 +547,9 @@ class Share20OCS extends OCSController {
$path = $userFolder->get($path); $path = $userFolder->get($path);
$path->lock(ILockingProvider::LOCK_SHARED); $path->lock(ILockingProvider::LOCK_SHARED);
} catch (\OCP\Files\NotFoundException $e) { } catch (\OCP\Files\NotFoundException $e) {
return [ throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
'statuscode' => 404,
'message' => $this->l->t('Wrong path, file/folder doesn\'t exist')
];
} catch (LockedException $e) { } catch (LockedException $e) {
return [ throw new OCSNotFoundException($this->l->t('Could not lock path'));
'statuscode' => 404,
'message' => $this->l->t('Could not lock path')
];
} }
} }
@ -646,14 +599,15 @@ class Share20OCS extends OCSController {
$path->unlock(ILockingProvider::LOCK_SHARED); $path->unlock(ILockingProvider::LOCK_SHARED);
} }
return ['data' => $formatted]; return new DataResponse($formatted);
} }
/** /**
* @NoAdminRequired * @NoAdminRequired
* *
* @param int $id * @param int $id
* @return array * @return DataResponse
* @throws OCSNotFoundException
*/ */
public function updateShare($id) { public function updateShare($id) {
if (!$this->shareManager->shareApiEnabled()) { if (!$this->shareManager->shareApiEnabled()) {
@ -666,20 +620,14 @@ class Share20OCS extends OCSController {
try { try {
$share = $this->getShareById($id); $share = $this->getShareById($id);
} catch (ShareNotFound $e) { } catch (ShareNotFound $e) {
return [ throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
'statuscode' => 404,
'message' => $this->l->t('Wrong share ID, share doesn\'t exist')
];
} }
$share->getNode()->lock(\OCP\Lock\ILockingProvider::LOCK_SHARED); $share->getNode()->lock(\OCP\Lock\ILockingProvider::LOCK_SHARED);
if (!$this->canAccessShare($share, false)) { if (!$this->canAccessShare($share, false)) {
$share->getNode()->unlock(ILockingProvider::LOCK_SHARED); $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [ throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
'statuscode' => 404,
'message' => $this->l->t('Wrong share ID, share doesn\'t exist')
];
} }
$permissions = $this->request->getParam('permissions', null); $permissions = $this->request->getParam('permissions', null);
@ -795,6 +743,7 @@ class Share20OCS extends OCSController {
$incomingShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0); $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)); $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)) { if (!empty($incomingShares)) {
$maxPermissions = 0; $maxPermissions = 0;
foreach ($incomingShares as $incomingShare) { foreach ($incomingShares as $incomingShare) {
@ -824,7 +773,7 @@ class Share20OCS extends OCSController {
$share->getNode()->unlock(\OCP\Lock\ILockingProvider::LOCK_SHARED); $share->getNode()->unlock(\OCP\Lock\ILockingProvider::LOCK_SHARED);
return ['data' => $this->formatShare($share)]; return new DataResponse($this->formatShare($share));
} }
/** /**