1
0

Updater.php 4.6 KB

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