*/
namespace OCA\Files_Sharing\API;
+use OCP\AppFramework\OCSController;
use OCP\Files\NotFoundException;
use OCP\IGroupManager;
use OCP\IL10N;
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;
*
* @package OCA\Files_Sharing\API
*/
-class Share20OCS {
+class Share20OCS extends OCSController {
/** @var IManager */
private $shareManager;
/** @var IUserManager */
private $userManager;
/** @var IRequest */
- private $request;
+ protected $request;
/** @var IRootFolder */
private $rootFolder;
/** @var IURLGenerator */
/**
* 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;
} 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();
/**
* 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);
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)
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
// 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);
//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')
+ ];
}
/*
$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);
// 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(
$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);
} 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);
}
}
- 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();
}
}
- 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);
$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')
+ ];
}
}
$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);
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;
])
) {
$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 (
) {
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
$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);
}
// 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);
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')
+ ];
}
}
}
$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)];
}
/**