Updater.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files_Sharing;
  28. use OC\Files\Cache\FileAccess;
  29. use OC\Files\Mount\MountPoint;
  30. use OCP\Constants;
  31. use OCP\Files\Folder;
  32. use OCP\Server;
  33. use OCP\Share\IShare;
  34. class Updater {
  35. /**
  36. * @param array $params
  37. */
  38. public static function renameHook($params) {
  39. self::renameChildren($params['oldpath'], $params['newpath']);
  40. self::moveShareInOrOutOfShare($params['newpath']);
  41. }
  42. /**
  43. * Fix for https://github.com/owncloud/core/issues/20769
  44. *
  45. * The owner is allowed to move their files (if they are shared) into a receiving folder
  46. * In this case we need to update the parent of the moved share. Since they are
  47. * effectively handing over ownership of the file the rest of the code needs to know
  48. * they need to build up the reshare tree.
  49. *
  50. * @param string $path
  51. */
  52. private static function moveShareInOrOutOfShare($path): void {
  53. $userFolder = \OC::$server->getUserFolder();
  54. // If the user folder can't be constructed (e.g. link share) just return.
  55. if ($userFolder === null) {
  56. return;
  57. }
  58. $user = $userFolder->getOwner();
  59. if (!$user) {
  60. throw new \Exception("user folder has no owner");
  61. }
  62. $src = $userFolder->get($path);
  63. $shareManager = \OC::$server->getShareManager();
  64. // FIXME: should CIRCLES be included here ??
  65. $shares = $shareManager->getSharesBy($user->getUID(), IShare::TYPE_USER, $src, false, -1);
  66. $shares = array_merge($shares, $shareManager->getSharesBy($user->getUID(), IShare::TYPE_GROUP, $src, false, -1));
  67. $shares = array_merge($shares, $shareManager->getSharesBy($user->getUID(), IShare::TYPE_ROOM, $src, false, -1));
  68. if ($src instanceof Folder) {
  69. $cacheAccess = Server::get(FileAccess::class);
  70. $sourceStorageId = $src->getStorage()->getCache()->getNumericStorageId();
  71. $sourceInternalPath = $src->getInternalPath();
  72. $subShares = array_merge(
  73. $shareManager->getSharesBy($user->getUID(), IShare::TYPE_USER),
  74. $shareManager->getSharesBy($user->getUID(), IShare::TYPE_GROUP),
  75. $shareManager->getSharesBy($user->getUID(), IShare::TYPE_ROOM),
  76. );
  77. $shareSourceIds = array_map(fn (IShare $share) => $share->getNodeId(), $subShares);
  78. $shareSources = $cacheAccess->getByFileIdsInStorage($shareSourceIds, $sourceStorageId);
  79. foreach ($subShares as $subShare) {
  80. $shareCacheEntry = $shareSources[$subShare->getNodeId()] ?? null;
  81. if (
  82. $shareCacheEntry &&
  83. str_starts_with($shareCacheEntry->getPath(), $sourceInternalPath . '/')
  84. ) {
  85. $shares[] = $subShare;
  86. }
  87. }
  88. }
  89. // If the path we move is not a share we don't care
  90. if (empty($shares)) {
  91. return;
  92. }
  93. // Check if the destination is inside a share
  94. $mountManager = \OC::$server->getMountManager();
  95. $dstMount = $mountManager->find($src->getPath());
  96. //Ownership is moved over
  97. foreach ($shares as $share) {
  98. if (
  99. $share->getShareType() !== IShare::TYPE_USER &&
  100. $share->getShareType() !== IShare::TYPE_GROUP &&
  101. $share->getShareType() !== IShare::TYPE_ROOM
  102. ) {
  103. continue;
  104. }
  105. if ($dstMount instanceof \OCA\Files_Sharing\SharedMount) {
  106. if (!($dstMount->getShare()->getPermissions() & Constants::PERMISSION_SHARE)) {
  107. $shareManager->deleteShare($share);
  108. continue;
  109. }
  110. $newOwner = $dstMount->getShare()->getShareOwner();
  111. $newPermissions = $share->getPermissions() & $dstMount->getShare()->getPermissions();
  112. } else {
  113. $newOwner = $userFolder->getOwner()->getUID();
  114. $newPermissions = $share->getPermissions();
  115. }
  116. $share->setShareOwner($newOwner);
  117. $share->setPermissions($newPermissions);
  118. $shareManager->updateShare($share);
  119. }
  120. }
  121. /**
  122. * rename mount point from the children if the parent was renamed
  123. *
  124. * @param string $oldPath old path relative to data/user/files
  125. * @param string $newPath new path relative to data/user/files
  126. */
  127. private static function renameChildren($oldPath, $newPath) {
  128. $absNewPath = \OC\Files\Filesystem::normalizePath('/' . \OC_User::getUser() . '/files/' . $newPath);
  129. $absOldPath = \OC\Files\Filesystem::normalizePath('/' . \OC_User::getUser() . '/files/' . $oldPath);
  130. $mountManager = \OC\Files\Filesystem::getMountManager();
  131. $mountedShares = $mountManager->findIn('/' . \OC_User::getUser() . '/files/' . $oldPath);
  132. foreach ($mountedShares as $mount) {
  133. /** @var MountPoint $mount */
  134. if ($mount->getStorage()->instanceOfStorage(ISharedStorage::class)) {
  135. $mountPoint = $mount->getMountPoint();
  136. $target = str_replace($absOldPath, $absNewPath, $mountPoint);
  137. $mount->moveMount($target);
  138. }
  139. }
  140. }
  141. }