GlobalStoragesController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Juan Pablo Villafáñez <jvillafanez@solidgear.es>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files_External\Controller;
  29. use OCA\Files_External\NotFoundException;
  30. use OCA\Files_External\Service\GlobalStoragesService;
  31. use OCP\AppFramework\Http;
  32. use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
  33. use OCP\AppFramework\Http\DataResponse;
  34. use OCP\IConfig;
  35. use OCP\IGroupManager;
  36. use OCP\IL10N;
  37. use OCP\IRequest;
  38. use OCP\IUserSession;
  39. use Psr\Log\LoggerInterface;
  40. /**
  41. * Global storages controller
  42. */
  43. class GlobalStoragesController extends StoragesController {
  44. /**
  45. * Creates a new global storages controller.
  46. *
  47. * @param string $AppName application name
  48. * @param IRequest $request request object
  49. * @param IL10N $l10n l10n service
  50. * @param GlobalStoragesService $globalStoragesService storage service
  51. * @param LoggerInterface $logger
  52. * @param IUserSession $userSession
  53. * @param IGroupManager $groupManager
  54. * @param IConfig $config
  55. */
  56. public function __construct(
  57. $AppName,
  58. IRequest $request,
  59. IL10N $l10n,
  60. GlobalStoragesService $globalStoragesService,
  61. LoggerInterface $logger,
  62. IUserSession $userSession,
  63. IGroupManager $groupManager,
  64. IConfig $config
  65. ) {
  66. parent::__construct(
  67. $AppName,
  68. $request,
  69. $l10n,
  70. $globalStoragesService,
  71. $logger,
  72. $userSession,
  73. $groupManager,
  74. $config
  75. );
  76. }
  77. /**
  78. * Create an external storage entry.
  79. *
  80. * @param string $mountPoint storage mount point
  81. * @param string $backend backend identifier
  82. * @param string $authMechanism authentication mechanism identifier
  83. * @param array $backendOptions backend-specific options
  84. * @param array $mountOptions mount-specific options
  85. * @param array $applicableUsers users for which to mount the storage
  86. * @param array $applicableGroups groups for which to mount the storage
  87. * @param int $priority priority
  88. *
  89. * @return DataResponse
  90. */
  91. #[PasswordConfirmationRequired]
  92. public function create(
  93. $mountPoint,
  94. $backend,
  95. $authMechanism,
  96. $backendOptions,
  97. $mountOptions,
  98. $applicableUsers,
  99. $applicableGroups,
  100. $priority
  101. ) {
  102. $canCreateNewLocalStorage = $this->config->getSystemValue('files_external_allow_create_new_local', true);
  103. if (!$canCreateNewLocalStorage && $backend === 'local') {
  104. return new DataResponse(
  105. [
  106. 'message' => $this->l10n->t('Forbidden to manage local mounts')
  107. ],
  108. Http::STATUS_FORBIDDEN
  109. );
  110. }
  111. $newStorage = $this->createStorage(
  112. $mountPoint,
  113. $backend,
  114. $authMechanism,
  115. $backendOptions,
  116. $mountOptions,
  117. $applicableUsers,
  118. $applicableGroups,
  119. $priority
  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 mount-specific options
  144. * @param array $applicableUsers users for which to mount the storage
  145. * @param array $applicableGroups groups for which to mount the storage
  146. * @param int $priority priority
  147. * @param bool $testOnly whether to storage should only test the connection or do more things
  148. *
  149. * @return DataResponse
  150. */
  151. #[PasswordConfirmationRequired]
  152. public function update(
  153. $id,
  154. $mountPoint,
  155. $backend,
  156. $authMechanism,
  157. $backendOptions,
  158. $mountOptions,
  159. $applicableUsers,
  160. $applicableGroups,
  161. $priority,
  162. $testOnly = true
  163. ) {
  164. $storage = $this->createStorage(
  165. $mountPoint,
  166. $backend,
  167. $authMechanism,
  168. $backendOptions,
  169. $mountOptions,
  170. $applicableUsers,
  171. $applicableGroups,
  172. $priority
  173. );
  174. if ($storage instanceof DataResponse) {
  175. return $storage;
  176. }
  177. $storage->setId($id);
  178. $response = $this->validate($storage);
  179. if (!empty($response)) {
  180. return $response;
  181. }
  182. try {
  183. $storage = $this->service->updateStorage($storage);
  184. } catch (NotFoundException $e) {
  185. return new DataResponse(
  186. [
  187. 'message' => $this->l10n->t('Storage with ID "%d" not found', [$id])
  188. ],
  189. Http::STATUS_NOT_FOUND
  190. );
  191. }
  192. $this->updateStorageStatus($storage, $testOnly);
  193. return new DataResponse(
  194. $storage->jsonSerialize(true),
  195. Http::STATUS_OK
  196. );
  197. }
  198. }