Updater.php 3.8 KB

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