globalstoragescontroller.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @author Robin McCorkell <robin@mccorkell.me.uk>
  4. * @author Vincent Petry <pvince81@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\Files_External\Controller;
  23. use \OCP\IConfig;
  24. use OCP\ILogger;
  25. use \OCP\IUserSession;
  26. use \OCP\IRequest;
  27. use \OCP\IL10N;
  28. use \OCP\AppFramework\Http\DataResponse;
  29. use \OCP\AppFramework\Controller;
  30. use \OCP\AppFramework\Http;
  31. use \OCA\Files_external\Service\GlobalStoragesService;
  32. use \OCA\Files_external\NotFoundException;
  33. use \OCA\Files_external\Lib\StorageConfig;
  34. use \OCA\Files_External\Service\BackendService;
  35. /**
  36. * Global storages controller
  37. */
  38. class GlobalStoragesController extends StoragesController {
  39. /**
  40. * Creates a new global storages controller.
  41. *
  42. * @param string $AppName application name
  43. * @param IRequest $request request object
  44. * @param IL10N $l10n l10n service
  45. * @param GlobalStoragesService $globalStoragesService storage service
  46. * @param ILogger $logger
  47. */
  48. public function __construct(
  49. $AppName,
  50. IRequest $request,
  51. IL10N $l10n,
  52. GlobalStoragesService $globalStoragesService,
  53. ILogger $logger
  54. ) {
  55. parent::__construct(
  56. $AppName,
  57. $request,
  58. $l10n,
  59. $globalStoragesService,
  60. $logger
  61. );
  62. }
  63. /**
  64. * Create an external storage entry.
  65. *
  66. * @param string $mountPoint storage mount point
  67. * @param string $backend backend identifier
  68. * @param string $authMechanism authentication mechanism identifier
  69. * @param array $backendOptions backend-specific options
  70. * @param array $mountOptions mount-specific options
  71. * @param array $applicableUsers users for which to mount the storage
  72. * @param array $applicableGroups groups for which to mount the storage
  73. * @param int $priority priority
  74. *
  75. * @return DataResponse
  76. */
  77. public function create(
  78. $mountPoint,
  79. $backend,
  80. $authMechanism,
  81. $backendOptions,
  82. $mountOptions,
  83. $applicableUsers,
  84. $applicableGroups,
  85. $priority
  86. ) {
  87. $newStorage = $this->createStorage(
  88. $mountPoint,
  89. $backend,
  90. $authMechanism,
  91. $backendOptions,
  92. $mountOptions,
  93. $applicableUsers,
  94. $applicableGroups,
  95. $priority
  96. );
  97. if ($newStorage instanceof DataResponse) {
  98. return $newStorage;
  99. }
  100. $response = $this->validate($newStorage);
  101. if (!empty($response)) {
  102. return $response;
  103. }
  104. $newStorage = $this->service->addStorage($newStorage);
  105. $this->updateStorageStatus($newStorage);
  106. return new DataResponse(
  107. $newStorage,
  108. Http::STATUS_CREATED
  109. );
  110. }
  111. /**
  112. * Update an external storage entry.
  113. *
  114. * @param int $id storage id
  115. * @param string $mountPoint storage mount point
  116. * @param string $backend backend identifier
  117. * @param string $authMechanism authentication mechansim identifier
  118. * @param array $backendOptions backend-specific options
  119. * @param array $mountOptions mount-specific options
  120. * @param array $applicableUsers users for which to mount the storage
  121. * @param array $applicableGroups groups for which to mount the storage
  122. * @param int $priority priority
  123. *
  124. * @return DataResponse
  125. */
  126. public function update(
  127. $id,
  128. $mountPoint,
  129. $backend,
  130. $authMechanism,
  131. $backendOptions,
  132. $mountOptions,
  133. $applicableUsers,
  134. $applicableGroups,
  135. $priority
  136. ) {
  137. $storage = $this->createStorage(
  138. $mountPoint,
  139. $backend,
  140. $authMechanism,
  141. $backendOptions,
  142. $mountOptions,
  143. $applicableUsers,
  144. $applicableGroups,
  145. $priority
  146. );
  147. if ($storage instanceof DataResponse) {
  148. return $storage;
  149. }
  150. $storage->setId($id);
  151. $response = $this->validate($storage);
  152. if (!empty($response)) {
  153. return $response;
  154. }
  155. try {
  156. $storage = $this->service->updateStorage($storage);
  157. } catch (NotFoundException $e) {
  158. return new DataResponse(
  159. [
  160. 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id))
  161. ],
  162. Http::STATUS_NOT_FOUND
  163. );
  164. }
  165. $this->updateStorageStatus($storage);
  166. return new DataResponse(
  167. $storage,
  168. Http::STATUS_OK
  169. );
  170. }
  171. }