OCMController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Maxence Lange <maxence@artificial-owl.com>
  5. *
  6. * @author Maxence Lange <maxence@artificial-owl.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Controller;
  25. use Exception;
  26. use OCP\AppFramework\Controller;
  27. use OCP\AppFramework\Http;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\Capabilities\ICapability;
  30. use OCP\IConfig;
  31. use OCP\IRequest;
  32. use OCP\Server;
  33. use Psr\Container\ContainerExceptionInterface;
  34. use Psr\Log\LoggerInterface;
  35. /**
  36. * Controller about the endpoint /ocm-provider/
  37. *
  38. * @since 28.0.0
  39. */
  40. class OCMController extends Controller {
  41. public function __construct(
  42. IRequest $request,
  43. private IConfig $config,
  44. private LoggerInterface $logger
  45. ) {
  46. parent::__construct('core', $request);
  47. }
  48. /**
  49. * generate a OCMProvider with local data and send it as DataResponse.
  50. * This replaces the old PHP file ocm-provider/index.php
  51. *
  52. * @PublicPage
  53. * @NoCSRFRequired
  54. *
  55. * @return DataResponse
  56. *
  57. * 200: OCM Provider details returned
  58. * 500: OCM not supported
  59. */
  60. public function discovery(): DataResponse {
  61. try {
  62. $cap = Server::get(
  63. $this->config->getAppValue(
  64. 'core',
  65. 'ocm_providers',
  66. '\OCA\CloudFederationAPI\Capabilities'
  67. )
  68. );
  69. if (!($cap instanceof ICapability)) {
  70. throw new Exception('loaded class does not implements OCP\Capabilities\ICapability');
  71. }
  72. return new DataResponse(
  73. $cap->getCapabilities()['ocm'] ?? ['enabled' => false],
  74. Http::STATUS_OK,
  75. [
  76. 'X-NEXTCLOUD-OCM-PROVIDERS' => true,
  77. 'Content-Type' => 'application/json'
  78. ]
  79. );
  80. } catch (ContainerExceptionInterface|Exception $e) {
  81. $this->logger->error('issue during OCM discovery request', ['exception' => $e]);
  82. return new DataResponse(
  83. ['message' => '/ocm-provider/ not supported'],
  84. Http::STATUS_INTERNAL_SERVER_ERROR
  85. );
  86. }
  87. }
  88. }