1
0

UserGlobalStoragesController.php 5.9 KB

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