Completed
Push — master ( 97b6c1...b26568 )
by Joas
12:33
created

EncryptAll::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OC\Core\Command\Encryption;
25
26
use OCP\App\IAppManager;
27
use OCP\Encryption\IManager;
28
use OCP\IConfig;
29
use Symfony\Component\Console\Command\Command;
30
use Symfony\Component\Console\Helper\QuestionHelper;
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Output\OutputInterface;
33
use Symfony\Component\Console\Question\ConfirmationQuestion;
34
35
class EncryptAll extends Command {
36
37
	/** @var IManager */
38
	protected $encryptionManager;
39
40
	/** @var  IAppManager */
41
	protected $appManager;
42
43
	/** @var IConfig */
44
	protected $config;
45
46
	/** @var  QuestionHelper */
47
	protected $questionHelper;
48
49
	/** @var bool */
50
	protected $wasTrashbinEnabled;
51
52
	/** @var  bool */
53
	protected $wasMaintenanceModeEnabled;
54
55
	/**
56
	 * @param IManager $encryptionManager
57
	 * @param IAppManager $appManager
58
	 * @param IConfig $config
59
	 * @param QuestionHelper $questionHelper
60
	 */
61 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
		IManager $encryptionManager,
63
		IAppManager $appManager,
64
		IConfig $config,
65
		QuestionHelper $questionHelper
66
	) {
67
		parent::__construct();
68
		$this->appManager = $appManager;
69
		$this->encryptionManager = $encryptionManager;
70
		$this->config = $config;
71
		$this->questionHelper = $questionHelper;
72
	}
73
74
	/**
75
	 * Set maintenance mode and disable the trashbin app
76
	 */
77 View Code Duplication
	protected function forceMaintenanceAndTrashbin() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
		$this->wasTrashbinEnabled = $this->appManager->isEnabledForUser('files_trashbin');
79
		$this->wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
80
		$this->config->setSystemValue('maintenance', true);
81
		$this->appManager->disableApp('files_trashbin');
82
	}
83
84
	/**
85
	 * Reset the maintenance mode and re-enable the trashbin app
86
	 */
87
	protected function resetMaintenanceAndTrashbin() {
88
		$this->config->setSystemValue('maintenance', $this->wasMaintenanceModeEnabled);
89
		if ($this->wasTrashbinEnabled) {
90
			$this->appManager->enableApp('files_trashbin');
91
		}
92
	}
93
94
	protected function configure() {
95
		parent::configure();
96
97
		$this->setName('encryption:encrypt-all');
98
		$this->setDescription('Encrypt all files for all users');
99
		$this->setHelp(
100
			'This will encrypt all files for all users. '
101
			. 'Please make sure that no user access his files during this process!'
102
		);
103
	}
104
105
	protected function execute(InputInterface $input, OutputInterface $output) {
106
107
		if ($this->encryptionManager->isEnabled() === false) {
108
			throw new \Exception('Server side encryption is not enabled');
109
		}
110
111
		$output->writeln("\n");
112
		$output->writeln('You are about to start to encrypt all files stored in your ownCloud.');
113
		$output->writeln('It will depend on the encryption module you use which files get encrypted.');
114
		$output->writeln('Depending on the number and size of your files this can take some time');
115
		$output->writeln('Please make sure that no user access his files during this process!');
116
		$output->writeln('');
117
		$question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false);
118
		if ($this->questionHelper->ask($input, $output, $question)) {
119
			$this->forceMaintenanceAndTrashbin();
120
121
			try {
122
				$defaultModule = $this->encryptionManager->getEncryptionModule();
123
				$defaultModule->encryptAll($input, $output);
124
			} catch (\Exception $ex) {
125
				$this->resetMaintenanceAndTrashbin();
126
				throw $ex;
127
			}
128
129
			$this->resetMaintenanceAndTrashbin();
130
		} else {
131
			$output->writeln('aborted');
132
		}
133
	}
134
135
}
136