UserGlobalStoragesController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 Robin Appelman <robin@icewind.nl>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_External\Controller;
  26. use OCA\Files_External\Lib\Auth\AuthMechanism;
  27. use OCA\Files_External\Lib\Auth\IUserProvided;
  28. use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
  29. use OCP\ILogger;
  30. use \OCP\IRequest;
  31. use \OCP\IL10N;
  32. use \OCP\AppFramework\Http\DataResponse;
  33. use \OCP\AppFramework\Http;
  34. use OCA\Files_External\Service\UserGlobalStoragesService;
  35. use OCA\Files_External\NotFoundException;
  36. use OCA\Files_External\Lib\StorageConfig;
  37. use \OCA\Files_External\Lib\Backend\Backend;
  38. use OCP\IUserSession;
  39. /**
  40. * User global storages controller
  41. */
  42. class UserGlobalStoragesController extends StoragesController {
  43. /**
  44. * @var IUserSession
  45. */
  46. private $userSession;
  47. /**
  48. * Creates a new user global storages controller.
  49. *
  50. * @param string $AppName application name
  51. * @param IRequest $request request object
  52. * @param IL10N $l10n l10n service
  53. * @param UserGlobalStoragesService $userGlobalStoragesService storage service
  54. * @param IUserSession $userSession
  55. */
  56. public function __construct(
  57. $AppName,
  58. IRequest $request,
  59. IL10N $l10n,
  60. UserGlobalStoragesService $userGlobalStoragesService,
  61. IUserSession $userSession,
  62. ILogger $logger
  63. ) {
  64. parent::__construct(
  65. $AppName,
  66. $request,
  67. $l10n,
  68. $userGlobalStoragesService,
  69. $logger
  70. );
  71. $this->userSession = $userSession;
  72. }
  73. /**
  74. * Get all storage entries
  75. *
  76. * @return DataResponse
  77. *
  78. * @NoAdminRequired
  79. */
  80. public function index() {
  81. $storages = $this->service->getUniqueStorages();
  82. // remove configuration data, this must be kept private
  83. foreach ($storages as $storage) {
  84. $this->sanitizeStorage($storage);
  85. }
  86. return new DataResponse(
  87. $storages,
  88. Http::STATUS_OK
  89. );
  90. }
  91. protected function manipulateStorageConfig(StorageConfig $storage) {
  92. /** @var AuthMechanism */
  93. $authMechanism = $storage->getAuthMechanism();
  94. $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser());
  95. /** @var Backend */
  96. $backend = $storage->getBackend();
  97. $backend->manipulateStorageConfig($storage, $this->userSession->getUser());
  98. }
  99. /**
  100. * Get an external storage entry.
  101. *
  102. * @param int $id storage id
  103. * @param bool $testOnly whether to storage should only test the connection or do more things
  104. * @return DataResponse
  105. *
  106. * @NoAdminRequired
  107. */
  108. public function show($id, $testOnly = true) {
  109. try {
  110. $storage = $this->service->getStorage($id);
  111. $this->updateStorageStatus($storage, $testOnly);
  112. } catch (NotFoundException $e) {
  113. return new DataResponse(
  114. [
  115. 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id))
  116. ],
  117. Http::STATUS_NOT_FOUND
  118. );
  119. }
  120. $this->sanitizeStorage($storage);
  121. return new DataResponse(
  122. $storage,
  123. Http::STATUS_OK
  124. );
  125. }
  126. /**
  127. * Update an external storage entry.
  128. * Only allows setting user provided backend fields
  129. *
  130. * @param int $id storage id
  131. * @param array $backendOptions backend-specific options
  132. * @param bool $testOnly whether to storage should only test the connection or do more things
  133. *
  134. * @return DataResponse
  135. *
  136. * @NoAdminRequired
  137. */
  138. public function update(
  139. $id,
  140. $backendOptions,
  141. $testOnly = true
  142. ) {
  143. try {
  144. $storage = $this->service->getStorage($id);
  145. $authMechanism = $storage->getAuthMechanism();
  146. if ($authMechanism instanceof IUserProvided) {
  147. $authMechanism->saveBackendOptions($this->userSession->getUser(), $id, $backendOptions);
  148. $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser());
  149. } else {
  150. return new DataResponse(
  151. [
  152. 'message' => (string)$this->l10n->t('Storage with id "%i" is not user editable', array($id))
  153. ],
  154. Http::STATUS_FORBIDDEN
  155. );
  156. }
  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, $testOnly);
  166. $this->sanitizeStorage($storage);
  167. return new DataResponse(
  168. $storage,
  169. Http::STATUS_OK
  170. );
  171. }
  172. /**
  173. * Remove sensitive data from a StorageConfig before returning it to the user
  174. *
  175. * @param StorageConfig $storage
  176. */
  177. protected function sanitizeStorage(StorageConfig $storage) {
  178. $storage->setBackendOptions([]);
  179. $storage->setMountOptions([]);
  180. if ($storage->getAuthMechanism() instanceof IUserProvided) {
  181. try {
  182. $storage->getAuthMechanism()->manipulateStorageConfig($storage, $this->userSession->getUser());
  183. } catch (InsufficientDataForMeaningfulAnswerException $e) {
  184. // not configured yet
  185. }
  186. }
  187. }
  188. }