GlobalStoragesController.php 5.3 KB

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