Example #1
0
 /**
  * @param string $path
  * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  * @param \OCP\Lock\ILockingProvider $provider
  */
 public function releaseLock($path, $type, ILockingProvider $provider)
 {
     /** @var \OCP\Files\Storage $targetStorage */
     list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
     $targetStorage->releaseLock($targetInternalPath, $type, $provider);
     // unlock the parent folders of the owner when unlocking the share as recipient
     if ($path === '') {
         $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId());
         $this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true);
     }
 }
 /**
  * Share a path
  *
  * @param IShare $share
  * @return IShare The share object
  * @throws ShareNotFound
  * @throws \Exception
  */
 public function create(IShare $share)
 {
     $shareWith = $share->getSharedWith();
     $itemSource = $share->getNodeId();
     $itemType = $share->getNodeType();
     $uidOwner = $share->getShareOwner();
     $permissions = $share->getPermissions();
     $sharedBy = $share->getSharedBy();
     /*
      * Check if file is not already shared with the remote user
      */
     $alreadyShared = $this->getSharedWith($shareWith, self::SHARE_TYPE_REMOTE, $share->getNode(), 1, 0);
     if (!empty($alreadyShared)) {
         $message = 'Sharing %s failed, because this item is already shared with %s';
         $message_t = $this->l->t('Sharing %s failed, because this item is already shared with %s', array($share->getNode()->getName(), $shareWith));
         $this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']);
         throw new \Exception($message_t);
     }
     // don't allow federated shares if source and target server are the same
     list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith);
     $currentServer = $this->addressHandler->generateRemoteURL();
     $currentUser = $sharedBy;
     if ($this->addressHandler->compareAddresses($user, $remote, $currentUser, $currentServer)) {
         $message = 'Not allowed to create a federated share with the same user.';
         $message_t = $this->l->t('Not allowed to create a federated share with the same user');
         $this->logger->debug($message, ['app' => 'Federated File Sharing']);
         throw new \Exception($message_t);
     }
     $token = $this->tokenHandler->generateToken();
     $shareWith = $user . '@' . $remote;
     $shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token);
     $send = $this->notifications->sendRemoteShare($token, $shareWith, $share->getNode()->getName(), $shareId, $share->getSharedBy());
     $data = $this->getRawShare($shareId);
     $share = $this->createShare($data);
     if ($send === false) {
         $this->delete($share);
         $message_t = $this->l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable.', [$share->getNode()->getName(), $shareWith]);
         throw new \Exception($message_t);
     }
     return $share;
 }
Example #3
0
 /**
  * Delete a share
  *
  * @param \OCP\Share\IShare $share
  * @throws ShareNotFound
  * @throws \InvalidArgumentException
  */
 public function deleteShare(\OCP\Share\IShare $share)
 {
     try {
         $share->getFullId();
     } catch (\UnexpectedValueException $e) {
         throw new \InvalidArgumentException('Share does not have a full id');
     }
     $formatHookParams = function (\OCP\Share\IShare $share) {
         // Prepare hook
         $shareType = $share->getShareType();
         $sharedWith = '';
         if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
             $sharedWith = $share->getSharedWith();
         } else {
             if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
                 $sharedWith = $share->getSharedWith();
             } else {
                 if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
                     $sharedWith = $share->getSharedWith();
                 }
             }
         }
         $hookParams = ['id' => $share->getId(), 'itemType' => $share->getNodeType(), 'itemSource' => $share->getNodeId(), 'shareType' => $shareType, 'shareWith' => $sharedWith, 'itemparent' => method_exists($share, 'getParent') ? $share->getParent() : '', 'uidOwner' => $share->getSharedBy(), 'fileSource' => $share->getNodeId(), 'fileTarget' => $share->getTarget()];
         return $hookParams;
     };
     $hookParams = $formatHookParams($share);
     // Emit pre-hook
     \OC_Hook::emit('OCP\\Share', 'pre_unshare', $hookParams);
     // Get all children and delete them as well
     $deletedShares = $this->deleteChildren($share);
     // Do the actual delete
     $provider = $this->factory->getProviderForType($share->getShareType());
     $provider->delete($share);
     // All the deleted shares caused by this delete
     $deletedShares[] = $share;
     //Format hook info
     $formattedDeletedShares = array_map(function ($share) use($formatHookParams) {
         return $formatHookParams($share);
     }, $deletedShares);
     $hookParams['deletedShares'] = $formattedDeletedShares;
     // Emit post hook
     \OC_Hook::emit('OCP\\Share', 'post_unshare', $hookParams);
 }
