OCMController.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. * @psalm-suppress MoreSpecificReturnType
  55. * @psalm-suppress LessSpecificReturnStatement
  56. * @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{}>
  57. *
  58. * 200: OCM Provider details returned
  59. * 500: OCM not supported
  60. */
  61. public function discovery(): DataResponse {
  62. try {
  63. $cap = Server::get(
  64. $this->config->getAppValue(
  65. 'core',
  66. 'ocm_providers',
  67. '\OCA\CloudFederationAPI\Capabilities'
  68. )
  69. );
  70. if (!($cap instanceof ICapability)) {
  71. throw new Exception('loaded class does not implements OCP\Capabilities\ICapability');
  72. }
  73. return new DataResponse(
  74. $cap->getCapabilities()['ocm'] ?? ['enabled' => false],
  75. Http::STATUS_OK,
  76. [
  77. 'X-NEXTCLOUD-OCM-PROVIDERS' => true,
  78. 'Content-Type' => 'application/json'
  79. ]
  80. );
  81. } catch (ContainerExceptionInterface|Exception $e) {
  82. $this->logger->error('issue during OCM discovery request', ['exception' => $e]);
  83. return new DataResponse(
  84. ['message' => '/ocm-provider/ not supported'],
  85. Http::STATUS_INTERNAL_SERVER_ERROR
  86. );
  87. }
  88. }
  89. }