ListModules.php 2.9 KB

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