Skip to content

Commit

Permalink
Merge pull request #43791 from nextcloud/share-api-cleanup
Browse files Browse the repository at this point in the history
Share api cleanup
  • Loading branch information
icewind1991 authored Mar 4, 2024
2 parents 7d6f797 + 96942e4 commit 1ae1596
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 500 deletions.
8 changes: 4 additions & 4 deletions apps/files_sharing/tests/UpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public function testDeleteParentFolder() {
// check if user2 can see the shared folder
$this->assertTrue($view->file_exists($this->folder));

$foldersShared = \OC\Share\Share::getItemsSharedWith('folder');
$this->assertSame(1, count($foldersShared));
$foldersShared = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, IShare::TYPE_USER);
$this->assertCount(1, $foldersShared);

$view->mkdir('localFolder');
$view->file_put_contents('localFolder/localFile.txt', 'local file');
Expand All @@ -116,8 +116,8 @@ public function testDeleteParentFolder() {
$this->loginHelper(self::TEST_FILES_SHARING_API_USER2);

// shared folder should be unshared
$foldersShared = \OC\Share\Share::getItemsSharedWith('folder');
$this->assertTrue(empty($foldersShared));
$foldersShared = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, IShare::TYPE_USER);
$this->assertCount(0, $foldersShared);

// trashbin should contain the local file but not the mount point
$rootView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
Expand Down
53 changes: 29 additions & 24 deletions lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
use OCP\Files\ReservedWordException;
use OCP\Files\Storage\IStorage;
use OCP\IUser;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use OCP\Server;
use OCP\Share\IManager;
use OCP\Share\IShare;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -731,6 +733,8 @@ public function deleteAll($directory) {
public function rename($source, $target) {
$absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source));
$absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target));
$targetParts = explode('/', $absolutePath2);
$targetUser = $targetParts[1] ?? null;
$result = false;
if (
Filesystem::isValidPath($target)
Expand Down Expand Up @@ -785,7 +789,7 @@ public function rename($source, $target) {
if ($internalPath1 === '') {
if ($mount1 instanceof MoveableMount) {
$sourceParentMount = $this->getMount(dirname($source));
if ($sourceParentMount === $mount2 && $this->targetIsNotShared($storage2, $internalPath2)) {
if ($sourceParentMount === $mount2 && $this->targetIsNotShared($targetUser, $absolutePath2)) {
/**
* @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1
*/
Expand Down Expand Up @@ -1781,28 +1785,29 @@ private function assertPathLength($path): void {
* It is not allowed to move a mount point into a different mount point or
* into an already shared folder
*/
private function targetIsNotShared(IStorage $targetStorage, string $targetInternalPath): bool {
// note: cannot use the view because the target is already locked
$fileId = $targetStorage->getCache()->getId($targetInternalPath);
if ($fileId === -1) {
// target might not exist, need to check parent instead
$fileId = $targetStorage->getCache()->getId(dirname($targetInternalPath));
}

// check if any of the parents were shared by the current owner (include collections)
$shares = Share::getItemShared(
'folder',
(string)$fileId,
\OC\Share\Constants::FORMAT_NONE,
null,
true
);

if (count($shares) > 0) {
$this->logger->debug(
'It is not allowed to move one mount point into a shared folder',
['app' => 'files']);
return false;
private function targetIsNotShared(string $user, string $targetPath): bool {
$providers = [
IShare::TYPE_USER,
IShare::TYPE_GROUP,
IShare::TYPE_EMAIL,
IShare::TYPE_CIRCLE,
IShare::TYPE_ROOM,
IShare::TYPE_DECK,
IShare::TYPE_SCIENCEMESH
];
$shareManager = Server::get(IManager::class);
/** @var IShare[] $shares */
$shares = array_merge(...array_map(function (int $type) use ($shareManager, $user) {
return $shareManager->getSharesBy($user, $type);
}, $providers));

foreach ($shares as $share) {
if (str_starts_with($targetPath, $share->getNode()->getPath())) {
$this->logger->debug(
'It is not allowed to move one mount point into a shared folder',
['app' => 'files']);
return false;
}
}

return true;
Expand Down
Loading

0 comments on commit 1ae1596

Please sign in to comment.