userstoragescontroller.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 OCA\Files_External\Lib\Auth\AuthMechanism;
  24. use \OCP\IConfig;
  25. use OCP\ILogger;
  26. use OCP\IUser;
  27. use \OCP\IUserSession;
  28. use \OCP\IRequest;
  29. use \OCP\IL10N;
  30. use \OCP\AppFramework\Http\DataResponse;
  31. use \OCP\AppFramework\Controller;
  32. use \OCP\AppFramework\Http;
  33. use \OCA\Files_external\Service\UserStoragesService;
  34. use \OCA\Files_External\Service\BackendService;
  35. use \OCA\Files_external\NotFoundException;
  36. use \OCA\Files_external\Lib\StorageConfig;
  37. use \OCA\Files_External\Lib\Backend\Backend;
  38. /**
  39. * User storages controller
  40. */
  41. class UserStoragesController extends StoragesController {
  42. /**
  43. * @var IUserSession
  44. */
  45. private $userSession;
  46. /**
  47. * Creates a new user storages controller.
  48. *
  49. * @param string $AppName application name
  50. * @param IRequest $request request object
  51. * @param IL10N $l10n l10n service
  52. * @param UserStoragesService $userStoragesService storage service
  53. * @param IUserSession $userSession
  54. * @param ILogger $logger
  55. */
  56. public function __construct(
  57. $AppName,
  58. IRequest $request,
  59. IL10N $l10n,
  60. UserStoragesService $userStoragesService,
  61. IUserSession $userSession,
  62. ILogger $logger
  63. ) {
  64. parent::__construct(
  65. $AppName,
  66. $request,
  67. $l10n,
  68. $userStoragesService,
  69. $logger
  70. );
  71. $this->userSession = $userSession;
  72. }
  73. protected function manipulateStorageConfig(StorageConfig $storage) {
  74. /** @var AuthMechanism */
  75. $authMechanism = $storage->getAuthMechanism();
  76. $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser());
  77. /** @var Backend */
  78. $backend = $storage->getBackend();
  79. $backend->manipulateStorageConfig($storage, $this->userSession->getUser());
  80. }
  81. /**
  82. * Get all storage entries
  83. *
  84. * @NoAdminRequired
  85. *
  86. * @return DataResponse
  87. */
  88. public function index() {
  89. return parent::index();
  90. }
  91. /**
  92. * Return storage
  93. *
  94. * @NoAdminRequired
  95. *
  96. * {@inheritdoc}
  97. */
  98. public function show($id) {
  99. return parent::show($id);
  100. }
  101. /**
  102. * Create an external storage entry.
  103. *
  104. * @param string $mountPoint storage mount point
  105. * @param string $backend backend identifier
  106. * @param string $authMechanism authentication mechanism identifier
  107. * @param array $backendOptions backend-specific options
  108. * @param array $mountOptions backend-specific mount options
  109. *
  110. * @return DataResponse
  111. *
  112. * @NoAdminRequired
  113. */
  114. public function create(
  115. $mountPoint,
  116. $backend,
  117. $authMechanism,
  118. $backendOptions,
  119. $mountOptions
  120. ) {
  121. $newStorage = $this->createStorage(
  122. $mountPoint,
  123. $backend,
  124. $authMechanism,
  125. $backendOptions,
  126. $mountOptions
  127. );
  128. if ($newStorage instanceOf DataResponse) {
  129. return $newStorage;
  130. }
  131. $response = $this->validate($newStorage);
  132. if (!empty($response)) {
  133. return $response;
  134. }
  135. $newStorage = $this->service->addStorage($newStorage);
  136. $this->updateStorageStatus($newStorage);
  137. return new DataResponse(
  138. $newStorage,
  139. Http::STATUS_CREATED
  140. );
  141. }
  142. /**
  143. * Update an external storage entry.
  144. *
  145. * @param int $id storage id
  146. * @param string $mountPoint storage mount point
  147. * @param string $backend backend identifier
  148. * @param string $authMechanism authentication mechanism identifier
  149. * @param array $backendOptions backend-specific options
  150. * @param array $mountOptions backend-specific mount options
  151. *
  152. * @return DataResponse
  153. *
  154. * @NoAdminRequired
  155. */
  156. public function update(
  157. $id,
  158. $mountPoint,
  159. $backend,
  160. $authMechanism,
  161. $backendOptions,
  162. $mountOptions
  163. ) {
  164. $storage = $this->createStorage(
  165. $mountPoint,
  166. $backend,
  167. $authMechanism,
  168. $backendOptions,
  169. $mountOptions
  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' => (string)$this->l10n->t('Storage with id "%i" not found', array($id))
  185. ],
  186. Http::STATUS_NOT_FOUND
  187. );
  188. }
  189. $this->updateStorageStatus($storage);
  190. return new DataResponse(
  191. $storage,
  192. Http::STATUS_OK
  193. );
  194. }
  195. /**
  196. * Delete storage
  197. *
  198. * @NoAdminRequired
  199. *
  200. * {@inheritdoc}
  201. */
  202. public function destroy($id) {
  203. return parent::destroy($id);
  204. }
  205. }