Skip to content

Commit 10f4199

Browse files
author
Vincent Petry
committed
Port isTargetAllowed to share 2.0
1 parent 3d4d0e7 commit 10f4199

File tree

3 files changed

+77
-52
lines changed

3 files changed

+77
-52
lines changed

apps/files_sharing/lib/SharedMount.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OC\Files\Mount\MountPoint;
3131
use OC\Files\Mount\MoveableMount;
3232
use OC\Files\View;
33+
use OCP\Share\IShare;
3334

3435
/**
3536
* Shared mount points can be moved by the user
@@ -178,13 +179,67 @@ protected function stripUserFilesPath($path) {
178179
return '/' . $relPath;
179180
}
180181

182+
/**
183+
* check if it is allowed to move a mount point to a given target.
184+
* It is not allowed to move a mount point into a different mount point or
185+
* into an already shared folder
186+
*
187+
* @param string $target path
188+
* @return boolean
189+
*/
190+
private function isTargetAllowed($target) {
191+
list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target);
192+
if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) {
193+
\OCP\Util::writeLog('files',
194+
'It is not allowed to move one mount point into another one',
195+
\OCP\Util::DEBUG);
196+
return false;
197+
}
198+
199+
// note: cannot use the view because the target is already locked
200+
$fileId = (int)$targetStorage->getCache()->getId($targetInternalPath);
201+
if ($fileId === -1) {
202+
// target might not exist, need to check parent instead
203+
$fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath));
204+
}
205+
206+
$targetNodes = \OC::$server->getRootFolder()->getById($fileId);
207+
if (empty($targetNodes)) {
208+
return false;
209+
}
210+
211+
$shareManager = \OC::$server->getShareManager();
212+
$targetNode = $targetNodes[0];
213+
// FIXME: make it stop earlier in '/$userId/files'
214+
while (!is_null($targetNode) && $targetNode->getPath() !== '/') {
215+
$shares = $shareManager->getSharesByPath($targetNode);
216+
217+
foreach ($shares as $share) {
218+
if ($this->user === $share->getShareOwner()) {
219+
\OCP\Util::writeLog('files',
220+
'It is not allowed to move one mount point into a shared folder',
221+
\OCP\Util::DEBUG);
222+
return false;
223+
}
224+
}
225+
226+
$targetNode = $targetNode->getParent();
227+
}
228+
229+
return true;
230+
}
231+
232+
181233
/**
182234
* Move the mount point to $target
183235
*
184236
* @param string $target the target mount point
185237
* @return bool
186238
*/
187239
public function moveMount($target) {
240+
if (!$this->isTargetAllowed($target)) {
241+
return false;
242+
}
188243

189244
$relTargetPath = $this->stripUserFilesPath($target);
190245
$share = $this->storage->getShare();

lib/private/Files/View.php

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -767,15 +767,11 @@ public function rename($path1, $path2) {
767767
$this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true);
768768

769769
if ($internalPath1 === '' and $mount1 instanceof MoveableMount) {
770-
if ($this->isTargetAllowed($absolutePath2)) {
771-
/**
772-
* @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1
773-
*/
774-
$sourceMountPoint = $mount1->getMountPoint();
775-
$result = $mount1->moveMount($absolutePath2);
770+
$sourceMountPoint = $mount1->getMountPoint();
771+
// can fail in case it's not allowed
772+
$result = $mount1->moveMount($absolutePath2);
773+
if ($result) {
776774
$manager->moveMount($sourceMountPoint, $mount1->getMountPoint());
777-
} else {
778-
$result = false;
779775
}
780776
// moving a file/folder within the same mount point
781777
} elseif ($storage1 === $storage2) {
@@ -1721,50 +1717,6 @@ private function assertPathLength($path) {
17211717
}
17221718
}
17231719

1724-
/**
1725-
* check if it is allowed to move a mount point to a given target.
1726-
* It is not allowed to move a mount point into a different mount point or
1727-
* into an already shared folder
1728-
*
1729-
* @param string $target path
1730-
* @return boolean
1731-
*/
1732-
private function isTargetAllowed($target) {
1733-
1734-
list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target);
1735-
if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) {
1736-
\OCP\Util::writeLog('files',
1737-
'It is not allowed to move one mount point into another one',
1738-
\OCP\Util::DEBUG);
1739-
return false;
1740-
}
1741-
1742-
// note: cannot use the view because the target is already locked
1743-
$fileId = (int)$targetStorage->getCache()->getId($targetInternalPath);
1744-
if ($fileId === -1) {
1745-
// target might not exist, need to check parent instead
1746-
$fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath));
1747-
}
1748-
1749-
// check if any of the parents were shared by the current owner (include collections)
1750-
$shares = \OCP\Share::getItemShared(
1751-
'folder',
1752-
$fileId,
1753-
\OCP\Share::FORMAT_NONE,
1754-
null,
1755-
true
1756-
);
1757-
1758-
if (count($shares) > 0) {
1759-
\OCP\Util::writeLog('files',
1760-
'It is not allowed to move one mount point into a shared folder',
1761-
\OCP\Util::DEBUG);
1762-
return false;
1763-
}
1764-
1765-
return true;
1766-
}
1767-
17681720
/**
17691721
* Get a fileinfo object for files that are ignored in the cache (part files)
17701722
*

lib/private/Share20/Manager.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,24 @@ public function getShareById($id, $recipient = null) {
983983
* @return Share[]
984984
*/
985985
public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) {
986+
$types = [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP];
987+
$providers = [];
988+
$results = [];
989+
990+
foreach ($types as $type) {
991+
$providers[] = $this->factory->getProviderForType($type);
992+
}
993+
994+
// in most cases it's the same provider
995+
if ($providers[0] === $providers[1]) {
996+
$providers = [$providers[0]];
997+
}
998+
999+
foreach ($providers as $provider) {
1000+
$results = array_merge($results, $provider->getSharesByPath($path));
1001+
}
1002+
1003+
return $results;
9861004
}
9871005

9881006
/**

0 commit comments

Comments
 (0)