/** * @param string $storage * @param SharedMount[] $mountpoints * @param array|null $arguments * @param \OCP\Files\Storage\IStorageFactory $loader */ public function __construct($storage, array $mountpoints, $arguments = null, $loader = null) { $this->user = $arguments['user']; $this->recipientView = new View('/' . $this->user . '/files'); $this->share = $arguments['newShare']; $newMountPoint = $this->verifyMountPoint($this->share, $mountpoints); $absMountPoint = '/' . $this->user . '/files' . $newMountPoint; $arguments['ownerView'] = new View('/' . $this->share->getShareOwner() . '/files'); parent::__construct($storage, $absMountPoint, $arguments, $loader); }
/** * Update a share * * @param IShare $share * @return IShare The share object */ public function update(IShare $share) { /* * We allow updating the permissions of federated shares */ $qb = $this->dbConnection->getQueryBuilder(); $qb->update('share')->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))->set('permissions', $qb->createNamedParameter($share->getPermissions()))->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))->execute(); return $share; }
/** * @inheritdoc */ public function move(\OCP\Share\IShare $share, $recipient) { if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { // Just update the target $qb = $this->dbConn->getQueryBuilder(); $qb->update('share')->set('file_target', $qb->createNamedParameter($share->getTarget()))->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))->execute(); } else { if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { // Check if there is a usergroup share $qb = $this->dbConn->getQueryBuilder(); $stmt = $qb->select('id')->from('share')->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))->setMaxResults(1)->execute(); $data = $stmt->fetch(); $stmt->closeCursor(); if ($data === false) { // No usergroup share yet. Create one. $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share')->values(['share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP), 'share_with' => $qb->createNamedParameter($recipient), 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()), 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()), 'parent' => $qb->createNamedParameter($share->getId()), 'item_type' => $qb->createNamedParameter($share->getNode() instanceof File ? 'file' : 'folder'), 'item_source' => $qb->createNamedParameter($share->getNode()->getId()), 'file_source' => $qb->createNamedParameter($share->getNode()->getId()), 'file_target' => $qb->createNamedParameter($share->getTarget()), 'permissions' => $qb->createNamedParameter($share->getPermissions()), 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp())])->execute(); } else { // Already a usergroup share. Update it. $qb = $this->dbConn->getQueryBuilder(); $qb->update('share')->set('file_target', $qb->createNamedParameter($share->getTarget()))->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))->execute(); } } } return $share; }
/** * Check for pre share requirements for user shares * * @param \OCP\Share\IShare $share * @throws \Exception */ protected function userCreateChecks(\OCP\Share\IShare $share) { // Check if we can share with group members only if ($this->shareWithGroupMembersOnly()) { $sharedBy = $this->userManager->get($share->getSharedBy()); $sharedWith = $this->userManager->get($share->getSharedWith()); // Verify we can share with this user $groups = array_intersect($this->groupManager->getUserGroupIds($sharedBy), $this->groupManager->getUserGroupIds($sharedWith)); if (empty($groups)) { throw new \Exception('Only sharing with group members is allowed'); } } /* * TODO: Could be costly, fix * * Also this is not what we want in the future.. then we want to squash identical shares. */ $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER); $existingShares = $provider->getSharesByPath($share->getNode()); foreach ($existingShares as $existingShare) { // Ignore if it is the same share if ($existingShare->getFullId() === $share->getFullId()) { continue; } // Identical share already existst if ($existingShare->getSharedWith() === $share->getSharedWith()) { throw new \Exception('Path already shared with this user'); } // The share is already shared with this user via a group share if ($existingShare->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { $group = $this->groupManager->get($existingShare->getSharedWith()); $user = $this->userManager->get($share->getSharedWith()); if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) { throw new \Exception('Path already shared with this user'); } } } }
public function getOwner($path) { return $this->superShare->getShareOwner(); }
/** * Makes sure that the token contains all the information that we need * * @param IShare $share * @param string $token * * @throws CheckException */ private function checkShareIsValid($share, $token) { if ($share->getShareOwner() === null || $share->getTarget() === null) { $message = 'Passed token seems to be valid, but it does not contain all necessary information . ("' . $token . '")'; throw new CheckException($message, Http::STATUS_NOT_FOUND); } }
/** * @param \OCP\Share\IShare $share * @return bool */ protected function canAccessShare(\OCP\Share\IShare $share) { // A file with permissions 0 can't be accessed by us. So Don't show it if ($share->getPermissions() === 0) { return false; } // Owner of the file and the sharer of the file can always get share if ($share->getShareOwner() === $this->currentUser->getUID() || $share->getSharedBy() === $this->currentUser->getUID()) { return true; } // If the share is shared with you (or a group you are a member of) if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() === $this->currentUser->getUID()) { return true; } if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { $sharedWith = $this->groupManager->get($share->getSharedWith()); if ($sharedWith->inGroup($this->currentUser)) { return true; } } return false; }
/** * in case of a re-share we need to send the other use (initiator or owner) * a message that the file was unshared * * @param IShare $share * @param bool $isOwner the user can either be the owner or the user who re-sahred it * @throws ShareNotFound * @throws \OC\HintException */ protected function revokeShare($share, $isOwner) { // also send a unShare request to the initiator, if this is a different user than the owner if ($share->getShareOwner() !== $share->getSharedBy()) { if ($isOwner) { list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy()); } else { list(, $remote) = $this->addressHandler->splitUserRemote($share->getShareOwner()); } $remoteId = $this->getRemoteId($share); $this->notifications->sendRevokeShare($remote, $remoteId, $share->getToken()); } }
/** * @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($share->getShareOwner())->will($this->returnSelf()); $this->rootFolder->method('getRelativePath')->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); } }
/** * Unshare a share from the recipient. If this is a group share * this means we need a special entry in the share db. * * @param \OCP\Share\IShare $share * @param IUser $recipient * @throws BackendError * @throws ProviderException */ public function deleteFromSelf(\OCP\Share\IShare $share, IUser $recipient) { if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { /** @var IGroup $group */ $group = $share->getSharedWith(); if (!$group->inGroup($recipient)) { throw new ProviderException('Recipient not in receiving group'); } // Try to fetch user specific share $qb = $this->dbConn->getQueryBuilder(); $stmt = $qb->select('*')->from('share')->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient->getUID())))->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))->execute(); $data = $stmt->fetch(); /* * Check if there already is a user specific group share. * If there is update it (if required). */ if ($data === false) { $qb = $this->dbConn->getQueryBuilder(); $type = $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder'; //Insert new share $qb->insert('share')->values(['share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP), 'share_with' => $qb->createNamedParameter($recipient->getUID()), 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()->getUID()), 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()->getUID()), 'parent' => $qb->createNamedParameter($share->getId()), 'item_type' => $qb->createNamedParameter($type), 'item_source' => $qb->createNamedParameter($share->getNode()->getId()), 'file_source' => $qb->createNamedParameter($share->getNode()->getId()), 'file_target' => $qb->createNamedParameter($share->getTarget()), 'permissions' => $qb->createNamedParameter(0), 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp())])->execute(); } else { if ($data['permissions'] !== 0) { // Update existing usergroup share $qb = $this->dbConn->getQueryBuilder(); $qb->update('share')->set('permissions', $qb->createNamedParameter(0))->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))->execute(); } } } else { if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { if ($share->getSharedWith() !== $recipient) { throw new ProviderException('Recipient does not match'); } // We can just delete user and link shares $this->delete($share); } else { throw new ProviderException('Invalid shareType'); } } }
/** * 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(); }
/** * check if we are the initiator or the owner of a re-share and return the correct UID * * @param Share\IShare $share * @return string */ protected function getCorrectUid(Share\IShare $share) { if ($this->userManager->userExists($share->getShareOwner())) { return $share->getShareOwner(); } return $share->getSharedBy(); }