HookManager.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 Psr\Log\LoggerInterface;
  12. class HookManager {
  13. private static ?Update $updater = null;
  14. public static function postShared($params): void {
  15. self::getUpdate()->postShared($params);
  16. }
  17. public static function postUnshared($params): void {
  18. // In case the unsharing happens in a background job, we don't have
  19. // a session and we load instead the user from the UserManager
  20. $path = Filesystem::getPath($params['fileSource']);
  21. $owner = Filesystem::getOwner($path);
  22. self::getUpdate($owner)->postUnshared($params);
  23. }
  24. public static function postRename($params): void {
  25. self::getUpdate()->postRename($params);
  26. }
  27. public static function postRestore($params): void {
  28. self::getUpdate()->postRestore($params);
  29. }
  30. private static function getUpdate(?string $owner = null): Update {
  31. if (is_null(self::$updater)) {
  32. $user = \OC::$server->getUserSession()->getUser();
  33. if (!$user && $owner) {
  34. $user = \OC::$server->getUserManager()->get($owner);
  35. }
  36. if (!$user) {
  37. throw new \Exception("Inconsistent data, File unshared, but owner not found. Should not happen");
  38. }
  39. $uid = '';
  40. if ($user) {
  41. $uid = $user->getUID();
  42. }
  43. $setupManager = \OC::$server->get(SetupManager::class);
  44. if (!$setupManager->isSetupComplete($user)) {
  45. $setupManager->setupForUser($user);
  46. }
  47. self::$updater = new Update(
  48. new View(),
  49. new Util(
  50. new View(),
  51. \OC::$server->getUserManager(),
  52. \OC::$server->getGroupManager(),
  53. \OC::$server->getConfig()),
  54. Filesystem::getMountManager(),
  55. \OC::$server->getEncryptionManager(),
  56. \OC::$server->getEncryptionFilesHelper(),
  57. \OC::$server->get(LoggerInterface::class),
  58. $uid
  59. );
  60. }
  61. return self::$updater;
  62. }
  63. }