You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigAdapter.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Robin Appelman <icewind@owncloud.com>
  6. * @author Robin McCorkell <robin@mccorkell.me.uk>
  7. * @author Vincent Petry <pvince81@owncloud.com>
  8. *
  9. * @copyright Copyright (c) 2016, ownCloud, Inc.
  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_External\Config;
  26. use OC\Files\Storage\Wrapper\Availability;
  27. use OCA\Files_External\Migration\StorageMigrator;
  28. use OCP\Files\Storage;
  29. use OC\Files\Mount\MountPoint;
  30. use OCP\Files\Storage\IStorageFactory;
  31. use OCA\Files_External\Lib\PersonalMount;
  32. use OCP\Files\Config\IMountProvider;
  33. use OCP\IUser;
  34. use OCA\Files_External\Service\UserStoragesService;
  35. use OCA\Files_External\Service\UserGlobalStoragesService;
  36. use OCA\Files_External\Lib\StorageConfig;
  37. use OC\Files\Storage\FailedStorage;
  38. use OCP\Files\StorageNotAvailableException;
  39. /**
  40. * Make the old files_external config work with the new public mount config api
  41. */
  42. class ConfigAdapter implements IMountProvider {
  43. /** @var UserStoragesService */
  44. private $userStoragesService;
  45. /** @var UserGlobalStoragesService */
  46. private $userGlobalStoragesService;
  47. /** @var StorageMigrator */
  48. private $migrator;
  49. /**
  50. * @param UserStoragesService $userStoragesService
  51. * @param UserGlobalStoragesService $userGlobalStoragesService
  52. * @param StorageMigrator $migrator
  53. */
  54. public function __construct(
  55. UserStoragesService $userStoragesService,
  56. UserGlobalStoragesService $userGlobalStoragesService,
  57. StorageMigrator $migrator
  58. ) {
  59. $this->userStoragesService = $userStoragesService;
  60. $this->userGlobalStoragesService = $userGlobalStoragesService;
  61. $this->migrator = $migrator;
  62. }
  63. /**
  64. * Process storage ready for mounting
  65. *
  66. * @param StorageConfig $storage
  67. * @param IUser $user
  68. */
  69. private function prepareStorageConfig(StorageConfig &$storage, IUser $user) {
  70. foreach ($storage->getBackendOptions() as $option => $value) {
  71. $storage->setBackendOption($option, \OC_Mount_Config::setUserVars(
  72. $user->getUID(), $value
  73. ));
  74. }
  75. $objectStore = $storage->getBackendOption('objectstore');
  76. if ($objectStore) {
  77. $objectClass = $objectStore['class'];
  78. if (!is_subclass_of($objectClass, '\OCP\Files\ObjectStore\IObjectStore')) {
  79. throw new \InvalidArgumentException('Invalid object store');
  80. }
  81. $storage->setBackendOption('objectstore', new $objectClass($objectStore));
  82. }
  83. $storage->getAuthMechanism()->manipulateStorageConfig($storage, $user);
  84. $storage->getBackend()->manipulateStorageConfig($storage, $user);
  85. }
  86. /**
  87. * Construct the storage implementation
  88. *
  89. * @param StorageConfig $storageConfig
  90. * @return Storage
  91. */
  92. private function constructStorage(StorageConfig $storageConfig) {
  93. $class = $storageConfig->getBackend()->getStorageClass();
  94. $storage = new $class($storageConfig->getBackendOptions());
  95. // auth mechanism should fire first
  96. $storage = $storageConfig->getBackend()->wrapStorage($storage);
  97. $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage);
  98. return $storage;
  99. }
  100. /**
  101. * Get all mountpoints applicable for the user
  102. *
  103. * @param \OCP\IUser $user
  104. * @param \OCP\Files\Storage\IStorageFactory $loader
  105. * @return \OCP\Files\Mount\IMountPoint[]
  106. */
  107. public function getMountsForUser(IUser $user, IStorageFactory $loader) {
  108. $this->migrator->migrateUser($user);
  109. $mounts = [];
  110. $this->userStoragesService->setUser($user);
  111. $this->userGlobalStoragesService->setUser($user);
  112. foreach ($this->userGlobalStoragesService->getUniqueStorages() as $storage) {
  113. try {
  114. $this->prepareStorageConfig($storage, $user);
  115. $impl = $this->constructStorage($storage);
  116. } catch (\Exception $e) {
  117. // propagate exception into filesystem
  118. $impl = new FailedStorage(['exception' => $e]);
  119. }
  120. try {
  121. $availability = $impl->getAvailability();
  122. if (!$availability['available'] && !Availability::shouldRecheck($availability)) {
  123. $impl = new FailedStorage([
  124. 'exception' => new StorageNotAvailableException('Storage with mount id ' . $storage->getId() . ' is not available')
  125. ]);
  126. }
  127. } catch (\Exception $e) {
  128. // propagate exception into filesystem
  129. $impl = new FailedStorage(['exception' => $e]);
  130. }
  131. $mount = new MountPoint(
  132. $impl,
  133. '/' . $user->getUID() . '/files' . $storage->getMountPoint(),
  134. null,
  135. $loader,
  136. $storage->getMountOptions()
  137. );
  138. $mounts[$storage->getMountPoint()] = $mount;
  139. }
  140. foreach ($this->userStoragesService->getStorages() as $storage) {
  141. try {
  142. $this->prepareStorageConfig($storage, $user);
  143. $impl = $this->constructStorage($storage);
  144. } catch (\Exception $e) {
  145. // propagate exception into filesystem
  146. $impl = new FailedStorage(['exception' => $e]);
  147. }
  148. $mount = new PersonalMount(
  149. $this->userStoragesService,
  150. $storage->getId(),
  151. $impl,
  152. '/' . $user->getUID() . '/files' . $storage->getMountPoint(),
  153. null,
  154. $loader,
  155. $storage->getMountOptions()
  156. );
  157. $mounts[$storage->getMountPoint()] = $mount;
  158. }
  159. $this->userStoragesService->resetUser();
  160. $this->userGlobalStoragesService->resetUser();
  161. return $mounts;
  162. }
  163. }