EncryptionWrapper.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Storage\Wrapper\Encryption;
  10. use OC\Files\View;
  11. use OC\Memcache\ArrayCache;
  12. use OCP\Files\Mount\IMountPoint;
  13. use OCP\Files\Storage\IDisableEncryptionStorage;
  14. use OCP\Files\Storage\IStorage;
  15. use Psr\Log\LoggerInterface;
  16. /**
  17. * Class EncryptionWrapper
  18. *
  19. * applies the encryption storage wrapper
  20. *
  21. * @package OC\Encryption
  22. */
  23. class EncryptionWrapper {
  24. /** @var ArrayCache */
  25. private $arrayCache;
  26. /** @var Manager */
  27. private $manager;
  28. private LoggerInterface $logger;
  29. /**
  30. * EncryptionWrapper constructor.
  31. */
  32. public function __construct(ArrayCache $arrayCache,
  33. Manager $manager,
  34. LoggerInterface $logger
  35. ) {
  36. $this->arrayCache = $arrayCache;
  37. $this->manager = $manager;
  38. $this->logger = $logger;
  39. }
  40. /**
  41. * Wraps the given storage when it is not a shared storage
  42. *
  43. * @param string $mountPoint
  44. * @param IStorage $storage
  45. * @param IMountPoint $mount
  46. * @param bool $force apply the wrapper even if the storage normally has encryption disabled, helpful for repair steps
  47. * @return Encryption|IStorage
  48. */
  49. public function wrapStorage(string $mountPoint, IStorage $storage, IMountPoint $mount, bool $force = false) {
  50. $parameters = [
  51. 'storage' => $storage,
  52. 'mountPoint' => $mountPoint,
  53. 'mount' => $mount
  54. ];
  55. if ($force || (!$storage->instanceOfStorage(IDisableEncryptionStorage::class) && $mountPoint !== '/')) {
  56. $user = \OC::$server->getUserSession()->getUser();
  57. $mountManager = Filesystem::getMountManager();
  58. $uid = $user ? $user->getUID() : null;
  59. $fileHelper = \OC::$server->getEncryptionFilesHelper();
  60. $keyStorage = \OC::$server->getEncryptionKeyStorage();
  61. $util = new Util(
  62. new View(),
  63. \OC::$server->getUserManager(),
  64. \OC::$server->getGroupManager(),
  65. \OC::$server->getConfig()
  66. );
  67. $update = new Update(
  68. new View(),
  69. $util,
  70. Filesystem::getMountManager(),
  71. $this->manager,
  72. $fileHelper,
  73. $this->logger,
  74. $uid
  75. );
  76. return new Encryption(
  77. $parameters,
  78. $this->manager,
  79. $util,
  80. $this->logger,
  81. $fileHelper,
  82. $uid,
  83. $keyStorage,
  84. $update,
  85. $mountManager,
  86. $this->arrayCache
  87. );
  88. } else {
  89. return $storage;
  90. }
  91. }
  92. }