ListModules.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Core\Command\Encryption;
  23. use OC\Core\Command\Base;
  24. use OCP\Encryption\IManager;
  25. use OCP\IConfig;
  26. use Symfony\Component\Console\Input\InputInterface;
  27. use Symfony\Component\Console\Output\OutputInterface;
  28. class ListModules extends Base {
  29. /** @var IManager */
  30. protected $encryptionManager;
  31. /** @var IConfig */
  32. protected $config;
  33. /**
  34. * @param IManager $encryptionManager
  35. * @param IConfig $config
  36. */
  37. public function __construct(
  38. IManager $encryptionManager,
  39. IConfig $config
  40. ) {
  41. parent::__construct();
  42. $this->encryptionManager = $encryptionManager;
  43. $this->config = $config;
  44. }
  45. protected function configure() {
  46. parent::configure();
  47. $this
  48. ->setName('encryption:list-modules')
  49. ->setDescription('List all available encryption modules')
  50. ;
  51. }
  52. protected function execute(InputInterface $input, OutputInterface $output) {
  53. $isMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
  54. if ($isMaintenanceModeEnabled) {
  55. $output->writeln("Maintenance mode must be disabled when listing modules");
  56. $output->writeln("in order to list the relevant encryption modules correctly.");
  57. return;
  58. }
  59. $encryptionModules = $this->encryptionManager->getEncryptionModules();
  60. $defaultEncryptionModuleId = $this->encryptionManager->getDefaultEncryptionModuleId();
  61. $encModules = array();
  62. foreach ($encryptionModules as $module) {
  63. $encModules[$module['id']]['displayName'] = $module['displayName'];
  64. $encModules[$module['id']]['default'] = $module['id'] === $defaultEncryptionModuleId;
  65. }
  66. $this->writeModuleList($input, $output, $encModules);
  67. }
  68. /**
  69. * @param InputInterface $input
  70. * @param OutputInterface $output
  71. * @param array $items
  72. */
  73. protected function writeModuleList(InputInterface $input, OutputInterface $output, $items) {
  74. if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) {
  75. array_walk($items, function(&$item) {
  76. if (!$item['default']) {
  77. $item = $item['displayName'];
  78. } else {
  79. $item = $item['displayName'] . ' [default*]';
  80. }
  81. });
  82. }
  83. $this->writeArrayInOutputFormat($input, $output, $items);
  84. }
  85. }