userglobalstoragescontroller.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * @author Robin McCorkell <robin@mccorkell.me.uk>
  4. *
  5. * @copyright Copyright (c) 2016, 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 OCA\Files_External\Lib\Auth\AuthMechanism;
  23. use OCA\Files_External\Lib\Auth\IUserProvided;
  24. use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
  25. use OCP\ILogger;
  26. use \OCP\IRequest;
  27. use \OCP\IL10N;
  28. use \OCP\AppFramework\Http\DataResponse;
  29. use \OCP\AppFramework\Http;
  30. use \OCA\Files_external\Service\UserGlobalStoragesService;
  31. use \OCA\Files_external\NotFoundException;
  32. use \OCA\Files_external\Lib\StorageConfig;
  33. use \OCA\Files_External\Lib\Backend\Backend;
  34. use OCP\IUserSession;
  35. /**
  36. * User global storages controller
  37. */
  38. class UserGlobalStoragesController extends StoragesController {
  39. /**
  40. * @var IUserSession
  41. */
  42. private $userSession;
  43. /**
  44. * Creates a new user global storages controller.
  45. *
  46. * @param string $AppName application name
  47. * @param IRequest $request request object
  48. * @param IL10N $l10n l10n service
  49. * @param UserGlobalStoragesService $userGlobalStoragesService storage service
  50. * @param IUserSession $userSession
  51. */
  52. public function __construct(
  53. $AppName,
  54. IRequest $request,
  55. IL10N $l10n,
  56. UserGlobalStoragesService $userGlobalStoragesService,
  57. IUserSession $userSession,
  58. ILogger $logger
  59. ) {
  60. parent::__construct(
  61. $AppName,
  62. $request,
  63. $l10n,
  64. $userGlobalStoragesService,
  65. $logger
  66. );
  67. $this->userSession = $userSession;
  68. }
  69. /**
  70. * Get all storage entries
  71. *
  72. * @return DataResponse
  73. *
  74. * @NoAdminRequired
  75. */
  76. public function index() {
  77. $storages = $this->service->getUniqueStorages();
  78. // remove configuration data, this must be kept private
  79. foreach ($storages as $storage) {
  80. $this->sanitizeStorage($storage);
  81. }
  82. return new DataResponse(
  83. $storages,
  84. Http::STATUS_OK
  85. );
  86. }
  87. protected function manipulateStorageConfig(StorageConfig $storage) {
  88. /** @var AuthMechanism */
  89. $authMechanism = $storage->getAuthMechanism();
  90. $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser());
  91. /** @var Backend */
  92. $backend = $storage->getBackend();
  93. $backend->manipulateStorageConfig($storage, $this->userSession->getUser());
  94. }
  95. /**
  96. * Get an external storage entry.
  97. *
  98. * @param int $id storage id
  99. * @return DataResponse
  100. *
  101. * @NoAdminRequired
  102. */
  103. public function show($id) {
  104. try {
  105. $storage = $this->service->getStorage($id);
  106. $this->updateStorageStatus($storage);
  107. } catch (NotFoundException $e) {
  108. return new DataResponse(
  109. [
  110. 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id))
  111. ],
  112. Http::STATUS_NOT_FOUND
  113. );
  114. }
  115. $this->sanitizeStorage($storage);
  116. return new DataResponse(
  117. $storage,
  118. Http::STATUS_OK
  119. );
  120. }
  121. /**
  122. * Update an external storage entry.
  123. * Only allows setting user provided backend fields
  124. *
  125. * @param int $id storage id
  126. * @param array $backendOptions backend-specific options
  127. *
  128. * @return DataResponse
  129. *
  130. * @NoAdminRequired
  131. */
  132. public function update(
  133. $id,
  134. $backendOptions
  135. ) {
  136. try {
  137. $storage = $this->service->getStorage($id);
  138. $authMechanism = $storage->getAuthMechanism();
  139. if ($authMechanism instanceof IUserProvided) {
  140. $authMechanism->saveBackendOptions($this->userSession->getUser(), $id, $backendOptions);
  141. $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser());
  142. } else {
  143. return new DataResponse(
  144. [
  145. 'message' => (string)$this->l10n->t('Storage with id "%i" is not user editable', array($id))
  146. ],
  147. Http::STATUS_FORBIDDEN
  148. );
  149. }
  150. } catch (NotFoundException $e) {
  151. return new DataResponse(
  152. [
  153. 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id))
  154. ],
  155. Http::STATUS_NOT_FOUND
  156. );
  157. }
  158. $this->updateStorageStatus($storage);
  159. $this->sanitizeStorage($storage);
  160. return new DataResponse(
  161. $storage,
  162. Http::STATUS_OK
  163. );
  164. }
  165. /**
  166. * Remove sensitive data from a StorageConfig before returning it to the user
  167. *
  168. * @param StorageConfig $storage
  169. */
  170. protected function sanitizeStorage(StorageConfig $storage) {
  171. $storage->setBackendOptions([]);
  172. $storage->setMountOptions([]);
  173. if ($storage->getAuthMechanism() instanceof IUserProvided) {
  174. try {
  175. $storage->getAuthMechanism()->manipulateStorageConfig($storage, $this->userSession->getUser());
  176. } catch (InsufficientDataForMeaningfulAnswerException $e) {
  177. // not configured yet
  178. }
  179. }
  180. }
  181. }