SetDefaultModule.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 OCP\Encryption\IManager;
  9. use OCP\IConfig;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. class SetDefaultModule extends Command {
  15. public function __construct(
  16. protected IManager $encryptionManager,
  17. protected IConfig $config,
  18. ) {
  19. parent::__construct();
  20. }
  21. protected function configure() {
  22. parent::configure();
  23. $this
  24. ->setName('encryption:set-default-module')
  25. ->setDescription('Set the encryption default module')
  26. ->addArgument(
  27. 'module',
  28. InputArgument::REQUIRED,
  29. 'ID of the encryption module that should be used'
  30. )
  31. ;
  32. }
  33. protected function execute(InputInterface $input, OutputInterface $output): int {
  34. $isMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
  35. if ($isMaintenanceModeEnabled) {
  36. $output->writeln("Maintenance mode must be disabled when setting default module,");
  37. $output->writeln("in order to load the relevant encryption modules correctly.");
  38. return 1;
  39. }
  40. $moduleId = $input->getArgument('module');
  41. if ($moduleId === $this->encryptionManager->getDefaultEncryptionModuleId()) {
  42. $output->writeln('"' . $moduleId . '"" is already the default module');
  43. } elseif ($this->encryptionManager->setDefaultEncryptionModule($moduleId)) {
  44. $output->writeln('<info>Set default module to "' . $moduleId . '"</info>');
  45. } else {
  46. $output->writeln('<error>The specified module "' . $moduleId . '" does not exist</error>');
  47. return 1;
  48. }
  49. return 0;
  50. }
  51. }