1
0

Backends.php 4.1 KB

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