1
0

Updater.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // If the path we move is not a share we don't care
  55. if (empty($shares)) {
  56. return;
  57. }
  58. // Check if the destination is inside a share
  59. $mountManager = \OC::$server->getMountManager();
  60. $dstMount = $mountManager->find($src->getPath());
  61. if (!($dstMount instanceof \OCA\Files_Sharing\SharedMount)) {
  62. return;
  63. }
  64. $newOwner = $dstMount->getShare()->getShareOwner();
  65. //Ownership is moved over
  66. foreach ($shares as $share) {
  67. /** @var \OCP\Share\IShare $share */
  68. $share->setShareOwner($newOwner);
  69. $shareManager->updateShare($share);
  70. }
  71. }
  72. /**
  73. * rename mount point from the children if the parent was renamed
  74. *
  75. * @param string $oldPath old path relative to data/user/files
  76. * @param string $newPath new path relative to data/user/files
  77. */
  78. static private function renameChildren($oldPath, $newPath) {
  79. $absNewPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $newPath);
  80. $absOldPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files/' . $oldPath);
  81. $mountManager = \OC\Files\Filesystem::getMountManager();
  82. $mountedShares = $mountManager->findIn('/' . \OCP\User::getUser() . '/files/' . $oldPath);
  83. foreach ($mountedShares as $mount) {
  84. if ($mount->getStorage()->instanceOfStorage('OCA\Files_Sharing\ISharedStorage')) {
  85. $mountPoint = $mount->getMountPoint();
  86. $target = str_replace($absOldPath, $absNewPath, $mountPoint);
  87. $mount->moveMount($target);
  88. }
  89. }
  90. }
  91. }