OCMController.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\DataResponse;
  13. use OCP\Capabilities\ICapability;
  14. use OCP\IConfig;
  15. use OCP\IRequest;
  16. use OCP\Server;
  17. use Psr\Container\ContainerExceptionInterface;
  18. use Psr\Log\LoggerInterface;
  19. /**
  20. * Controller about the endpoint /ocm-provider/
  21. *
  22. * @since 28.0.0
  23. */
  24. class OCMController extends Controller {
  25. public function __construct(
  26. IRequest $request,
  27. private IConfig $config,
  28. private LoggerInterface $logger
  29. ) {
  30. parent::__construct('core', $request);
  31. }
  32. /**
  33. * generate a OCMProvider with local data and send it as DataResponse.
  34. * This replaces the old PHP file ocm-provider/index.php
  35. *
  36. * @PublicPage
  37. * @NoCSRFRequired
  38. * @psalm-suppress MoreSpecificReturnType
  39. * @psalm-suppress LessSpecificReturnStatement
  40. * @return DataResponse<Http::STATUS_OK, array{enabled: bool, apiVersion: string, endPoint: string, resourceTypes: array{name: string, shareTypes: 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. #[FrontpageRoute(verb: 'GET', url: '/ocm-provider/')]
  46. public function discovery(): DataResponse {
  47. try {
  48. $cap = Server::get(
  49. $this->config->getAppValue(
  50. 'core',
  51. 'ocm_providers',
  52. '\OCA\CloudFederationAPI\Capabilities'
  53. )
  54. );
  55. if (!($cap instanceof ICapability)) {
  56. throw new Exception('loaded class does not implements OCP\Capabilities\ICapability');
  57. }
  58. return new DataResponse(
  59. $cap->getCapabilities()['ocm'] ?? ['enabled' => false],
  60. Http::STATUS_OK,
  61. [
  62. 'X-NEXTCLOUD-OCM-PROVIDERS' => true,
  63. 'Content-Type' => 'application/json'
  64. ]
  65. );
  66. } catch (ContainerExceptionInterface|Exception $e) {
  67. $this->logger->error('issue during OCM discovery request', ['exception' => $e]);
  68. return new DataResponse(
  69. ['message' => '/ocm-provider/ not supported'],
  70. Http::STATUS_INTERNAL_SERVER_ERROR
  71. );
  72. }
  73. }
  74. }