UserGlobalStoragesController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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\IRequest;
  42. use OCP\IUserSession;
  43. use Psr\Log\LoggerInterface;
  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 LoggerInterface $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. LoggerInterface $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. /** @var UserGlobalStoragesService */
  89. $service = $this->service;
  90. $storages = array_map(function ($storage) {
  91. // remove configuration data, this must be kept private
  92. $this->sanitizeStorage($storage);
  93. return $storage->jsonSerialize(true);
  94. }, $service->getUniqueStorages());
  95. return new DataResponse(
  96. $storages,
  97. Http::STATUS_OK
  98. );
  99. }
  100. protected function manipulateStorageConfig(StorageConfig $storage) {
  101. /** @var AuthMechanism */
  102. $authMechanism = $storage->getAuthMechanism();
  103. $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser());
  104. /** @var Backend */
  105. $backend = $storage->getBackend();
  106. $backend->manipulateStorageConfig($storage, $this->userSession->getUser());
  107. }
  108. /**
  109. * Get an external storage entry.
  110. *
  111. * @param int $id storage id
  112. * @param bool $testOnly whether to storage should only test the connection or do more things
  113. * @return DataResponse
  114. *
  115. * @NoAdminRequired
  116. */
  117. public function show($id, $testOnly = true) {
  118. try {
  119. $storage = $this->service->getStorage($id);
  120. $this->updateStorageStatus($storage, $testOnly);
  121. } catch (NotFoundException $e) {
  122. return new DataResponse(
  123. [
  124. 'message' => $this->l10n->t('Storage with ID "%d" not found', [$id])
  125. ],
  126. Http::STATUS_NOT_FOUND
  127. );
  128. }
  129. $this->sanitizeStorage($storage);
  130. $data = $storage->jsonSerialize(true);
  131. $isAdmin = $this->groupManager->isAdmin($this->userSession->getUser()->getUID());
  132. $data['can_edit'] = $storage->getType() === StorageConfig::MOUNT_TYPE_PERSONAL || $isAdmin;
  133. return new DataResponse(
  134. $data,
  135. Http::STATUS_OK
  136. );
  137. }
  138. /**
  139. * Update an external storage entry.
  140. * Only allows setting user provided backend fields
  141. *
  142. * @param int $id storage id
  143. * @param array $backendOptions backend-specific options
  144. * @param bool $testOnly whether to storage should only test the connection or do more things
  145. *
  146. * @return DataResponse
  147. *
  148. * @NoAdminRequired
  149. */
  150. public function update(
  151. $id,
  152. $backendOptions,
  153. $testOnly = true
  154. ) {
  155. try {
  156. $storage = $this->service->getStorage($id);
  157. $authMechanism = $storage->getAuthMechanism();
  158. if ($authMechanism instanceof IUserProvided || $authMechanism instanceof UserGlobalAuth) {
  159. $authMechanism->saveBackendOptions($this->userSession->getUser(), $id, $backendOptions);
  160. $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser());
  161. } else {
  162. return new DataResponse(
  163. [
  164. 'message' => $this->l10n->t('Storage with ID "%d" is not editable by non-admins', [$id])
  165. ],
  166. Http::STATUS_FORBIDDEN
  167. );
  168. }
  169. } catch (NotFoundException $e) {
  170. return new DataResponse(
  171. [
  172. 'message' => $this->l10n->t('Storage with ID "%d" not found', [$id])
  173. ],
  174. Http::STATUS_NOT_FOUND
  175. );
  176. }
  177. $this->updateStorageStatus($storage, $testOnly);
  178. $this->sanitizeStorage($storage);
  179. return new DataResponse(
  180. $storage->jsonSerialize(true),
  181. Http::STATUS_OK
  182. );
  183. }
  184. /**
  185. * Remove sensitive data from a StorageConfig before returning it to the user
  186. *
  187. * @param StorageConfig $storage
  188. */
  189. protected function sanitizeStorage(StorageConfig $storage) {
  190. $storage->setBackendOptions([]);
  191. $storage->setMountOptions([]);
  192. if ($storage->getAuthMechanism() instanceof IUserProvided) {
  193. try {
  194. $storage->getAuthMechanism()->manipulateStorageConfig($storage, $this->userSession->getUser());
  195. } catch (InsufficientDataForMeaningfulAnswerException $e) {
  196. // not configured yet
  197. }
  198. }
  199. }
  200. }