Updater.php 4.0 KB

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