1
0

HookManager.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Encryption;
  8. use OC\Files\Filesystem;
  9. use OC\Files\SetupManager;
  10. use OC\Files\View;
  11. use OCP\Encryption\IFile;
  12. use Psr\Log\LoggerInterface;
  13. class HookManager {
  14. private static ?Update $updater = null;
  15. public static function postShared($params): void {
  16. self::getUpdate()->postShared($params);
  17. }
  18. public static function postUnshared($params): void {
  19. // In case the unsharing happens in a background job, we don't have
  20. // a session and we load instead the user from the UserManager
  21. $path = Filesystem::getPath($params['fileSource']);
  22. $owner = Filesystem::getOwner($path);
  23. self::getUpdate($owner)->postUnshared($params);
  24. }
  25. public static function postRename($params): void {
  26. self::getUpdate()->postRename($params);
  27. }
  28. public static function postRestore($params): void {
  29. self::getUpdate()->postRestore($params);
  30. }
  31. private static function getUpdate(?string $owner = null): Update {
  32. if (is_null(self::$updater)) {
  33. $user = \OC::$server->getUserSession()->getUser();
  34. if (!$user && $owner) {
  35. $user = \OC::$server->getUserManager()->get($owner);
  36. }
  37. if (!$user) {
  38. throw new \Exception("Inconsistent data, File unshared, but owner not found. Should not happen");
  39. }
  40. $uid = '';
  41. if ($user) {
  42. $uid = $user->getUID();
  43. }
  44. $setupManager = \OC::$server->get(SetupManager::class);
  45. if (!$setupManager->isSetupComplete($user)) {
  46. $setupManager->setupForUser($user);
  47. }
  48. self::$updater = new Update(
  49. new View(),
  50. new Util(
  51. new View(),
  52. \OC::$server->getUserManager(),
  53. \OC::$server->getGroupManager(),
  54. \OC::$server->getConfig()),
  55. Filesystem::getMountManager(),
  56. \OC::$server->getEncryptionManager(),
  57. \OC::$server->get(IFile::class),
  58. \OC::$server->get(LoggerInterface::class),
  59. $uid
  60. );
  61. }
  62. return self::$updater;
  63. }
  64. }