EnableMasterKey.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. public function __construct(
  17. protected Util $util,
  18. protected IConfig $config,
  19. protected QuestionHelper $questionHelper,
  20. ) {
  21. parent::__construct();
  22. }
  23. protected function configure(): void {
  24. $this
  25. ->setName('encryption:enable-master-key')
  26. ->setDescription('Enable the master key. Only available for fresh installations with no existing encrypted data! There is also no way to disable it again.');
  27. }
  28. protected function execute(InputInterface $input, OutputInterface $output): int {
  29. $isAlreadyEnabled = $this->util->isMasterKeyEnabled();
  30. if ($isAlreadyEnabled) {
  31. $output->writeln('Master key already enabled');
  32. return self::SUCCESS;
  33. }
  34. $question = new ConfirmationQuestion(
  35. 'Warning: Only available for fresh installations with no existing encrypted data! '
  36. . 'There is also no way to disable it again. Do you want to continue? (y/n) ', false);
  37. if ($this->questionHelper->ask($input, $output, $question)) {
  38. $this->config->setAppValue('encryption', 'useMasterKey', '1');
  39. $output->writeln('Master key successfully enabled.');
  40. return self::SUCCESS;
  41. }
  42. $output->writeln('aborted.');
  43. return self::FAILURE;
  44. }
  45. }