Пример #1
0
 /**
  * create re-share on behalf of another user
  *
  * @param $params
  * @return \OC_OCS_Result
  */
 public function reShare($params)
 {
     $id = isset($params['id']) ? (int) $params['id'] : null;
     $token = $this->request->getParam('token', null);
     $shareWith = $this->request->getParam('shareWith', null);
     $permission = (int) $this->request->getParam('permission', null);
     $remoteId = (int) $this->request->getParam('remoteId', null);
     if ($id === null || $token === null || $shareWith === null || $permission === null || $remoteId === null) {
         return new \OC_OCS_Result(null, Http::STATUS_BAD_REQUEST);
     }
     try {
         $share = $this->federatedShareProvider->getShareById($id);
     } catch (Share\Exceptions\ShareNotFound $e) {
         return new \OC_OCS_Result(null, Http::STATUS_NOT_FOUND);
     }
     // don't allow to share a file back to the owner
     list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith);
     $owner = $share->getShareOwner();
     $currentServer = $this->addressHandler->generateRemoteURL();
     if ($this->addressHandler->compareAddresses($user, $remote, $owner, $currentServer)) {
         return new \OC_OCS_Result(null, Http::STATUS_FORBIDDEN);
     }
     if ($this->verifyShare($share, $token)) {
         // check if re-sharing is allowed
         if ($share->getPermissions() | ~Constants::PERMISSION_SHARE) {
             $share->setPermissions($share->getPermissions() & $permission);
             // the recipient of the initial share is now the initiator for the re-share
             $share->setSharedBy($share->getSharedWith());
             $share->setSharedWith($shareWith);
             try {
                 $result = $this->federatedShareProvider->create($share);
                 $this->federatedShareProvider->storeRemoteId((int) $result->getId(), $remoteId);
                 return new \OC_OCS_Result(['token' => $result->getToken(), 'remoteId' => $result->getId()]);
             } catch (\Exception $e) {
                 return new \OC_OCS_Result(null, Http::STATUS_BAD_REQUEST);
             }
         } else {
             return new \OC_OCS_Result(null, Http::STATUS_FORBIDDEN);
         }
     }
     return new \OC_OCS_Result(null, Http::STATUS_BAD_REQUEST);
 }
Пример #2
0
 /**
  * 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;
 }
Пример #3
0
 /**
  * @dataProvider dataTestCompareAddresses
  *
  * @param string $user1
  * @param string $server1
  * @param string $user2
  * @param string $server2
  * @param bool $expected
  */
 public function testCompareAddresses($user1, $server1, $user2, $server2, $expected)
 {
     $this->assertSame($expected, $this->addressHandler->compareAddresses($user1, $server1, $user2, $server2));
 }
 /**
  * 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();
     $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);
     }
     $share->setSharedWith($user . '@' . $remote);
     try {
         $remoteShare = $this->getShareFromExternalShareTable($share);
     } catch (ShareNotFound $e) {
         $remoteShare = null;
     }
     if ($remoteShare) {
         try {
             $uidOwner = $remoteShare['owner'] . '@' . $remoteShare['remote'];
             $shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, 'tmp_token_' . time());
             $share->setId($shareId);
             list($token, $remoteId) = $this->askOwnerToReShare($shareWith, $share, $shareId);
             // remote share was create successfully if we get a valid token as return
             $send = is_string($token) && $token !== '';
         } catch (\Exception $e) {
             // fall back to old re-share behavior if the remote server
             // doesn't support flat re-shares (was introduced with ownCloud 9.1)
             $this->removeShareFromTable($share);
             $shareId = $this->createFederatedShare($share);
         }
         if ($send) {
             $this->updateSuccessfulReshare($shareId, $token);
             $this->storeRemoteId($shareId, $remoteId);
         } else {
             $this->removeShareFromTable($share);
             $message_t = $this->l->t('File is already shared with %s', [$shareWith]);
             throw new \Exception($message_t);
         }
     } else {
         $shareId = $this->createFederatedShare($share);
     }
     $data = $this->getRawShare($shareId);
     return $this->createShareObject($data);
 }