Updater.php 4.7 KB

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