ListModules.php 2.8 KB

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