1
0

ApiController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\Files_External\Controller;
  9. use OCA\Files_External\Lib\StorageConfig;
  10. use OCA\Files_External\ResponseDefinitions;
  11. use OCA\Files_External\Service\UserGlobalStoragesService;
  12. use OCA\Files_External\Service\UserStoragesService;
  13. use OCP\AppFramework\Http;
  14. use OCP\AppFramework\Http\Attribute\OpenAPI;
  15. use OCP\AppFramework\Http\DataResponse;
  16. use OCP\AppFramework\OCSController;
  17. use OCP\IRequest;
  18. /**
  19. * @psalm-import-type Files_ExternalMount from ResponseDefinitions
  20. */
  21. class ApiController extends OCSController {
  22. private UserGlobalStoragesService $userGlobalStoragesService;
  23. private UserStoragesService $userStoragesService;
  24. public function __construct(
  25. string $appName,
  26. IRequest $request,
  27. UserGlobalStoragesService $userGlobalStorageService,
  28. UserStoragesService $userStorageService
  29. ) {
  30. parent::__construct($appName, $request);
  31. $this->userGlobalStoragesService = $userGlobalStorageService;
  32. $this->userStoragesService = $userStorageService;
  33. }
  34. /**
  35. * Formats the given mount config to a mount entry.
  36. *
  37. * @param string $mountPoint mount point name, relative to the data dir
  38. * @param StorageConfig $mountConfig mount config to format
  39. *
  40. * @return Files_ExternalMount
  41. */
  42. private function formatMount(string $mountPoint, StorageConfig $mountConfig): array {
  43. // split path from mount point
  44. $path = \dirname($mountPoint);
  45. if ($path === '.' || $path === '/') {
  46. $path = '';
  47. }
  48. $isSystemMount = $mountConfig->getType() === StorageConfig::MOUNT_TYPE_ADMIN;
  49. $permissions = \OCP\Constants::PERMISSION_READ;
  50. // personal mounts can be deleted
  51. if (!$isSystemMount) {
  52. $permissions |= \OCP\Constants::PERMISSION_DELETE;
  53. }
  54. $entry = [
  55. 'id' => $mountConfig->getId(),
  56. 'type' => 'dir',
  57. 'name' => basename($mountPoint),
  58. 'path' => $path,
  59. 'permissions' => $permissions,
  60. 'scope' => $isSystemMount ? 'system' : 'personal',
  61. 'backend' => $mountConfig->getBackend()->getText(),
  62. 'class' => $mountConfig->getBackend()->getIdentifier(),
  63. 'config' => $mountConfig->jsonSerialize(true),
  64. ];
  65. return $entry;
  66. }
  67. /**
  68. * @NoAdminRequired
  69. *
  70. * Get the mount points visible for this user
  71. *
  72. * @return DataResponse<Http::STATUS_OK, Files_ExternalMount[], array{}>
  73. *
  74. * 200: User mounts returned
  75. */
  76. public function getUserMounts(): DataResponse {
  77. $entries = [];
  78. $mountPoints = [];
  79. foreach ($this->userGlobalStoragesService->getStorages() as $storage) {
  80. $mountPoint = $storage->getMountPoint();
  81. $mountPoints[$mountPoint] = $storage;
  82. }
  83. foreach ($this->userStoragesService->getStorages() as $storage) {
  84. $mountPoint = $storage->getMountPoint();
  85. $mountPoints[$mountPoint] = $storage;
  86. }
  87. foreach ($mountPoints as $mountPoint => $mount) {
  88. $entries[] = $this->formatMount($mountPoint, $mount);
  89. }
  90. return new DataResponse($entries);
  91. }
  92. /**
  93. * @NoAdminRequired
  94. * @NoCSRFRequired
  95. *
  96. * Ask for credentials using a browser's native basic auth prompt
  97. * Then returns it if provided
  98. */
  99. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  100. public function askNativeAuth(): DataResponse {
  101. if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
  102. $response = new DataResponse([], Http::STATUS_UNAUTHORIZED);
  103. $response->addHeader('WWW-Authenticate', 'Basic realm="Storage authentification needed"');
  104. return $response;
  105. }
  106. $user = $_SERVER['PHP_AUTH_USER'];
  107. $password = $_SERVER['PHP_AUTH_PW'];
  108. // Reset auth
  109. unset($_SERVER['PHP_AUTH_USER']);
  110. unset($_SERVER['PHP_AUTH_PW']);
  111. // Using 401 again to ensure we clear any cached Authorization
  112. return new DataResponse([
  113. 'user' => $user,
  114. 'password' => $password,
  115. ], Http::STATUS_UNAUTHORIZED);
  116. }
  117. }