EncryptAll.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\App\IAppManager;
  9. use OCP\Encryption\IManager;
  10. use OCP\IConfig;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Helper\QuestionHelper;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Question\ConfirmationQuestion;
  16. class EncryptAll extends Command {
  17. protected bool $wasTrashbinEnabled = false;
  18. protected bool $wasMaintenanceModeEnabled = false;
  19. public function __construct(
  20. protected IManager $encryptionManager,
  21. protected IAppManager $appManager,
  22. protected IConfig $config,
  23. protected QuestionHelper $questionHelper,
  24. ) {
  25. parent::__construct();
  26. }
  27. /**
  28. * Set maintenance mode and disable the trashbin app
  29. */
  30. protected function forceMaintenanceAndTrashbin(): void {
  31. $this->wasTrashbinEnabled = (bool)$this->appManager->isEnabledForUser('files_trashbin');
  32. $this->wasMaintenanceModeEnabled = $this->config->getSystemValueBool('maintenance');
  33. $this->config->setSystemValue('maintenance', true);
  34. $this->appManager->disableApp('files_trashbin');
  35. }
  36. /**
  37. * Reset the maintenance mode and re-enable the trashbin app
  38. */
  39. protected function resetMaintenanceAndTrashbin(): void {
  40. $this->config->setSystemValue('maintenance', $this->wasMaintenanceModeEnabled);
  41. if ($this->wasTrashbinEnabled) {
  42. $this->appManager->enableApp('files_trashbin');
  43. }
  44. }
  45. protected function configure() {
  46. parent::configure();
  47. $this->setName('encryption:encrypt-all');
  48. $this->setDescription('Encrypt all files for all users');
  49. $this->setHelp(
  50. 'This will encrypt all files for all users. '
  51. . 'Please make sure that no user access his files during this process!'
  52. );
  53. }
  54. protected function execute(InputInterface $input, OutputInterface $output): int {
  55. if (!$input->isInteractive()) {
  56. $output->writeln('Invalid TTY.');
  57. $output->writeln('If you are trying to execute the command in a Docker ');
  58. $output->writeln("container, do not forget to execute 'docker exec' with");
  59. $output->writeln("the '-i' and '-t' options.");
  60. $output->writeln('');
  61. return 1;
  62. }
  63. if ($this->encryptionManager->isEnabled() === false) {
  64. throw new \Exception('Server side encryption is not enabled');
  65. }
  66. $output->writeln("\n");
  67. $output->writeln('You are about to encrypt all files stored in your Nextcloud installation.');
  68. $output->writeln('Depending on the number of available files, and their size, this may take quite some time.');
  69. $output->writeln('Please ensure that no user accesses their files during this time!');
  70. $output->writeln('Note: The encryption module you use determines which files get encrypted.');
  71. $output->writeln('');
  72. $question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false);
  73. if ($this->questionHelper->ask($input, $output, $question)) {
  74. $this->forceMaintenanceAndTrashbin();
  75. try {
  76. $defaultModule = $this->encryptionManager->getEncryptionModule();
  77. $defaultModule->encryptAll($input, $output);
  78. } catch (\Exception $ex) {
  79. $this->resetMaintenanceAndTrashbin();
  80. throw $ex;
  81. }
  82. $this->resetMaintenanceAndTrashbin();
  83. return 0;
  84. }
  85. $output->writeln('aborted');
  86. return 1;
  87. }
  88. }