GlobalStoragesController.php 4.5 KB

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