1
0

EncryptionWrapper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. * @author Robin Appelman <robin@icewind.nl>
  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 OC\Encryption;
  26. use OC\Files\Filesystem;
  27. use OC\Files\Storage\Wrapper\Encryption;
  28. use OC\Files\View;
  29. use OC\Memcache\ArrayCache;
  30. use OCP\Files\Mount\IMountPoint;
  31. use OCP\Files\Storage\IDisableEncryptionStorage;
  32. use OCP\Files\Storage\IStorage;
  33. use Psr\Log\LoggerInterface;
  34. /**
  35. * Class EncryptionWrapper
  36. *
  37. * applies the encryption storage wrapper
  38. *
  39. * @package OC\Encryption
  40. */
  41. class EncryptionWrapper {
  42. /** @var ArrayCache */
  43. private $arrayCache;
  44. /** @var Manager */
  45. private $manager;
  46. private LoggerInterface $logger;
  47. /**
  48. * EncryptionWrapper constructor.
  49. */
  50. public function __construct(ArrayCache $arrayCache,
  51. Manager $manager,
  52. LoggerInterface $logger
  53. ) {
  54. $this->arrayCache = $arrayCache;
  55. $this->manager = $manager;
  56. $this->logger = $logger;
  57. }
  58. /**
  59. * Wraps the given storage when it is not a shared storage
  60. *
  61. * @param string $mountPoint
  62. * @param IStorage $storage
  63. * @param IMountPoint $mount
  64. * @param bool $force apply the wrapper even if the storage normally has encryption disabled, helpful for repair steps
  65. * @return Encryption|IStorage
  66. */
  67. public function wrapStorage(string $mountPoint, IStorage $storage, IMountPoint $mount, bool $force = false) {
  68. $parameters = [
  69. 'storage' => $storage,
  70. 'mountPoint' => $mountPoint,
  71. 'mount' => $mount
  72. ];
  73. if ($force || (!$storage->instanceOfStorage(IDisableEncryptionStorage::class) && $mountPoint !== '/')) {
  74. $user = \OC::$server->getUserSession()->getUser();
  75. $mountManager = Filesystem::getMountManager();
  76. $uid = $user ? $user->getUID() : null;
  77. $fileHelper = \OC::$server->getEncryptionFilesHelper();
  78. $keyStorage = \OC::$server->getEncryptionKeyStorage();
  79. $util = new Util(
  80. new View(),
  81. \OC::$server->getUserManager(),
  82. \OC::$server->getGroupManager(),
  83. \OC::$server->getConfig()
  84. );
  85. $update = new Update(
  86. new View(),
  87. $util,
  88. Filesystem::getMountManager(),
  89. $this->manager,
  90. $fileHelper,
  91. $this->logger,
  92. $uid
  93. );
  94. return new Encryption(
  95. $parameters,
  96. $this->manager,
  97. $util,
  98. $this->logger,
  99. $fileHelper,
  100. $uid,
  101. $keyStorage,
  102. $update,
  103. $mountManager,
  104. $this->arrayCache
  105. );
  106. } else {
  107. return $storage;
  108. }
  109. }
  110. }