private $currentUser;
/** @var IL10N */
private $l;
+ /** @var \OCP\Files\Node */
+ private $lockedNode;
/**
* Share20OCS constructor.
* @throws OCSNotFoundException
*/
public function getShare($id) {
- if (!$this->shareManager->shareApiEnabled()) {
- return [
- 'statuscode' => 404,
- 'message' => $this->l->t('Share API is disabled')
- ];
- }
-
try {
$share = $this->getShareById($id);
} catch (ShareNotFound $e) {
* @NoAdminRequired
*
* @param string $id
- * @return \OC\OCS\Result
+ * @return DataResponse
* @throws OCSNotFoundException
*/
public function deleteShare($id) {
- if (!$this->shareManager->shareApiEnabled()) {
- return [
- 'statuscode' => 404,
- 'message' => $this->l->t('Share API is disabled')
- ];
- }
-
try {
$share = $this->getShareById($id);
} catch (ShareNotFound $e) {
}
try {
- $share->getNode()->lock(ILockingProvider::LOCK_SHARED);
+ $this->lock($share->getNode());
} catch (LockedException $e) {
throw new OCSNotFoundException($this->l->t('could not delete share'));
}
if (!$this->canAccessShare($share, false)) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
throw new OCSNotFoundException($this->l->t('Could not delete share'));
}
$this->shareManager->deleteShare($share);
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
-
return new DataResponse([]);
}
public function createShare() {
$share = $this->shareManager->newShare();
- if (!$this->shareManager->shareApiEnabled()) {
- return [
- 'statuscode' => 404,
- 'message' => $this->l->t('Share API is disabled')
- ];
- }
-
// Verify path
$path = $this->request->getParam('path', null);
if ($path === null) {
$share->setNode($path);
try {
- $share->getNode()->lock(ILockingProvider::LOCK_SHARED);
+ $this->lock($share->getNode());
} catch (LockedException $e) {
throw new OCSNotFoundException($this->l->t('Could not create share'));
}
}
if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
throw new OCSNotFoundException($this->l->t('invalid permissions'));
}
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);
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);
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);
throw new OCSNotFoundException($this->l->t('Please specify a valid group'));
}
$share->setSharedWith($shareWith);
} else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
//Can we even share links?
if (!$this->shareManager->shareApiAllowLinks()) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
throw new OCSNotFoundException($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 DataResponse($this->formatShare($existingShares[0]));
}
if ($publicUpload === 'true') {
// Check if public upload is allowed
if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
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);
throw new OCSNotFoundException($this->l->t('Public upload is only possible for publicly shared folders'));
}
$expireDate = $this->parseDate($expireDate);
$share->setExpirationDate($expireDate);
} catch (\Exception $e) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
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 [
'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 [
'statuscode' => 400,
$this->l->t('Unknown share type')
$share = $this->shareManager->createShare($share);
} catch (GenericShareException $e) {
$code = $e->getCode() === 0 ? 403 : $e->getCode();
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [
'statuscode' => $code,
'message' => $e->getHint()
];
}catch (\Exception $e) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [
'statuscode' => 403,
'message' => $e->getMessage()
$output = $this->formatShare($share);
- $share->getNode()->unlock(\OCP\Lock\ILockingProvider::LOCK_SHARED);
-
return ['data' => $output];
}
* @throws OCSNotFoundException
*/
public function getShares() {
- if (!$this->shareManager->shareApiEnabled()) {
- return [];
- }
-
$sharedWithMe = $this->request->getParam('shared_with_me', null);
$reshares = $this->request->getParam('reshares', null);
$subfiles = $this->request->getParam('subfiles');
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;
}
}
}
- if ($path !== null) {
- $path->unlock(ILockingProvider::LOCK_SHARED);
- }
-
return new DataResponse($formatted);
}
* @throws OCSNotFoundException
*/
public function updateShare($id) {
- if (!$this->shareManager->shareApiEnabled()) {
- return [
- 'statuscode' => 404,
- 'message' => $this->l->t('Share API is disabled')
- ];
- }
-
try {
$share = $this->getShareById($id);
} catch (ShareNotFound $e) {
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);
throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
}
*/
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
if ($permissions === null && $password === null && $publicUpload === null && $expireDate === null) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [
'statuscode' => 400,
'message' => 'Wrong or no update parameter given'
\OCP\Constants::PERMISSION_CREATE, // hidden file list
])
) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [
'statuscode' => 400,
'message' => $this->l->t('Can\'t change permissions for public share links')
$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 [
'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 [
'statuscode' => 400,
'message' => $this->l->t('Public upload is only possible for publicly shared folders')
try {
$expireDate = $this->parseDate($expireDate);
} catch (\Exception $e) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [
'statuscode' => 400,
'message' => $e->getMessage()
} else {
// For other shares only permissions is valid.
if ($permissions === null) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [
'statuscode' => 400,
'message' => $this->l->t('Wrong or no update parameter given')
}
if ($share->getPermissions() & ~$maxPermissions) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [
'statuscode' => 404,
'message' => $this->l->t('Cannot increase permissions')
try {
$share = $this->shareManager->updateShare($share);
} catch (\Exception $e) {
- $share->getNode()->unlock(ILockingProvider::LOCK_SHARED);
return [
'statuscode' => 400,
'message' => $e->getMessage()
];
}
- $share->getNode()->unlock(\OCP\Lock\ILockingProvider::LOCK_SHARED);
-
return new DataResponse($this->formatShare($share));
}
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);
+ }
+ }
}