/** * create unique target * @param string $filePath * @param string $shareWith * @param string $exclude * @return string */ public function generateTarget($filePath, $shareWith, $exclude = null) { $shareFolder = \OCA\Files_Sharing\Helper::getShareFolder(); $target = \OC\Files\Filesystem::normalizePath($shareFolder . '/' . basename($filePath)); // for group shares we return the target right away if ($shareWith === false) { return $target; } \OC\Files\Filesystem::initMountPoints($shareWith); $view = new \OC\Files\View('/' . $shareWith . '/files'); if (!$view->is_dir($shareFolder)) { $dir = ''; $subdirs = explode('/', $shareFolder); foreach ($subdirs as $subdir) { $dir = $dir . '/' . $subdir; if (!$view->is_dir($dir)) { $view->mkdir($dir); } } } $excludeList = \OCP\Share::getItemsSharedWithUser('file', $shareWith, self::FORMAT_TARGET_NAMES); if (is_array($exclude)) { $excludeList = array_merge($excludeList, $exclude); } return \OCA\Files_Sharing\Helper::generateUniqueTarget($target, $excludeList, $view); }
public function generateTarget($itemSource, $shareWith, $exclude = null) { // Always make target be test.txt to cause conflicts if (substr($itemSource, 0, strlen('test')) !== 'test') { $target = "test.txt"; } else { $target = $itemSource; } $shares = \OCP\Share::getItemsSharedWithUser('test', $shareWith); $knownTargets = array(); foreach ($shares as $share) { $knownTargets[] = $share['item_target']; } if (in_array($target, $knownTargets)) { $pos = strrpos($target, '.'); $name = substr($target, 0, $pos); $ext = substr($target, $pos); $append = ''; $i = 1; while (in_array($name . $append . $ext, $knownTargets)) { $append = $i; $i++; } $target = $name . $append . $ext; } return $target; }
/** * Get all mountpoints applicable for the user and check for shares where we need to update the etags * * @param \OCP\IUser $user * @param \OCP\Files\Storage\IStorageFactory $storageFactory * @return \OCP\Files\Mount\IMountPoint[] */ public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) { $shares = \OCP\Share::getItemsSharedWithUser('file', $user->getUID()); $shares = array_filter($shares, function ($share) { return $share['permissions'] > 0; }); $shares = array_map(function ($share) use($user, $storageFactory) { return new SharedMount('\\OC\\Files\\Storage\\Shared', '/' . $user->getUID() . '/' . $share['file_target'], array('share' => $share, 'user' => $user->getUID()), $storageFactory); }, $shares); // array_filter removes the null values from the array return array_filter($shares); }
/** * create unique target * @param string $filePath * @param string $shareWith * @param string $exclude * @return string */ public function generateTarget($filePath, $shareWith, $exclude = null) { $target = '/' . basename($filePath); // for group shares we return the target right away if ($shareWith === false) { return $target; } \OC\Files\Filesystem::initMountPoints($shareWith); $view = new \OC\Files\View('/' . $shareWith . '/files'); $excludeList = \OCP\Share::getItemsSharedWithUser('file', $shareWith, self::FORMAT_TARGET_NAMES); if (is_array($exclude)) { $excludeList = array_merge($excludeList, $exclude); } return \OCA\Files_Sharing\Helper::generateUniqueTarget($target, $excludeList, $view); }
/** * Get all mountpoints applicable for the user and check for shares where we need to update the etags * * @param \OCP\IUser $user * @param \OCP\Files\Storage\IStorageFactory $storageFactory * @return \OCP\Files\Mount\IMountPoint[] */ public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) { $shares = \OCP\Share::getItemsSharedWithUser('file', $user->getUID()); $propagator = $this->propagationManager->getSharePropagator($user->getUID()); $propagator->propagateDirtyMountPoints($shares); $shares = array_filter($shares, function ($share) { return $share['permissions'] > 0; }); $shares = array_map(function ($share) use($user, $storageFactory) { // for updating etags for the share owner when we make changes to this share. $ownerPropagator = $this->propagationManager->getChangePropagator($share['uid_owner']); return new SharedMount('\\OC\\Files\\Storage\\Shared', '/' . $user->getUID() . '/' . $share['file_target'], array('propagationManager' => $this->propagationManager, 'propagator' => $ownerPropagator, 'share' => $share, 'user' => $user->getUID()), $storageFactory); }, $shares); // array_filter removes the null values from the array return array_filter($shares); }
/** * Get all mountpoints applicable for the user and check for shares where we need to update the etags * * @param \OCP\IUser $user * @param \OCP\Files\Storage\IStorageFactory $storageFactory * @return \OCP\Files\Mount\IMountPoint[] */ public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) { $shares = \OCP\Share::getItemsSharedWithUser('file', $user->getUID()); $propagator = $this->propagationManager->getSharePropagator($user->getUID()); $propagator->propagateDirtyMountPoints($shares); $shares = array_filter($shares, function ($share) { return $share['permissions'] > 0; }); return array_map(function ($share) use($user, $storageFactory) { Filesystem::initMountPoints($share['uid_owner']); // for updating etags for the share owner when we make changes to this share. $ownerPropagator = $this->propagationManager->getChangePropagator($share['uid_owner']); // for updating our etags when changes are made to the share from the owners side (probably indirectly by us trough another share) $this->propagationManager->listenToOwnerChanges($share['uid_owner'], $user->getUID()); return new SharedMount('\\OC\\Files\\Storage\\Shared', '/' . $user->getUID() . '/' . $share['file_target'], array('propagator' => $ownerPropagator, 'share' => $share, 'user' => $user->getUID()), $storageFactory); }, $shares); }
/** * Generate a unique target for the item * @param string $itemType * @param string $itemSource * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK * @param string $shareWith User or group the item is being shared with * @param string $uidOwner User that is the owner of shared item * @param string $suggestedTarget The suggested target originating from a reshare (optional) * @param int $groupParent The id of the parent group share (optional) * @throws \Exception * @return string Item target */ public static function generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $suggestedTarget = null, $groupParent = null) { // FIXME: $uidOwner and $groupParent seems to be unused $backend = \OC\Share\Share::getBackend($itemType); if ($shareType == self::SHARE_TYPE_LINK) { if (isset($suggestedTarget)) { return $suggestedTarget; } return $backend->generateTarget($itemSource, false); } else { if ($itemType == 'file' || $itemType == 'folder') { $column = 'file_target'; $columnSource = 'file_source'; } else { $column = 'item_target'; $columnSource = 'item_source'; } if ($shareType == self::SHARE_TYPE_USER) { // Share with is a user, so set share type to user and groups $shareType = self::$shareTypeUserAndGroups; } $exclude = array(); $result = \OCP\Share::getItemsSharedWithUser($itemType, $shareWith); foreach ($result as $row) { if ($row['permissions'] > 0) { $exclude[] = $row[$column]; } } // Check if suggested target exists first if (!isset($suggestedTarget)) { $suggestedTarget = $itemSource; } if ($shareType == self::SHARE_TYPE_GROUP) { $target = $backend->generateTarget($suggestedTarget, false, $exclude); } else { $target = $backend->generateTarget($suggestedTarget, $shareWith, $exclude); } return $target; } }
/** * Get all mountpoints applicable for the user and check for shares where we need to update the etags * * @param \OCP\IUser $user * @param \OCP\Files\Storage\IStorageFactory $storageFactory * @return \OCP\Files\Mount\IMountPoint[] */ public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) { $shares = \OCP\Share::getItemsSharedWithUser('file', $user->getUID()); $propagator = $this->propagationManager->getSharePropagator($user->getUID()); $propagator->propagateDirtyMountPoints($shares); $shares = array_filter($shares, function ($share) { return $share['permissions'] > 0; }); $shares = array_map(function ($share) use($user, $storageFactory) { try { Filesystem::initMountPoints($share['uid_owner']); } catch (NoUserException $e) { \OC::$server->getLogger()->warning('The user \'' . $share['uid_owner'] . '\' of share with ID \'' . $share['id'] . '\' can\'t be retrieved.', array('app' => 'files_sharing')); return null; } // for updating etags for the share owner when we make changes to this share. $ownerPropagator = $this->propagationManager->getChangePropagator($share['uid_owner']); // for updating our etags when changes are made to the share from the owners side (probably indirectly by us trough another share) $this->propagationManager->listenToOwnerChanges($share['uid_owner'], $user->getUID()); return new SharedMount('\\OC\\Files\\Storage\\Shared', '/' . $user->getUID() . '/' . $share['file_target'], array('propagator' => $ownerPropagator, 'share' => $share, 'user' => $user->getUID()), $storageFactory); }, $shares); // array_filter removes the null values from the array return array_filter($shares); }
public function testGetShareSubItemsWhenUserNotInGroup() { \OCP\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_GROUP, $this->group1, \OCP\Constants::PERMISSION_READ); $result = \OCP\Share::getItemsSharedWithUser('test', $this->user2); $this->assertCount(1, $result); $groupShareId = array_keys($result)[0]; // remove user from group $userObject = \OC::$server->getUserManager()->get($this->user2); \OC::$server->getGroupManager()->get($this->group1)->removeUser($userObject); $result = \OCP\Share::getItemsSharedWithUser('test', $this->user2); $this->assertCount(0, $result); // test with buggy data $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); $qb->insert('share')->values(['share_type' => $qb->expr()->literal(2), 'share_with' => $qb->expr()->literal($this->user2), 'parent' => $qb->expr()->literal($groupShareId), 'uid_owner' => $qb->expr()->literal($this->user1), 'item_type' => $qb->expr()->literal('test'), 'item_source' => $qb->expr()->literal('test.txt'), 'item_target' => $qb->expr()->literal('test.txt'), 'file_target' => $qb->expr()->literal('test2.txt'), 'permissions' => $qb->expr()->literal(1), 'stime' => $qb->expr()->literal(time())])->execute(); $result = \OCP\Share::getItemsSharedWithUser('test', $this->user2); $this->assertCount(0, $result); $qb->delete('share')->execute(); }
public static function setup($options) { $shares = \OCP\Share::getItemsSharedWithUser('file', $options['user']); $manager = Filesystem::getMountManager(); $loader = Filesystem::getLoader(); if (!\OCP\User::isLoggedIn() || \OCP\User::getUser() != $options['user'] || $shares) { foreach ($shares as $share) { // don't mount shares where we have no permissions if ($share['permissions'] > 0) { $mount = new SharedMount('\\OC\\Files\\Storage\\Shared', $options['user_dir'] . '/' . $share['file_target'], array('share' => $share, 'user' => $options['user']), $loader); $manager->addMount($mount); } } } }
/** * {@inheritDoc} */ public function listAll($userId) { return array_merge(Share::getItemsSharedWithUser('calendar', $userId, ShareTypes::ENTITYLIST), Share::getItemsShared('object', ShareTypes::GROUPEDLIST)); }
private function getUserShares($targetUserId) { return \OCP\Share::getItemsSharedWithUser('file', $targetUserId); }