Example #4
0
 /**
  * @dataProvider dataFormatShare
  *
  * @param array $expects
  * @param \OCP\Share\IShare $share
  * @param array $users
  * @param $exception
  */
 public function testFormatShare(array $expects, \OCP\Share\IShare $share, array $users, $exception)
 {
     $this->userManager->method('get')->will($this->returnValueMap($users));
     $this->urlGenerator->method('linkToRouteAbsolute')->with('files_sharing.sharecontroller.showShare', ['token' => 'myToken'])->willReturn('myLink');
     $this->rootFolder->method('getUserFolder')->with($this->currentUser->getUID())->will($this->returnSelf());
     if (!$exception) {
         $this->rootFolder->method('getById')->with($share->getNodeId())->willReturn([$share->getNode()]);
         $this->rootFolder->method('getRelativePath')->with($share->getNode()->getPath())->will($this->returnArgument(0));
     }
     try {
         $result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
         $this->assertFalse($exception);
         $this->assertEquals($expects, $result);
     } catch (NotFoundException $e) {
         $this->assertTrue($exception);
     }
 }
Example #5
0
 /**
  * Get the file id of the root of the storage
  *
  * @return int
  */
 public function getStorageRootId()
 {
     return $this->share->getNodeId();
 }
Example #6
0
 /**
  * throws hooks when a share is attempted to be accessed
  *
  * @param \OCP\Share\IShare|string $share the Share instance if available,
  * otherwise token
  * @param int $errorCode
  * @param string $errorMessage
  * @throws OC\HintException
  * @throws OC\ServerNotAvailableException
  */
 protected function emitAccessShareHook($share, $errorCode = 200, $errorMessage = '')
 {
     $itemType = $itemSource = $uidOwner = '';
     $token = $share;
     $exception = null;
     if ($share instanceof \OCP\Share\IShare) {
         try {
             $token = $share->getToken();
             $uidOwner = $share->getSharedBy();
             $itemType = $share->getNodeType();
             $itemSource = $share->getNodeId();
         } catch (\Exception $e) {
             // we log what we know and pass on the exception afterwards
             $exception = $e;
         }
     }
     \OC_Hook::emit('OCP\\Share', 'share_link_access', ['itemType' => $itemType, 'itemSource' => $itemSource, 'uidOwner' => $uidOwner, 'token' => $token, 'errorCode' => $errorCode, 'errorMessage' => $errorMessage]);
     if (!is_null($exception)) {
         throw $exception;
     }
 }
