UserStoragesController.php 5.1 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\Attribute\NoAdminRequired;
  15. use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
  16. use OCP\AppFramework\Http\DataResponse;
  17. use OCP\IConfig;
  18. use OCP\IGroupManager;
  19. use OCP\IL10N;
  20. use OCP\IRequest;
  21. use OCP\IUserSession;
  22. use Psr\Log\LoggerInterface;
  23. /**
  24. * User storages controller
  25. */
  26. class UserStoragesController extends StoragesController {
  27. /**
  28. * Creates a new user storages controller.
  29. *
  30. * @param string $AppName application name
  31. * @param IRequest $request request object
  32. * @param IL10N $l10n l10n service
  33. * @param UserStoragesService $userStoragesService storage service
  34. * @param LoggerInterface $logger
  35. * @param IUserSession $userSession
  36. * @param IGroupManager $groupManager
  37. */
  38. public function __construct(
  39. $AppName,
  40. IRequest $request,
  41. IL10N $l10n,
  42. UserStoragesService $userStoragesService,
  43. LoggerInterface $logger,
  44. IUserSession $userSession,
  45. IGroupManager $groupManager,
  46. IConfig $config,
  47. ) {
  48. parent::__construct(
  49. $AppName,
  50. $request,
  51. $l10n,
  52. $userStoragesService,
  53. $logger,
  54. $userSession,
  55. $groupManager,
  56. $config
  57. );
  58. }
  59. protected function manipulateStorageConfig(StorageConfig $storage) {
  60. /** @var AuthMechanism */
  61. $authMechanism = $storage->getAuthMechanism();
  62. $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser());
  63. /** @var Backend */
  64. $backend = $storage->getBackend();
  65. $backend->manipulateStorageConfig($storage, $this->userSession->getUser());
  66. }
  67. /**
  68. * Get all storage entries
  69. *
  70. * @return DataResponse
  71. */
  72. #[NoAdminRequired]
  73. public function index() {
  74. return parent::index();
  75. }
  76. /**
  77. * Return storage
  78. *
  79. * {@inheritdoc}
  80. */
  81. #[NoAdminRequired]
  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. #[PasswordConfirmationRequired]
  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. #[PasswordConfirmationRequired]
  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. * {@inheritdoc}
  194. */
  195. #[NoAdminRequired]
  196. #[PasswordConfirmationRequired]
  197. public function destroy($id) {
  198. return parent::destroy($id);
  199. }
  200. }