Example #1
0
 /**
  * Validate if the expiration date fits the system settings
  *
  * @param \OCP\Share\IShare $share The share to validate the expiration date of
  * @return \OCP\Share\IShare The expiration date or null if $expireDate was null and it is not required
  * @throws GenericShareException
  * @throws \InvalidArgumentException
  * @throws \Exception
  */
 protected function validateExpirationDate(\OCP\Share\IShare $share)
 {
     $expirationDate = $share->getExpirationDate();
     if ($expirationDate !== null) {
         //Make sure the expiration date is a date
         $expirationDate->setTime(0, 0, 0);
         $date = new \DateTime();
         $date->setTime(0, 0, 0);
         if ($date >= $expirationDate) {
             $message = $this->l->t('Expiration date is in the past');
             throw new GenericShareException($message, $message, 404);
         }
     }
     // If we enforce the expiration date check that is does not exceed
     if ($this->shareApiLinkDefaultExpireDateEnforced()) {
         if ($expirationDate === null) {
             throw new \InvalidArgumentException('Expiration date is enforced');
         }
         $date = new \DateTime();
         $date->setTime(0, 0, 0);
         $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
         if ($date < $expirationDate) {
             $message = $this->l->t('Cannot set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]);
             throw new GenericShareException($message, $message, 404);
         }
     }
     // If expiredate is empty set a default one if there is a default
     if ($expirationDate === null && $this->shareApiLinkDefaultExpireDate()) {
         $expirationDate = new \DateTime();
         $expirationDate->setTime(0, 0, 0);
         $expirationDate->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
     }
     $accepted = true;
     $message = '';
     \OCP\Util::emitHook('\\OC\\Share', 'verifyExpirationDate', ['expirationDate' => &$expirationDate, 'accepted' => &$accepted, 'message' => &$message, 'passwordSet' => $share->getPassword() === null]);
     if (!$accepted) {
         throw new \Exception($message);
     }
     $share->setExpirationDate($expirationDate);
     return $expirationDate;
 }
Example #2
0
 /**
  * Update a share
  *
  * @param \OCP\Share\IShare $share
  * @return \OCP\Share\IShare The share object
  */
 public function update(\OCP\Share\IShare $share)
 {
     if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
         /*
          * We allow updating the recipient on user shares.
          */
         $qb = $this->dbConn->getQueryBuilder();
         $qb->update('share')->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))->set('share_with', $qb->createNamedParameter($share->getSharedWith()))->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))->set('permissions', $qb->createNamedParameter($share->getPermissions()))->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))->execute();
     } else {
         if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
             $qb = $this->dbConn->getQueryBuilder();
             $qb->update('share')->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))->set('permissions', $qb->createNamedParameter($share->getPermissions()))->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))->execute();
             /*
              * Update all user defined group shares
              */
             $qb = $this->dbConn->getQueryBuilder();
             $qb->update('share')->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))->execute();
             /*
              * Now update the permissions for all children that have not set it to 0
              */
             $qb = $this->dbConn->getQueryBuilder();
             $qb->update('share')->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))->set('permissions', $qb->createNamedParameter($share->getPermissions()))->execute();
         } else {
             if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
                 $qb = $this->dbConn->getQueryBuilder();
                 $qb->update('share')->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))->set('share_with', $qb->createNamedParameter($share->getPassword()))->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))->set('permissions', $qb->createNamedParameter($share->getPermissions()))->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))->set('token', $qb->createNamedParameter($share->getToken()))->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))->execute();
             }
         }
     }
     return $share;
 }
Example #3
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()];
     $node = $share->getNode();
     $result['path'] = $this->rootFolder->getUserFolder($share->getShareOwner())->getRelativePath($node->getPath());
     if ($node instanceof \OCP\Files\Folder) {
         $result['item_type'] = 'folder';
     } else {
         $result['item_type'] = 'file';
     }
     $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;
 }