UserStoragesController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Controller;
  8. use OCA\Files_External\Lib\Auth\AuthMechanism;
  9. use OCA\Files_External\Lib\Backend\Backend;
  10. use OCA\Files_External\Lib\StorageConfig;
  11. use OCA\Files_External\NotFoundException;
  12. use OCA\Files_External\Service\UserStoragesService;
  13. use OCP\AppFramework\Http;
  14. use OCP\AppFramework\Http\DataResponse;
  15. use OCP\IConfig;
  16. use OCP\IGroupManager;
  17. use OCP\IL10N;
  18. use OCP\IRequest;
  19. use OCP\IUserSession;
  20. use Psr\Log\LoggerInterface;
  21. /**
  22. * User storages controller
  23. */
  24. class UserStoragesController extends StoragesController {
  25. /**
  26. * Creates a new user storages controller.
  27. *
  28. * @param string $AppName application name
  29. * @param IRequest $request request object
  30. * @param IL10N $l10n l10n service
  31. * @param UserStoragesService $userStoragesService storage service
  32. * @param LoggerInterface $logger
  33. * @param IUserSession $userSession
  34. * @param IGroupManager $groupManager
  35. */
  36. public function __construct(
  37. $AppName,
  38. IRequest $request,
  39. IL10N $l10n,
  40. UserStoragesService $userStoragesService,
  41. LoggerInterface $logger,
  42. IUserSession $userSession,
  43. IGroupManager $groupManager,
  44. IConfig $config
  45. ) {
  46. parent::__construct(
  47. $AppName,
  48. $request,
  49. $l10n,
  50. $userStoragesService,
  51. $logger,
  52. $userSession,
  53. $groupManager,
  54. $config
  55. );
  56. }
  57. protected function manipulateStorageConfig(StorageConfig $storage) {
  58. /** @var AuthMechanism */
  59. $authMechanism = $storage->getAuthMechanism();
  60. $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser());
  61. /** @var Backend */
  62. $backend = $storage->getBackend();
  63. $backend->manipulateStorageConfig($storage, $this->userSession->getUser());
  64. }
  65. /**
  66. * Get all storage entries
  67. *
  68. * @NoAdminRequired
  69. *
  70. * @return DataResponse
  71. */
  72. public function index() {
  73. return parent::index();
  74. }
  75. /**
  76. * Return storage
  77. *
  78. * @NoAdminRequired
  79. *
  80. * {@inheritdoc}
  81. */
  82. public function show($id, $testOnly = true) {
  83. return parent::show($id, $testOnly);
  84. }
  85. /**
  86. * Create an external storage entry.
  87. *
  88. * @param string $mountPoint storage mount point
  89. * @param string $backend backend identifier
  90. * @param string $authMechanism authentication mechanism identifier
  91. * @param array $backendOptions backend-specific options
  92. * @param array $mountOptions backend-specific mount options
  93. *
  94. * @return DataResponse
  95. *
  96. * @NoAdminRequired
  97. */
  98. public function create(
  99. $mountPoint,
  100. $backend,
  101. $authMechanism,
  102. $backendOptions,
  103. $mountOptions
  104. ) {
  105. $canCreateNewLocalStorage = $this->config->getSystemValue('files_external_allow_create_new_local', true);
  106. if (!$canCreateNewLocalStorage && $backend === 'local') {
  107. return new DataResponse(
  108. [
  109. 'message' => $this->l10n->t('Forbidden to manage local mounts')
  110. ],
  111. Http::STATUS_FORBIDDEN
  112. );
  113. }
  114. $newStorage = $this->createStorage(
  115. $mountPoint,
  116. $backend,
  117. $authMechanism,
  118. $backendOptions,
  119. $mountOptions
  120. );
  121. if ($newStorage instanceof DataResponse) {
  122. return $newStorage;
  123. }
  124. $response = $this->validate($newStorage);
  125. if (!empty($response)) {
  126. return $response;
  127. }
  128. $newStorage = $this->service->addStorage($newStorage);
  129. $this->updateStorageStatus($newStorage);
  130. return new DataResponse(
  131. $newStorage->jsonSerialize(true),
  132. Http::STATUS_CREATED
  133. );
  134. }
  135. /**
  136. * Update an external storage entry.
  137. *
  138. * @param int $id storage id
  139. * @param string $mountPoint storage mount point
  140. * @param string $backend backend identifier
  141. * @param string $authMechanism authentication mechanism identifier
  142. * @param array $backendOptions backend-specific options
  143. * @param array $mountOptions backend-specific mount options
  144. * @param bool $testOnly whether to storage should only test the connection or do more things
  145. *
  146. * @return DataResponse
  147. *
  148. * @NoAdminRequired
  149. */
  150. public function update(
  151. $id,
  152. $mountPoint,
  153. $backend,
  154. $authMechanism,
  155. $backendOptions,
  156. $mountOptions,
  157. $testOnly = true
  158. ) {
  159. $storage = $this->createStorage(
  160. $mountPoint,
  161. $backend,
  162. $authMechanism,
  163. $backendOptions,
  164. $mountOptions
  165. );
  166. if ($storage instanceof DataResponse) {
  167. return $storage;
  168. }
  169. $storage->setId($id);
  170. $response = $this->validate($storage);
  171. if (!empty($response)) {
  172. return $response;
  173. }
  174. try {
  175. $storage = $this->service->updateStorage($storage);
  176. } catch (NotFoundException $e) {
  177. return new DataResponse(
  178. [
  179. 'message' => $this->l10n->t('Storage with ID "%d" not found', [$id])
  180. ],
  181. Http::STATUS_NOT_FOUND
  182. );
  183. }
  184. $this->updateStorageStatus($storage, $testOnly);
  185. return new DataResponse(
  186. $storage->jsonSerialize(true),
  187. Http::STATUS_OK
  188. );
  189. }
  190. /**
  191. * Delete storage
  192. *
  193. * @NoAdminRequired
  194. *
  195. * {@inheritdoc}
  196. */
  197. public function destroy($id) {
  198. return parent::destroy($id);
  199. }
  200. }