1
0

OCMController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use Exception;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\FrontpageRoute;
  12. use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
  13. use OCP\AppFramework\Http\Attribute\PublicPage;
  14. use OCP\AppFramework\Http\DataResponse;
  15. use OCP\Capabilities\ICapability;
  16. use OCP\IAppConfig;
  17. use OCP\IRequest;
  18. use OCP\Server;
  19. use Psr\Container\ContainerExceptionInterface;
  20. use Psr\Log\LoggerInterface;
  21. /**
  22. * Controller about the endpoint /ocm-provider/
  23. *
  24. * @since 28.0.0
  25. */
  26. class OCMController extends Controller {
  27. public function __construct(
  28. IRequest $request,
  29. private readonly IAppConfig $appConfig,
  30. private LoggerInterface $logger,
  31. ) {
  32. parent::__construct('core', $request);
  33. }
  34. /**
  35. * generate a OCMProvider with local data and send it as DataResponse.
  36. * This replaces the old PHP file ocm-provider/index.php
  37. *
  38. * @psalm-suppress MoreSpecificReturnType
  39. * @psalm-suppress LessSpecificReturnStatement
  40. * @return DataResponse<Http::STATUS_OK, array{enabled: bool, apiVersion: string, endPoint: string, resourceTypes: list<array{name: string, shareTypes: list<string>, protocols: array{webdav: string}}>}, array{X-NEXTCLOUD-OCM-PROVIDERS: true, Content-Type: 'application/json'}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
  41. *
  42. * 200: OCM Provider details returned
  43. * 500: OCM not supported
  44. */
  45. #[PublicPage]
  46. #[NoCSRFRequired]
  47. #[FrontpageRoute(verb: 'GET', url: '/ocm-provider/')]
  48. public function discovery(): DataResponse {
  49. try {
  50. $cap = Server::get(
  51. $this->appConfig->getValueString(
  52. 'core', 'ocm_providers',
  53. \OCA\CloudFederationAPI\Capabilities::class,
  54. lazy: true
  55. )
  56. );
  57. if (!($cap instanceof ICapability)) {
  58. throw new Exception('loaded class does not implements OCP\Capabilities\ICapability');
  59. }
  60. return new DataResponse(
  61. $cap->getCapabilities()['ocm'] ?? ['enabled' => false],
  62. Http::STATUS_OK,
  63. [
  64. 'X-NEXTCLOUD-OCM-PROVIDERS' => true,
  65. 'Content-Type' => 'application/json'
  66. ]
  67. );
  68. } catch (ContainerExceptionInterface|Exception $e) {
  69. $this->logger->error('issue during OCM discovery request', ['exception' => $e]);
  70. return new DataResponse(
  71. ['message' => '/ocm-provider/ not supported'],
  72. Http::STATUS_INTERNAL_SERVER_ERROR
  73. );
  74. }
  75. }
  76. }