ListModules.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 OC\Core\Command\Encryption;
  8. use OC\Core\Command\Base;
  9. use OCP\Encryption\IManager;
  10. use OCP\IConfig;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class ListModules extends Base {
  14. public function __construct(
  15. protected IManager $encryptionManager,
  16. protected IConfig $config,
  17. ) {
  18. parent::__construct();
  19. }
  20. protected function configure() {
  21. parent::configure();
  22. $this
  23. ->setName('encryption:list-modules')
  24. ->setDescription('List all available encryption modules')
  25. ;
  26. }
  27. protected function execute(InputInterface $input, OutputInterface $output): int {
  28. $isMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
  29. if ($isMaintenanceModeEnabled) {
  30. $output->writeln("Maintenance mode must be disabled when listing modules");
  31. $output->writeln("in order to list the relevant encryption modules correctly.");
  32. return 1;
  33. }
  34. $encryptionModules = $this->encryptionManager->getEncryptionModules();
  35. $defaultEncryptionModuleId = $this->encryptionManager->getDefaultEncryptionModuleId();
  36. $encModules = [];
  37. foreach ($encryptionModules as $module) {
  38. $encModules[$module['id']]['displayName'] = $module['displayName'];
  39. $encModules[$module['id']]['default'] = $module['id'] === $defaultEncryptionModuleId;
  40. }
  41. $this->writeModuleList($input, $output, $encModules);
  42. return 0;
  43. }
  44. /**
  45. * @param InputInterface $input
  46. * @param OutputInterface $output
  47. * @param array $items
  48. */
  49. protected function writeModuleList(InputInterface $input, OutputInterface $output, $items) {
  50. if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) {
  51. array_walk($items, function (&$item) {
  52. if (!$item['default']) {
  53. $item = $item['displayName'];
  54. } else {
  55. $item = $item['displayName'] . ' [default*]';
  56. }
  57. });
  58. }
  59. $this->writeArrayInOutputFormat($input, $output, $items);
  60. }
  61. }