Updater.php 3.7 KB

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