Updater.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_Sharing;
  26. class Updater {
  27. /**
  28. * @param array $params
  29. */
  30. static public function renameHook($params) {
  31. self::renameChildren($params['oldpath'], $params['newpath']);
  32. self::moveShareToShare($params['newpath']);
  33. }
  34. /**
  35. * Fix for https://github.com/owncloud/core/issues/20769
  36. *
  37. * The owner is allowed to move their files (if they are shared) into a receiving folder
  38. * In this case we need to update the parent of the moved share. Since they are
  39. * effectively handing over ownership of the file the rest of the code needs to know
  40. * they need to build up the reshare tree.
  41. *
  42. * @param string $path
  43. */
  44. static private function moveShareToShare($path) {
  45. $userFolder = \OC::$server->getUserFolder();
  46. // If the user folder can't be constructed (e.g. link share) just return.
  47. if ($userFolder === null) {
  48. return;
  49. }
  50. $src = $userFolder->get($path);
  51. $shareManager = \OC::$server->getShareManager();
  52. $shares = $shareManager->getSharesBy($userFolder->getOwner()->getUID(), \OCP\Share::SHARE_TYPE_USER, $src, false, -1);
  53. $shares = array_merge($shares, $shareManager->getSharesBy($userFolder->getOwner()->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $src, false, -1));
  54. $shares = array_merge($shares, $shareManager->getSharesBy($userFolder->getOwner()->getUID(), \OCP\Share::SHARE_TYPE_ROOM, $src, false, -1));
  55. // If the path we move is not a share we don't care
  56. if (empty($shares)) {
  57. return;
  58. }
  59. // Check if the destination is inside a share
  60. $mountManager = \OC::$server->getMountManager();
  61. $dstMount = $mountManager->find($src->getPath());
  62. if (!($dstMount instanceof \OCA\Files_Sharing\SharedMount)) {
  63. return;
  64. }
  65. $newOwner = $dstMount->getShare()->getShareOwner();
  66. //Ownership is moved over
  67. foreach ($shares as $share) {
  68. /** @var \OCP\Share\IShare $share */
  69. $share->setShareOwner($newOwner);
  70. $shareManager->updateShare($share);
  71. }
  72. }
  73. /**
  74. * rename mount point from the children if the parent was renamed
  75. *
  76. * @param string $oldPath old path relative to data/user/files
  77. * @param string $newPath new path relative to data/user/files
  78. */
  79. static private function renameChildren($oldPath, $newPath) {
  80. $absNewPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $newPath);
  81. $absOldPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $oldPath);
  82. $mountManager = \OC\Files\Filesystem::getMountManager();
  83. $mountedShares = $mountManager->findIn('/' . \OCP\User::getUser() . '/files/' . $oldPath);
  84. foreach ($mountedShares as $mount) {
  85. if ($mount->getStorage()->instanceOfStorage(ISharedStorage::class)) {
  86. $mountPoint = $mount->getMountPoint();
  87. $target = str_replace($absOldPath, $absNewPath, $mountPoint);
  88. $mount->moveMount($target);
  89. }
  90. }
  91. }
  92. }