Updater.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing;
  8. use OC\Files\Cache\FileAccess;
  9. use OC\Files\Filesystem;
  10. use OC\Files\Mount\MountPoint;
  11. use OCP\Constants;
  12. use OCP\Files\Folder;
  13. use OCP\Server;
  14. use OCP\Share\IShare;
  15. class Updater {
  16. /**
  17. * @param array $params
  18. */
  19. public static function renameHook($params) {
  20. self::renameChildren($params['oldpath'], $params['newpath']);
  21. self::moveShareInOrOutOfShare($params['newpath']);
  22. }
  23. /**
  24. * Fix for https://github.com/owncloud/core/issues/20769
  25. *
  26. * The owner is allowed to move their files (if they are shared) into a receiving folder
  27. * In this case we need to update the parent of the moved share. Since they are
  28. * effectively handing over ownership of the file the rest of the code needs to know
  29. * they need to build up the reshare tree.
  30. *
  31. * @param string $path
  32. */
  33. private static function moveShareInOrOutOfShare($path): void {
  34. $userFolder = \OC::$server->getUserFolder();
  35. // If the user folder can't be constructed (e.g. link share) just return.
  36. if ($userFolder === null) {
  37. return;
  38. }
  39. $user = $userFolder->getOwner();
  40. if (!$user) {
  41. throw new \Exception('user folder has no owner');
  42. }
  43. $src = $userFolder->get($path);
  44. $shareManager = \OC::$server->getShareManager();
  45. // FIXME: should CIRCLES be included here ??
  46. $shares = $shareManager->getSharesBy($user->getUID(), IShare::TYPE_USER, $src, false, -1);
  47. $shares = array_merge($shares, $shareManager->getSharesBy($user->getUID(), IShare::TYPE_GROUP, $src, false, -1));
  48. $shares = array_merge($shares, $shareManager->getSharesBy($user->getUID(), IShare::TYPE_ROOM, $src, false, -1));
  49. if ($src instanceof Folder) {
  50. $cacheAccess = Server::get(FileAccess::class);
  51. $sourceStorageId = $src->getStorage()->getCache()->getNumericStorageId();
  52. $sourceInternalPath = $src->getInternalPath();
  53. $subShares = array_merge(
  54. $shareManager->getSharesBy($user->getUID(), IShare::TYPE_USER),
  55. $shareManager->getSharesBy($user->getUID(), IShare::TYPE_GROUP),
  56. $shareManager->getSharesBy($user->getUID(), IShare::TYPE_ROOM),
  57. );
  58. $shareSourceIds = array_map(fn (IShare $share) => $share->getNodeId(), $subShares);
  59. $shareSources = $cacheAccess->getByFileIdsInStorage($shareSourceIds, $sourceStorageId);
  60. foreach ($subShares as $subShare) {
  61. $shareCacheEntry = $shareSources[$subShare->getNodeId()] ?? null;
  62. if (
  63. $shareCacheEntry &&
  64. str_starts_with($shareCacheEntry->getPath(), $sourceInternalPath . '/')
  65. ) {
  66. $shares[] = $subShare;
  67. }
  68. }
  69. }
  70. // If the path we move is not a share we don't care
  71. if (empty($shares)) {
  72. return;
  73. }
  74. // Check if the destination is inside a share
  75. $mountManager = \OC::$server->getMountManager();
  76. $dstMount = $mountManager->find($src->getPath());
  77. //Ownership is moved over
  78. foreach ($shares as $share) {
  79. if (
  80. $share->getShareType() !== IShare::TYPE_USER &&
  81. $share->getShareType() !== IShare::TYPE_GROUP &&
  82. $share->getShareType() !== IShare::TYPE_ROOM
  83. ) {
  84. continue;
  85. }
  86. if ($dstMount instanceof SharedMount) {
  87. if (!($dstMount->getShare()->getPermissions() & Constants::PERMISSION_SHARE)) {
  88. $shareManager->deleteShare($share);
  89. continue;
  90. }
  91. $newOwner = $dstMount->getShare()->getShareOwner();
  92. $newPermissions = $share->getPermissions() & $dstMount->getShare()->getPermissions();
  93. } else {
  94. $newOwner = $userFolder->getOwner()->getUID();
  95. $newPermissions = $share->getPermissions();
  96. }
  97. $share->setShareOwner($newOwner);
  98. $share->setPermissions($newPermissions);
  99. $shareManager->updateShare($share);
  100. }
  101. }
  102. /**
  103. * rename mount point from the children if the parent was renamed
  104. *
  105. * @param string $oldPath old path relative to data/user/files
  106. * @param string $newPath new path relative to data/user/files
  107. */
  108. private static function renameChildren($oldPath, $newPath) {
  109. $absNewPath = Filesystem::normalizePath('/' . \OC_User::getUser() . '/files/' . $newPath);
  110. $absOldPath = Filesystem::normalizePath('/' . \OC_User::getUser() . '/files/' . $oldPath);
  111. $mountManager = Filesystem::getMountManager();
  112. $mountedShares = $mountManager->findIn('/' . \OC_User::getUser() . '/files/' . $oldPath);
  113. foreach ($mountedShares as $mount) {
  114. /** @var MountPoint $mount */
  115. if ($mount->getStorage()->instanceOfStorage(ISharedStorage::class)) {
  116. $mountPoint = $mount->getMountPoint();
  117. $target = str_replace($absOldPath, $absNewPath, $mountPoint);
  118. $mount->moveMount($target);
  119. }
  120. }
  121. }
  122. }