1
0

OCMController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\Attribute\FrontpageRoute;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\Capabilities\ICapability;
  31. use OCP\IConfig;
  32. use OCP\IRequest;
  33. use OCP\Server;
  34. use Psr\Container\ContainerExceptionInterface;
  35. use Psr\Log\LoggerInterface;
  36. /**
  37. * Controller about the endpoint /ocm-provider/
  38. *
  39. * @since 28.0.0
  40. */
  41. class OCMController extends Controller {
  42. public function __construct(
  43. IRequest $request,
  44. private IConfig $config,
  45. private LoggerInterface $logger
  46. ) {
  47. parent::__construct('core', $request);
  48. }
  49. /**
  50. * generate a OCMProvider with local data and send it as DataResponse.
  51. * This replaces the old PHP file ocm-provider/index.php
  52. *
  53. * @PublicPage
  54. * @NoCSRFRequired
  55. * @psalm-suppress MoreSpecificReturnType
  56. * @psalm-suppress LessSpecificReturnStatement
  57. * @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{}>
  58. *
  59. * 200: OCM Provider details returned
  60. * 500: OCM not supported
  61. */
  62. #[FrontpageRoute(verb: 'GET', url: '/ocm-provider/')]
  63. public function discovery(): DataResponse {
  64. try {
  65. $cap = Server::get(
  66. $this->config->getAppValue(
  67. 'core',
  68. 'ocm_providers',
  69. '\OCA\CloudFederationAPI\Capabilities'
  70. )
  71. );
  72. if (!($cap instanceof ICapability)) {
  73. throw new Exception('loaded class does not implements OCP\Capabilities\ICapability');
  74. }
  75. return new DataResponse(
  76. $cap->getCapabilities()['ocm'] ?? ['enabled' => false],
  77. Http::STATUS_OK,
  78. [
  79. 'X-NEXTCLOUD-OCM-PROVIDERS' => true,
  80. 'Content-Type' => 'application/json'
  81. ]
  82. );
  83. } catch (ContainerExceptionInterface|Exception $e) {
  84. $this->logger->error('issue during OCM discovery request', ['exception' => $e]);
  85. return new DataResponse(
  86. ['message' => '/ocm-provider/ not supported'],
  87. Http::STATUS_INTERNAL_SERVER_ERROR
  88. );
  89. }
  90. }
  91. }