Example #7
0
 /**
  * Convert an IShare to an array for OCS output
  *
  * @param \OCP\Share\IShare $share
  * @return array
  * @throws NotFoundException In case the node can't be resolved.
  */
 protected function formatShare(\OCP\Share\IShare $share)
 {
     $sharedBy = $this->userManager->get($share->getSharedBy());
     $shareOwner = $this->userManager->get($share->getShareOwner());
     $result = ['id' => $share->getId(), 'share_type' => $share->getShareType(), 'uid_owner' => $share->getSharedBy(), 'displayname_owner' => $sharedBy !== null ? $sharedBy->getDisplayName() : $share->getSharedBy(), 'permissions' => $share->getPermissions(), 'stime' => $share->getShareTime()->getTimestamp(), 'parent' => null, 'expiration' => null, 'token' => null, 'uid_file_owner' => $share->getShareOwner(), 'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner()];
     $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID());
     $nodes = $userFolder->getById($share->getNodeId());
     if (empty($nodes)) {
         throw new NotFoundException();
     }
     $node = $nodes[0];
     $result['path'] = $userFolder->getRelativePath($node->getPath());
     if ($node instanceof \OCP\Files\Folder) {
         $result['item_type'] = 'folder';
     } else {
         $result['item_type'] = 'file';
     }
     $result['mimetype'] = $node->getMimeType();
     $result['storage_id'] = $node->getStorage()->getId();
     $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
     $result['item_source'] = $node->getId();
     $result['file_source'] = $node->getId();
     $result['file_parent'] = $node->getParent()->getId();
     $result['file_target'] = $share->getTarget();
     if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
         $sharedWith = $this->userManager->get($share->getSharedWith());
         $result['share_with'] = $share->getSharedWith();
         $result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith();
     } else {
         if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
             $result['share_with'] = $share->getSharedWith();
             $result['share_with_displayname'] = $share->getSharedWith();
         } else {
             if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
                 $result['share_with'] = $share->getPassword();
                 $result['share_with_displayname'] = $share->getPassword();
                 $result['token'] = $share->getToken();
                 $result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]);
                 $expiration = $share->getExpirationDate();
                 if ($expiration !== null) {
                     $result['expiration'] = $expiration->format('Y-m-d 00:00:00');
                 }
             } else {
                 if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
                     $result['share_with'] = $share->getSharedWith();
                     $result['share_with_displayname'] = $share->getSharedWith();
                     $result['token'] = $share->getToken();
                 }
             }
         }
     }
     $result['mail_send'] = $share->getMailSend() ? 1 : 0;
     return $result;
 }
 /**
  * create federated share and inform the recipient
  *
  * @param IShare $share
  * @return int
  * @throws ShareNotFound
  * @throws \Exception
  */
 protected function createFederatedShare(IShare $share)
 {
     $token = $this->tokenHandler->generateToken();
     $shareId = $this->addShareToDB($share->getNodeId(), $share->getNodeType(), $share->getSharedWith(), $share->getSharedBy(), $share->getShareOwner(), $share->getPermissions(), $token);
     $sharedByFederatedId = $share->getSharedBy();
     if ($this->userManager->userExists($sharedByFederatedId)) {
         $sharedByFederatedId = $sharedByFederatedId . '@' . $this->addressHandler->generateRemoteURL();
     }
     $send = $this->notifications->sendRemoteShare($token, $share->getSharedWith(), $share->getNode()->getName(), $shareId, $share->getShareOwner(), $share->getShareOwner() . '@' . $this->addressHandler->generateRemoteURL(), $share->getSharedBy(), $sharedByFederatedId);
     if ($send === false) {
         $data = $this->getRawShare($shareId);
         $share = $this->createShareObject($data);
         $this->removeShareFromTable($share);
         $message_t = $this->l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable.', [$share->getNode()->getName(), $share->getSharedWith()]);
         throw new \Exception($message_t);
     }
     return $shareId;
 }
Example #9
0
 /**
  * Creates the environment based on the share the token links to
  *
  * @param IShare $share
  */
 public function setTokenBasedEnv($share)
 {
     $origShareOwnerId = $share->getShareOwner();
     $this->userFolder = $this->rootFolder->getUserFolder($origShareOwnerId);
     $this->sharedNodeId = $share->getNodeId();
     $this->sharedNode = $share->getNode();
     $this->fromRootToFolder = $this->buildFromRootToFolder($this->sharedNodeId);
     $this->folderName = $share->getTarget();
     $this->userId = $origShareOwnerId;
     $this->sharePassword = $share->getPassword();
 }
 /**
  * create federated share and inform the recipient
  *
  * @param IShare $share
  * @return int
  * @throws ShareNotFound
  * @throws \Exception
  */
 protected function createFederatedShare(IShare $share)
 {
     $token = $this->tokenHandler->generateToken();
     $shareId = $this->addShareToDB($share->getNodeId(), $share->getNodeType(), $share->getSharedWith(), $share->getSharedBy(), $share->getShareOwner(), $share->getPermissions(), $token);
     try {
         $sharedByFederatedId = $share->getSharedBy();
         if ($this->userManager->userExists($sharedByFederatedId)) {
             $sharedByFederatedId = $sharedByFederatedId . '@' . $this->addressHandler->generateRemoteURL();
         }
         $send = $this->notifications->sendRemoteShare($token, $share->getSharedWith(), $share->getNode()->getName(), $shareId, $share->getShareOwner(), $share->getShareOwner() . '@' . $this->addressHandler->generateRemoteURL(), $share->getSharedBy(), $sharedByFederatedId);
         if ($send === false) {
             $message_t = $this->l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable.', [$share->getNode()->getName(), $share->getSharedWith()]);
             throw new \Exception($message_t);
         }
     } catch (\Exception $e) {
         $this->logger->error('Failed to notify remote server of federated share, removing share (' . $e->getMessage() . ')');
         $this->removeShareFromTableById($shareId);
         throw $e;
     }
     return $shareId;
 }