GlobalStoragesController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\NotFoundException;
  9. use OCA\Files_External\Service\GlobalStoragesService;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\IConfig;
  14. use OCP\IGroupManager;
  15. use OCP\IL10N;
  16. use OCP\IRequest;
  17. use OCP\IUserSession;
  18. use Psr\Log\LoggerInterface;
  19. /**
  20. * Global storages controller
  21. */
  22. class GlobalStoragesController extends StoragesController {
  23. /**
  24. * Creates a new global storages controller.
  25. *
  26. * @param string $AppName application name
  27. * @param IRequest $request request object
  28. * @param IL10N $l10n l10n service
  29. * @param GlobalStoragesService $globalStoragesService storage service
  30. * @param LoggerInterface $logger
  31. * @param IUserSession $userSession
  32. * @param IGroupManager $groupManager
  33. * @param IConfig $config
  34. */
  35. public function __construct(
  36. $AppName,
  37. IRequest $request,
  38. IL10N $l10n,
  39. GlobalStoragesService $globalStoragesService,
  40. LoggerInterface $logger,
  41. IUserSession $userSession,
  42. IGroupManager $groupManager,
  43. IConfig $config,
  44. ) {
  45. parent::__construct(
  46. $AppName,
  47. $request,
  48. $l10n,
  49. $globalStoragesService,
  50. $logger,
  51. $userSession,
  52. $groupManager,
  53. $config
  54. );
  55. }
  56. /**
  57. * Create an external storage entry.
  58. *
  59. * @param string $mountPoint storage mount point
  60. * @param string $backend backend identifier
  61. * @param string $authMechanism authentication mechanism identifier
  62. * @param array $backendOptions backend-specific options
  63. * @param array $mountOptions mount-specific options
  64. * @param array $applicableUsers users for which to mount the storage
  65. * @param array $applicableGroups groups for which to mount the storage
  66. * @param int $priority priority
  67. *
  68. * @return DataResponse
  69. */
  70. #[PasswordConfirmationRequired]
  71. public function create(
  72. $mountPoint,
  73. $backend,
  74. $authMechanism,
  75. $backendOptions,
  76. $mountOptions,
  77. $applicableUsers,
  78. $applicableGroups,
  79. $priority,
  80. ) {
  81. $canCreateNewLocalStorage = $this->config->getSystemValue('files_external_allow_create_new_local', true);
  82. if (!$canCreateNewLocalStorage && $backend === 'local') {
  83. return new DataResponse(
  84. [
  85. 'message' => $this->l10n->t('Forbidden to manage local mounts')
  86. ],
  87. Http::STATUS_FORBIDDEN
  88. );
  89. }
  90. $newStorage = $this->createStorage(
  91. $mountPoint,
  92. $backend,
  93. $authMechanism,
  94. $backendOptions,
  95. $mountOptions,
  96. $applicableUsers,
  97. $applicableGroups,
  98. $priority
  99. );
  100. if ($newStorage instanceof DataResponse) {
  101. return $newStorage;
  102. }
  103. $response = $this->validate($newStorage);
  104. if (!empty($response)) {
  105. return $response;
  106. }
  107. $newStorage = $this->service->addStorage($newStorage);
  108. $this->updateStorageStatus($newStorage);
  109. return new DataResponse(
  110. $newStorage->jsonSerialize(true),
  111. Http::STATUS_CREATED
  112. );
  113. }
  114. /**
  115. * Update an external storage entry.
  116. *
  117. * @param int $id storage id
  118. * @param string $mountPoint storage mount point
  119. * @param string $backend backend identifier
  120. * @param string $authMechanism authentication mechanism identifier
  121. * @param array $backendOptions backend-specific options
  122. * @param array $mountOptions mount-specific options
  123. * @param array $applicableUsers users for which to mount the storage
  124. * @param array $applicableGroups groups for which to mount the storage
  125. * @param int $priority priority
  126. * @param bool $testOnly whether to storage should only test the connection or do more things
  127. *
  128. * @return DataResponse
  129. */
  130. #[PasswordConfirmationRequired]
  131. public function update(
  132. $id,
  133. $mountPoint,
  134. $backend,
  135. $authMechanism,
  136. $backendOptions,
  137. $mountOptions,
  138. $applicableUsers,
  139. $applicableGroups,
  140. $priority,
  141. $testOnly = true,
  142. ) {
  143. $storage = $this->createStorage(
  144. $mountPoint,
  145. $backend,
  146. $authMechanism,
  147. $backendOptions,
  148. $mountOptions,
  149. $applicableUsers,
  150. $applicableGroups,
  151. $priority
  152. );
  153. if ($storage instanceof DataResponse) {
  154. return $storage;
  155. }
  156. $storage->setId($id);
  157. $response = $this->validate($storage);
  158. if (!empty($response)) {
  159. return $response;
  160. }
  161. try {
  162. $storage = $this->service->updateStorage($storage);
  163. } catch (NotFoundException $e) {
  164. return new DataResponse(
  165. [
  166. 'message' => $this->l10n->t('Storage with ID "%d" not found', [$id])
  167. ],
  168. Http::STATUS_NOT_FOUND
  169. );
  170. }
  171. $this->updateStorageStatus($storage, $testOnly);
  172. return new DataResponse(
  173. $storage->jsonSerialize(true),
  174. Http::STATUS_OK
  175. );
  176. }
  177. }