2013-02-26 01:21:48 -05:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2013-02-26 01:21:48 -05:00
|
|
|
/**
|
2024-06-06 13:48:28 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2013-02-26 01:21:48 -05:00
|
|
|
*/
|
2016-05-02 09:16:20 -04:00
|
|
|
namespace OCA\Files_Sharing;
|
2013-02-26 01:21:48 -05:00
|
|
|
|
2024-03-07 05:59:53 -05:00
|
|
|
use OC\Files\Cache\FileAccess;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OC\Files\Filesystem;
|
2022-03-03 11:15:02 -05:00
|
|
|
use OC\Files\Mount\MountPoint;
|
2021-04-15 06:51:03 -04:00
|
|
|
use OCP\Constants;
|
2022-03-28 03:28:37 -04:00
|
|
|
use OCP\Files\Folder;
|
2025-02-03 09:34:01 -05:00
|
|
|
use OCP\Files\Mount\IMountManager;
|
2024-03-07 05:59:53 -05:00
|
|
|
use OCP\Server;
|
2020-06-24 10:49:16 -04:00
|
|
|
use OCP\Share\IShare;
|
|
|
|
|
|
2016-05-02 09:16:20 -04:00
|
|
|
class Updater {
|
2013-02-26 01:21:48 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $params
|
|
|
|
|
*/
|
2020-04-10 10:51:06 -04:00
|
|
|
public static function renameHook($params) {
|
2014-07-02 09:35:15 -04:00
|
|
|
self::renameChildren($params['oldpath'], $params['newpath']);
|
2022-03-31 06:08:42 -04:00
|
|
|
self::moveShareInOrOutOfShare($params['newpath']);
|
2015-12-07 07:24:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fix for https://github.com/owncloud/core/issues/20769
|
|
|
|
|
*
|
|
|
|
|
* The owner is allowed to move their files (if they are shared) into a receiving folder
|
|
|
|
|
* In this case we need to update the parent of the moved share. Since they are
|
|
|
|
|
* effectively handing over ownership of the file the rest of the code needs to know
|
|
|
|
|
* they need to build up the reshare tree.
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
*/
|
2022-03-31 06:08:42 -04:00
|
|
|
private static function moveShareInOrOutOfShare($path): void {
|
2015-12-07 07:24:16 -05:00
|
|
|
$userFolder = \OC::$server->getUserFolder();
|
2016-02-05 07:57:57 -05:00
|
|
|
|
|
|
|
|
// If the user folder can't be constructed (e.g. link share) just return.
|
|
|
|
|
if ($userFolder === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-03-07 05:59:53 -05:00
|
|
|
$user = $userFolder->getOwner();
|
|
|
|
|
if (!$user) {
|
|
|
|
|
throw new \Exception('user folder has no owner');
|
|
|
|
|
}
|
2016-02-05 07:57:57 -05:00
|
|
|
|
2015-12-07 07:24:16 -05:00
|
|
|
$src = $userFolder->get($path);
|
|
|
|
|
|
2025-02-03 09:34:01 -05:00
|
|
|
$shareManager = Server::get(\OCP\Share\IManager::class);
|
2016-04-15 03:01:10 -04:00
|
|
|
|
2024-11-14 01:41:41 -05:00
|
|
|
// We intentionally include invalid shares, as they have been automatically invalidated due to the node no longer
|
|
|
|
|
// being accessible for the user. Only in this case where we adjust the share after it was moved we want to ignore
|
|
|
|
|
// this to be able to still adjust it.
|
|
|
|
|
|
2022-03-28 03:28:37 -04:00
|
|
|
// FIXME: should CIRCLES be included here ??
|
2024-11-14 01:41:41 -05:00
|
|
|
$shares = $shareManager->getSharesBy($user->getUID(), IShare::TYPE_USER, $src, false, -1, onlyValid: false);
|
|
|
|
|
$shares = array_merge($shares, $shareManager->getSharesBy($user->getUID(), IShare::TYPE_GROUP, $src, false, -1, onlyValid: false));
|
|
|
|
|
$shares = array_merge($shares, $shareManager->getSharesBy($user->getUID(), IShare::TYPE_ROOM, $src, false, -1, onlyValid: false));
|
2015-12-07 07:24:16 -05:00
|
|
|
|
2022-03-28 03:28:37 -04:00
|
|
|
if ($src instanceof Folder) {
|
2024-03-07 05:59:53 -05:00
|
|
|
$cacheAccess = Server::get(FileAccess::class);
|
|
|
|
|
|
|
|
|
|
$sourceStorageId = $src->getStorage()->getCache()->getNumericStorageId();
|
|
|
|
|
$sourceInternalPath = $src->getInternalPath();
|
|
|
|
|
$subShares = array_merge(
|
2024-11-14 01:41:41 -05:00
|
|
|
$shareManager->getSharesBy($user->getUID(), IShare::TYPE_USER, onlyValid: false),
|
|
|
|
|
$shareManager->getSharesBy($user->getUID(), IShare::TYPE_GROUP, onlyValid: false),
|
|
|
|
|
$shareManager->getSharesBy($user->getUID(), IShare::TYPE_ROOM, onlyValid: false),
|
2024-03-07 05:59:53 -05:00
|
|
|
);
|
|
|
|
|
$shareSourceIds = array_map(fn (IShare $share) => $share->getNodeId(), $subShares);
|
|
|
|
|
$shareSources = $cacheAccess->getByFileIdsInStorage($shareSourceIds, $sourceStorageId);
|
2022-03-28 03:28:37 -04:00
|
|
|
foreach ($subShares as $subShare) {
|
2024-03-07 05:59:53 -05:00
|
|
|
$shareCacheEntry = $shareSources[$subShare->getNodeId()] ?? null;
|
|
|
|
|
if (
|
|
|
|
|
$shareCacheEntry
|
|
|
|
|
&& str_starts_with($shareCacheEntry->getPath(), $sourceInternalPath . '/')
|
|
|
|
|
) {
|
|
|
|
|
$shares[] = $subShare;
|
|
|
|
|
}
|
2022-03-28 03:28:37 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-07 07:24:16 -05:00
|
|
|
// If the path we move is not a share we don't care
|
|
|
|
|
if (empty($shares)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if the destination is inside a share
|
2025-02-03 09:34:01 -05:00
|
|
|
$mountManager = Server::get(IMountManager::class);
|
2015-12-07 07:24:16 -05:00
|
|
|
$dstMount = $mountManager->find($src->getPath());
|
|
|
|
|
|
2016-04-15 03:01:10 -04:00
|
|
|
//Ownership is moved over
|
2015-12-07 07:24:16 -05:00
|
|
|
foreach ($shares as $share) {
|
2022-03-28 03:28:37 -04:00
|
|
|
if (
|
|
|
|
|
$share->getShareType() !== IShare::TYPE_USER
|
|
|
|
|
&& $share->getShareType() !== IShare::TYPE_GROUP
|
|
|
|
|
&& $share->getShareType() !== IShare::TYPE_ROOM
|
|
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
if ($dstMount instanceof SharedMount) {
|
2022-03-31 06:08:42 -04:00
|
|
|
if (!($dstMount->getShare()->getPermissions() & Constants::PERMISSION_SHARE)) {
|
|
|
|
|
$shareManager->deleteShare($share);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$newOwner = $dstMount->getShare()->getShareOwner();
|
|
|
|
|
$newPermissions = $share->getPermissions() & $dstMount->getShare()->getPermissions();
|
|
|
|
|
} else {
|
|
|
|
|
$newOwner = $userFolder->getOwner()->getUID();
|
|
|
|
|
$newPermissions = $share->getPermissions();
|
2021-04-15 06:51:03 -04:00
|
|
|
}
|
2022-03-31 06:08:42 -04:00
|
|
|
|
2016-04-15 03:01:10 -04:00
|
|
|
$share->setShareOwner($newOwner);
|
2022-03-31 06:08:42 -04:00
|
|
|
$share->setPermissions($newPermissions);
|
2024-11-14 01:41:41 -05:00
|
|
|
$shareManager->updateShare($share, onlyValid: false);
|
2015-12-07 07:24:16 -05:00
|
|
|
}
|
2013-02-26 01:21:48 -05:00
|
|
|
}
|
|
|
|
|
|
2014-07-02 09:35:15 -04:00
|
|
|
/**
|
|
|
|
|
* rename mount point from the children if the parent was renamed
|
2014-12-04 13:51:04 -05:00
|
|
|
*
|
2014-07-02 09:35:15 -04:00
|
|
|
* @param string $oldPath old path relative to data/user/files
|
|
|
|
|
* @param string $newPath new path relative to data/user/files
|
|
|
|
|
*/
|
2020-04-10 10:51:06 -04:00
|
|
|
private static function renameChildren($oldPath, $newPath) {
|
2024-10-10 06:40:31 -04:00
|
|
|
$absNewPath = Filesystem::normalizePath('/' . \OC_User::getUser() . '/files/' . $newPath);
|
|
|
|
|
$absOldPath = Filesystem::normalizePath('/' . \OC_User::getUser() . '/files/' . $oldPath);
|
2014-07-02 09:35:15 -04:00
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
$mountManager = Filesystem::getMountManager();
|
2021-02-16 05:56:07 -05:00
|
|
|
$mountedShares = $mountManager->findIn('/' . \OC_User::getUser() . '/files/' . $oldPath);
|
2014-07-02 09:35:15 -04:00
|
|
|
foreach ($mountedShares as $mount) {
|
2022-03-03 11:15:02 -05:00
|
|
|
/** @var MountPoint $mount */
|
2018-01-25 17:16:13 -05:00
|
|
|
if ($mount->getStorage()->instanceOfStorage(ISharedStorage::class)) {
|
2014-07-02 09:35:15 -04:00
|
|
|
$mountPoint = $mount->getMountPoint();
|
|
|
|
|
$target = str_replace($absOldPath, $absNewPath, $mountPoint);
|
|
|
|
|
$mount->moveMount($target);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-07 10:00:03 -05:00
|
|
|
}
|