ListModules.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 Ruben Homs <ruben@homs.codes>
  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 OC\Core\Command\Encryption;
  25. use OC\Core\Command\Base;
  26. use OCP\Encryption\IManager;
  27. use OCP\IConfig;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. class ListModules extends Base {
  31. public function __construct(
  32. protected IManager $encryptionManager,
  33. protected IConfig $config,
  34. ) {
  35. parent::__construct();
  36. }
  37. protected function configure() {
  38. parent::configure();
  39. $this
  40. ->setName('encryption:list-modules')
  41. ->setDescription('List all available encryption modules')
  42. ;
  43. }
  44. protected function execute(InputInterface $input, OutputInterface $output): int {
  45. $isMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
  46. if ($isMaintenanceModeEnabled) {
  47. $output->writeln("Maintenance mode must be disabled when listing modules");
  48. $output->writeln("in order to list the relevant encryption modules correctly.");
  49. return 1;
  50. }
  51. $encryptionModules = $this->encryptionManager->getEncryptionModules();
  52. $defaultEncryptionModuleId = $this->encryptionManager->getDefaultEncryptionModuleId();
  53. $encModules = [];
  54. foreach ($encryptionModules as $module) {
  55. $encModules[$module['id']]['displayName'] = $module['displayName'];
  56. $encModules[$module['id']]['default'] = $module['id'] === $defaultEncryptionModuleId;
  57. }
  58. $this->writeModuleList($input, $output, $encModules);
  59. return 0;
  60. }
  61. /**
  62. * @param InputInterface $input
  63. * @param OutputInterface $output
  64. * @param array $items
  65. */
  66. protected function writeModuleList(InputInterface $input, OutputInterface $output, $items) {
  67. if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) {
  68. array_walk($items, function (&$item) {
  69. if (!$item['default']) {
  70. $item = $item['displayName'];
  71. } else {
  72. $item = $item['displayName'] . ' [default*]';
  73. }
  74. });
  75. }
  76. $this->writeArrayInOutputFormat($input, $output, $items);
  77. }
  78. }