Backends.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  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, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_External\Command;
  25. use OC\Core\Command\Base;
  26. use OCA\Files_External\Lib\Auth\AuthMechanism;
  27. use OCA\Files_External\Lib\Backend\Backend;
  28. use OCA\Files_External\Lib\DefinitionParameter;
  29. use OCA\Files_External\Service\BackendService;
  30. use Symfony\Component\Console\Input\InputArgument;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. class Backends extends Base {
  34. public function __construct(
  35. private BackendService $backendService,
  36. ) {
  37. parent::__construct();
  38. }
  39. protected function configure(): void {
  40. $this
  41. ->setName('files_external:backends')
  42. ->setDescription('Show available authentication and storage backends')
  43. ->addArgument(
  44. 'type',
  45. InputArgument::OPTIONAL,
  46. 'only show backends of a certain type. Possible values are "authentication" or "storage"'
  47. )->addArgument(
  48. 'backend',
  49. InputArgument::OPTIONAL,
  50. 'only show information of a specific backend'
  51. );
  52. parent::configure();
  53. }
  54. protected function execute(InputInterface $input, OutputInterface $output): int {
  55. $authBackends = $this->backendService->getAuthMechanisms();
  56. $storageBackends = $this->backendService->getBackends();
  57. $data = [
  58. 'authentication' => array_map([$this, 'serializeAuthBackend'], $authBackends),
  59. 'storage' => array_map([$this, 'serializeAuthBackend'], $storageBackends)
  60. ];
  61. $type = $input->getArgument('type');
  62. $backend = $input->getArgument('backend');
  63. if ($type) {
  64. if (!isset($data[$type])) {
  65. $output->writeln('<error>Invalid type "' . $type . '". Possible values are "authentication" or "storage"</error>');
  66. return self::FAILURE;
  67. }
  68. $data = $data[$type];
  69. if ($backend) {
  70. if (!isset($data[$backend])) {
  71. $output->writeln('<error>Unknown backend "' . $backend . '" of type "' . $type . '"</error>');
  72. return self::FAILURE;
  73. }
  74. $data = $data[$backend];
  75. }
  76. }
  77. $this->writeArrayInOutputFormat($input, $output, $data);
  78. return self::SUCCESS;
  79. }
  80. private function serializeAuthBackend(\JsonSerializable $backend): array {
  81. $data = $backend->jsonSerialize();
  82. $result = [
  83. 'name' => $data['name'],
  84. 'identifier' => $data['identifier'],
  85. 'configuration' => $this->formatConfiguration($data['configuration'])
  86. ];
  87. if ($backend instanceof Backend) {
  88. $result['storage_class'] = $backend->getStorageClass();
  89. $authBackends = $this->backendService->getAuthMechanismsByScheme(array_keys($backend->getAuthSchemes()));
  90. $result['supported_authentication_backends'] = array_keys($authBackends);
  91. $authConfig = array_map(function (AuthMechanism $auth) {
  92. return $this->serializeAuthBackend($auth)['configuration'];
  93. }, $authBackends);
  94. $result['authentication_configuration'] = array_combine(array_keys($authBackends), $authConfig);
  95. }
  96. return $result;
  97. }
  98. /**
  99. * @param DefinitionParameter[] $parameters
  100. * @return string[]
  101. */
  102. private function formatConfiguration(array $parameters): array {
  103. $configuration = array_filter($parameters, function (DefinitionParameter $parameter) {
  104. return $parameter->getType() !== DefinitionParameter::VALUE_HIDDEN;
  105. });
  106. return array_map(function (DefinitionParameter $parameter) {
  107. return $parameter->getTypeName();
  108. }, $configuration);
  109. }
  110. }