1
0

Backends.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Command;
  8. use OC\Core\Command\Base;
  9. use OCA\Files_External\Lib\Auth\AuthMechanism;
  10. use OCA\Files_External\Lib\Backend\Backend;
  11. use OCA\Files_External\Lib\DefinitionParameter;
  12. use OCA\Files_External\Service\BackendService;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class Backends extends Base {
  17. public function __construct(
  18. private BackendService $backendService,
  19. ) {
  20. parent::__construct();
  21. }
  22. protected function configure(): void {
  23. $this
  24. ->setName('files_external:backends')
  25. ->setDescription('Show available authentication and storage backends')
  26. ->addArgument(
  27. 'type',
  28. InputArgument::OPTIONAL,
  29. 'only show backends of a certain type. Possible values are "authentication" or "storage"'
  30. )->addArgument(
  31. 'backend',
  32. InputArgument::OPTIONAL,
  33. 'only show information of a specific backend'
  34. );
  35. parent::configure();
  36. }
  37. protected function execute(InputInterface $input, OutputInterface $output): int {
  38. $authBackends = $this->backendService->getAuthMechanisms();
  39. $storageBackends = $this->backendService->getBackends();
  40. $data = [
  41. 'authentication' => array_map([$this, 'serializeAuthBackend'], $authBackends),
  42. 'storage' => array_map([$this, 'serializeAuthBackend'], $storageBackends)
  43. ];
  44. $type = $input->getArgument('type');
  45. $backend = $input->getArgument('backend');
  46. if ($type) {
  47. if (!isset($data[$type])) {
  48. $output->writeln('<error>Invalid type "' . $type . '". Possible values are "authentication" or "storage"</error>');
  49. return self::FAILURE;
  50. }
  51. $data = $data[$type];
  52. if ($backend) {
  53. if (!isset($data[$backend])) {
  54. $output->writeln('<error>Unknown backend "' . $backend . '" of type "' . $type . '"</error>');
  55. return self::FAILURE;
  56. }
  57. $data = $data[$backend];
  58. }
  59. }
  60. $this->writeArrayInOutputFormat($input, $output, $data);
  61. return self::SUCCESS;
  62. }
  63. private function serializeAuthBackend(\JsonSerializable $backend): array {
  64. $data = $backend->jsonSerialize();
  65. $result = [
  66. 'name' => $data['name'],
  67. 'identifier' => $data['identifier'],
  68. 'configuration' => $this->formatConfiguration($data['configuration'])
  69. ];
  70. if ($backend instanceof Backend) {
  71. $result['storage_class'] = $backend->getStorageClass();
  72. $authBackends = $this->backendService->getAuthMechanismsByScheme(array_keys($backend->getAuthSchemes()));
  73. $result['supported_authentication_backends'] = array_keys($authBackends);
  74. $authConfig = array_map(function (AuthMechanism $auth) {
  75. return $this->serializeAuthBackend($auth)['configuration'];
  76. }, $authBackends);
  77. $result['authentication_configuration'] = array_combine(array_keys($authBackends), $authConfig);
  78. }
  79. return $result;
  80. }
  81. /**
  82. * @param DefinitionParameter[] $parameters
  83. * @return string[]
  84. */
  85. private function formatConfiguration(array $parameters): array {
  86. $configuration = array_filter($parameters, function (DefinitionParameter $parameter) {
  87. return $parameter->getType() !== DefinitionParameter::VALUE_HIDDEN;
  88. });
  89. return array_map(function (DefinitionParameter $parameter) {
  90. return $parameter->getTypeName();
  91. }, $configuration);
  92. }
  93. }