You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

globalstoragescontroller.php 4.3KB

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