Updater.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // We intentionally include invalid shares, as they have been automatically invalidated due to the node no longer
  46. // being accessible for the user. Only in this case where we adjust the share after it was moved we want to ignore
  47. // this to be able to still adjust it.
  48. // FIXME: should CIRCLES be included here ??
  49. $shares = $shareManager->getSharesBy($user->getUID(), IShare::TYPE_USER, $src, false, -1, onlyValid: false);
  50. $shares = array_merge($shares, $shareManager->getSharesBy($user->getUID(), IShare::TYPE_GROUP, $src, false, -1, onlyValid: false));
  51. $shares = array_merge($shares, $shareManager->getSharesBy($user->getUID(), IShare::TYPE_ROOM, $src, false, -1, onlyValid: false));
  52. if ($src instanceof Folder) {
  53. $cacheAccess = Server::get(FileAccess::class);
  54. $sourceStorageId = $src->getStorage()->getCache()->getNumericStorageId();
  55. $sourceInternalPath = $src->getInternalPath();
  56. $subShares = array_merge(
  57. $shareManager->getSharesBy($user->getUID(), IShare::TYPE_USER, onlyValid: false),
  58. $shareManager->getSharesBy($user->getUID(), IShare::TYPE_GROUP, onlyValid: false),
  59. $shareManager->getSharesBy($user->getUID(), IShare::TYPE_ROOM, onlyValid: false),
  60. );
  61. $shareSourceIds = array_map(fn (IShare $share) => $share->getNodeId(), $subShares);
  62. $shareSources = $cacheAccess->getByFileIdsInStorage($shareSourceIds, $sourceStorageId);
  63. foreach ($subShares as $subShare) {
  64. $shareCacheEntry = $shareSources[$subShare->getNodeId()] ?? null;
  65. if (
  66. $shareCacheEntry &&
  67. str_starts_with($shareCacheEntry->getPath(), $sourceInternalPath . '/')
  68. ) {
  69. $shares[] = $subShare;
  70. }
  71. }
  72. }
  73. // If the path we move is not a share we don't care
  74. if (empty($shares)) {
  75. return;
  76. }
  77. // Check if the destination is inside a share
  78. $mountManager = \OC::$server->getMountManager();
  79. $dstMount = $mountManager->find($src->getPath());
  80. //Ownership is moved over
  81. foreach ($shares as $share) {
  82. if (
  83. $share->getShareType() !== IShare::TYPE_USER &&
  84. $share->getShareType() !== IShare::TYPE_GROUP &&
  85. $share->getShareType() !== IShare::TYPE_ROOM
  86. ) {
  87. continue;
  88. }
  89. if ($dstMount instanceof SharedMount) {
  90. if (!($dstMount->getShare()->getPermissions() & Constants::PERMISSION_SHARE)) {
  91. $shareManager->deleteShare($share);
  92. continue;
  93. }
  94. $newOwner = $dstMount->getShare()->getShareOwner();
  95. $newPermissions = $share->getPermissions() & $dstMount->getShare()->getPermissions();
  96. } else {
  97. $newOwner = $userFolder->getOwner()->getUID();
  98. $newPermissions = $share->getPermissions();
  99. }
  100. $share->setShareOwner($newOwner);
  101. $share->setPermissions($newPermissions);
  102. $shareManager->updateShare($share, onlyValid: false);
  103. }
  104. }
  105. /**
  106. * rename mount point from the children if the parent was renamed
  107. *
  108. * @param string $oldPath old path relative to data/user/files
  109. * @param string $newPath new path relative to data/user/files
  110. */
  111. private static function renameChildren($oldPath, $newPath) {
  112. $absNewPath = Filesystem::normalizePath('/' . \OC_User::getUser() . '/files/' . $newPath);
  113. $absOldPath = Filesystem::normalizePath('/' . \OC_User::getUser() . '/files/' . $oldPath);
  114. $mountManager = Filesystem::getMountManager();
  115. $mountedShares = $mountManager->findIn('/' . \OC_User::getUser() . '/files/' . $oldPath);
  116. foreach ($mountedShares as $mount) {
  117. /** @var MountPoint $mount */
  118. if ($mount->getStorage()->instanceOfStorage(ISharedStorage::class)) {
  119. $mountPoint = $mount->getMountPoint();
  120. $target = str_replace($absOldPath, $absNewPath, $mountPoint);
  121. $mount->moveMount($target);
  122. }
  123. }
  124. }
  125. }