/**
  * 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;
 }
 public function testGetSharedByWithLimit()
 {
     $node = $this->getMock('\\OCP\\Files\\File');
     $node->method('getId')->willReturn(42);
     $node->method('getName')->willReturn('myFile');
     $this->tokenHandler->method('generateToken')->willReturn('token');
     $this->notifications->method('sendRemoteShare')->willReturn(true);
     $this->rootFolder->expects($this->never())->method($this->anything());
     $share = $this->shareManager->newShare();
     $share->setSharedWith('*****@*****.**')->setSharedBy('sharedBy')->setShareOwner('shareOwner')->setPermissions(19)->setNode($node);
     $this->provider->create($share);
     $share2 = $this->shareManager->newShare();
     $share2->setSharedWith('*****@*****.**')->setSharedBy('sharedBy')->setShareOwner('shareOwner')->setPermissions(19)->setNode($node);
     $this->provider->create($share2);
     $shares = $this->provider->getSharesBy('shareOwner', \OCP\Share::SHARE_TYPE_REMOTE, null, true, 1, 1);
     $this->assertCount(1, $shares);
     $this->assertEquals('*****@*****.**', $shares[0]->getSharedWith());
 }
 /**
  * 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;
 }
 /**
  * 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;
 }
 public function testGenerateToken()
 {
     $this->secureRandom->expects($this->once())->method('generate')->with($this->expectedTokenLength, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS)->willReturn(true);
     $this->assertTrue($this->tokenHandler->generateToken());
 }