Backends.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. private BackendService $backendService;
  35. public function __construct(BackendService $backendService
  36. ) {
  37. parent::__construct();
  38. $this->backendService = $backendService;
  39. }
  40. protected function configure(): void {
  41. $this
  42. ->setName('files_external:backends')
  43. ->setDescription('Show available authentication and storage backends')
  44. ->addArgument(
  45. 'type',
  46. InputArgument::OPTIONAL,
  47. 'only show backends of a certain type. Possible values are "authentication" or "storage"'
  48. )->addArgument(
  49. 'backend',
  50. InputArgument::OPTIONAL,
  51. 'only show information of a specific backend'
  52. );
  53. parent::configure();
  54. }
  55. protected function execute(InputInterface $input, OutputInterface $output): int {
  56. $authBackends = $this->backendService->getAuthMechanisms();
  57. $storageBackends = $this->backendService->getBackends();
  58. $data = [
  59. 'authentication' => array_map([$this, 'serializeAuthBackend'], $authBackends),
  60. 'storage' => array_map([$this, 'serializeAuthBackend'], $storageBackends)
  61. ];
  62. $type = $input->getArgument('type');
  63. $backend = $input->getArgument('backend');
  64. if ($type) {
  65. if (!isset($data[$type])) {
  66. $output->writeln('<error>Invalid type "' . $type . '". Possible values are "authentication" or "storage"</error>');
  67. return 1;
  68. }
  69. $data = $data[$type];
  70. if ($backend) {
  71. if (!isset($data[$backend])) {
  72. $output->writeln('<error>Unknown backend "' . $backend . '" of type "' . $type . '"</error>');
  73. return 1;
  74. }
  75. $data = $data[$backend];
  76. }
  77. }
  78. $this->writeArrayInOutputFormat($input, $output, $data);
  79. return 0;
  80. }
  81. private function serializeAuthBackend(\JsonSerializable $backend) {
  82. $data = $backend->jsonSerialize();
  83. $result = [
  84. 'name' => $data['name'],
  85. 'identifier' => $data['identifier'],
  86. 'configuration' => $this->formatConfiguration($data['configuration'])
  87. ];
  88. if ($backend instanceof Backend) {
  89. $result['storage_class'] = $backend->getStorageClass();
  90. $authBackends = $this->backendService->getAuthMechanismsByScheme(array_keys($backend->getAuthSchemes()));
  91. $result['supported_authentication_backends'] = array_keys($authBackends);
  92. $authConfig = array_map(function (AuthMechanism $auth) {
  93. return $this->serializeAuthBackend($auth)['configuration'];
  94. }, $authBackends);
  95. $result['authentication_configuration'] = array_combine(array_keys($authBackends), $authConfig);
  96. }
  97. return $result;
  98. }
  99. /**
  100. * @param DefinitionParameter[] $parameters
  101. * @return string[]
  102. */
  103. private function formatConfiguration(array $parameters) {
  104. $configuration = array_filter($parameters, function (DefinitionParameter $parameter) {
  105. return $parameter->getType() !== DefinitionParameter::VALUE_HIDDEN;
  106. });
  107. return array_map(function (DefinitionParameter $parameter) {
  108. return $parameter->getTypeName();
  109. }, $configuration);
  110. }
  111. }