UserStoragesController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Juan Pablo Villafáñez <jvillafanez@solidgear.es>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Robin McCorkell <robin@mccorkell.me.uk>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Files_External\Controller;
  30. use OCA\Files_External\Lib\Auth\AuthMechanism;
  31. use OCA\Files_External\Lib\Backend\Backend;
  32. use OCA\Files_External\Lib\StorageConfig;
  33. use OCA\Files_External\NotFoundException;
  34. use OCA\Files_External\Service\UserStoragesService;
  35. use OCP\AppFramework\Http;
  36. use OCP\AppFramework\Http\DataResponse;
  37. use OCP\IConfig;
  38. use OCP\IGroupManager;
  39. use OCP\IL10N;
  40. use OCP\ILogger;
  41. use OCP\IRequest;
  42. use OCP\IUserSession;
  43. /**
  44. * User storages controller
  45. */
  46. class UserStoragesController extends StoragesController {
  47. /**
  48. * Creates a new user storages controller.
  49. *
  50. * @param string $AppName application name
  51. * @param IRequest $request request object
  52. * @param IL10N $l10n l10n service
  53. * @param UserStoragesService $userStoragesService storage service
  54. * @param ILogger $logger
  55. * @param IUserSession $userSession
  56. * @param IGroupManager $groupManager
  57. */
  58. public function __construct(
  59. $AppName,
  60. IRequest $request,
  61. IL10N $l10n,
  62. UserStoragesService $userStoragesService,
  63. ILogger $logger,
  64. IUserSession $userSession,
  65. IGroupManager $groupManager,
  66. IConfig $config
  67. ) {
  68. parent::__construct(
  69. $AppName,
  70. $request,
  71. $l10n,
  72. $userStoragesService,
  73. $logger,
  74. $userSession,
  75. $groupManager,
  76. $config
  77. );
  78. }
  79. protected function manipulateStorageConfig(StorageConfig $storage) {
  80. /** @var AuthMechanism */
  81. $authMechanism = $storage->getAuthMechanism();
  82. $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser());
  83. /** @var Backend */
  84. $backend = $storage->getBackend();
  85. $backend->manipulateStorageConfig($storage, $this->userSession->getUser());
  86. }
  87. /**
  88. * Get all storage entries
  89. *
  90. * @NoAdminRequired
  91. *
  92. * @return DataResponse
  93. */
  94. public function index() {
  95. return parent::index();
  96. }
  97. /**
  98. * Return storage
  99. *
  100. * @NoAdminRequired
  101. *
  102. * {@inheritdoc}
  103. */
  104. public function show($id, $testOnly = true) {
  105. return parent::show($id, $testOnly);
  106. }
  107. /**
  108. * Create an external storage entry.
  109. *
  110. * @param string $mountPoint storage mount point
  111. * @param string $backend backend identifier
  112. * @param string $authMechanism authentication mechanism identifier
  113. * @param array $backendOptions backend-specific options
  114. * @param array $mountOptions backend-specific mount options
  115. *
  116. * @return DataResponse
  117. *
  118. * @NoAdminRequired
  119. */
  120. public function create(
  121. $mountPoint,
  122. $backend,
  123. $authMechanism,
  124. $backendOptions,
  125. $mountOptions
  126. ) {
  127. $canCreateNewLocalStorage = $this->config->getSystemValue('files_external_allow_create_new_local', true);
  128. if (!$canCreateNewLocalStorage && $backend === 'local') {
  129. return new DataResponse(
  130. [
  131. 'message' => $this->l10n->t('Forbidden to manage local mounts')
  132. ],
  133. Http::STATUS_FORBIDDEN
  134. );
  135. }
  136. $newStorage = $this->createStorage(
  137. $mountPoint,
  138. $backend,
  139. $authMechanism,
  140. $backendOptions,
  141. $mountOptions
  142. );
  143. if ($newStorage instanceof DataResponse) {
  144. return $newStorage;
  145. }
  146. $response = $this->validate($newStorage);
  147. if (!empty($response)) {
  148. return $response;
  149. }
  150. $newStorage = $this->service->addStorage($newStorage);
  151. $this->updateStorageStatus($newStorage);
  152. return new DataResponse(
  153. $this->formatStorageForUI($newStorage),
  154. Http::STATUS_CREATED
  155. );
  156. }
  157. /**
  158. * Update an external storage entry.
  159. *
  160. * @param int $id storage id
  161. * @param string $mountPoint storage mount point
  162. * @param string $backend backend identifier
  163. * @param string $authMechanism authentication mechanism identifier
  164. * @param array $backendOptions backend-specific options
  165. * @param array $mountOptions backend-specific mount options
  166. * @param bool $testOnly whether to storage should only test the connection or do more things
  167. *
  168. * @return DataResponse
  169. *
  170. * @NoAdminRequired
  171. */
  172. public function update(
  173. $id,
  174. $mountPoint,
  175. $backend,
  176. $authMechanism,
  177. $backendOptions,
  178. $mountOptions,
  179. $testOnly = true
  180. ) {
  181. $storage = $this->createStorage(
  182. $mountPoint,
  183. $backend,
  184. $authMechanism,
  185. $backendOptions,
  186. $mountOptions
  187. );
  188. if ($storage instanceof DataResponse) {
  189. return $storage;
  190. }
  191. $storage->setId($id);
  192. $response = $this->validate($storage);
  193. if (!empty($response)) {
  194. return $response;
  195. }
  196. try {
  197. $storage = $this->service->updateStorage($storage);
  198. } catch (NotFoundException $e) {
  199. return new DataResponse(
  200. [
  201. 'message' => $this->l10n->t('Storage with ID "%d" not found', [$id])
  202. ],
  203. Http::STATUS_NOT_FOUND
  204. );
  205. }
  206. $this->updateStorageStatus($storage, $testOnly);
  207. return new DataResponse(
  208. $this->formatStorageForUI($storage),
  209. Http::STATUS_OK
  210. );
  211. }
  212. /**
  213. * Delete storage
  214. *
  215. * @NoAdminRequired
  216. *
  217. * {@inheritdoc}
  218. */
  219. public function destroy($id) {
  220. return parent::destroy($id);
  221. }
  222. }