1
0

EnableMasterKey.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Encryption\Command;
  8. use OCA\Encryption\Util;
  9. use OCP\IConfig;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Helper\QuestionHelper;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Console\Question\ConfirmationQuestion;
  15. class EnableMasterKey extends Command {
  16. /** @var Util */
  17. protected $util;
  18. /** @var IConfig */
  19. protected $config;
  20. /** @var QuestionHelper */
  21. protected $questionHelper;
  22. /**
  23. * @param Util $util
  24. * @param IConfig $config
  25. * @param QuestionHelper $questionHelper
  26. */
  27. public function __construct(Util $util,
  28. IConfig $config,
  29. QuestionHelper $questionHelper) {
  30. $this->util = $util;
  31. $this->config = $config;
  32. $this->questionHelper = $questionHelper;
  33. parent::__construct();
  34. }
  35. protected function configure() {
  36. $this
  37. ->setName('encryption:enable-master-key')
  38. ->setDescription('Enable the master key. Only available for fresh installations with no existing encrypted data! There is also no way to disable it again.');
  39. }
  40. protected function execute(InputInterface $input, OutputInterface $output): int {
  41. $isAlreadyEnabled = $this->util->isMasterKeyEnabled();
  42. if ($isAlreadyEnabled) {
  43. $output->writeln('Master key already enabled');
  44. } else {
  45. $question = new ConfirmationQuestion(
  46. 'Warning: Only available for fresh installations with no existing encrypted data! '
  47. . 'There is also no way to disable it again. Do you want to continue? (y/n) ', false);
  48. if ($this->questionHelper->ask($input, $output, $question)) {
  49. $this->config->setAppValue('encryption', 'useMasterKey', '1');
  50. $output->writeln('Master key successfully enabled.');
  51. } else {
  52. $output->writeln('aborted.');
  53. return 1;
  54. }
  55. }
  56. return 0;
  57. }
  58. }