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

DecryptAll::forceMaintenanceAndTrashbin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 6
loc 6
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 davitol <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Sergio Bertolín <[email protected]>
9
 * @author Vincent Petry <[email protected]>
10
 *
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OC\Core\Command\Encryption;
28
29
use OCP\App\IAppManager;
30
use OCP\Encryption\IManager;
31
use OCP\IConfig;
32
use Symfony\Component\Console\Command\Command;
33
use Symfony\Component\Console\Helper\QuestionHelper;
34
use Symfony\Component\Console\Input\InputArgument;
35
use Symfony\Component\Console\Input\InputInterface;
36
use Symfony\Component\Console\Output\OutputInterface;
37
use Symfony\Component\Console\Question\ConfirmationQuestion;
38
39
class DecryptAll extends Command {
40
41
	/** @var IManager */
42
	protected $encryptionManager;
43
44
	/** @var  IAppManager */
45
	protected $appManager;
46
47
	/** @var IConfig */
48
	protected $config;
49
50
	/** @var  QuestionHelper */
51
	protected $questionHelper;
52
53
	/** @var bool */
54
	protected $wasTrashbinEnabled;
55
56
	/** @var  bool */
57
	protected $wasMaintenanceModeEnabled;
58
59
	/** @var \OC\Encryption\DecryptAll */
60
	protected $decryptAll;
61
62
	/**
63
	 * @param IManager $encryptionManager
64
	 * @param IAppManager $appManager
65
	 * @param IConfig $config
66
	 * @param \OC\Encryption\DecryptAll $decryptAll
67
	 * @param QuestionHelper $questionHelper
68
	 */
69 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...
70
		IManager $encryptionManager,
71
		IAppManager $appManager,
72
		IConfig $config,
73
		\OC\Encryption\DecryptAll $decryptAll,
74
		QuestionHelper $questionHelper
75
	) {
76
		parent::__construct();
77
78
		$this->appManager = $appManager;
79
		$this->encryptionManager = $encryptionManager;
80
		$this->config = $config;
81
		$this->decryptAll = $decryptAll;
82
		$this->questionHelper = $questionHelper;
83
	}
84
85
	/**
86
	 * Set maintenance mode and disable the trashbin app
87
	 */
88 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...
89
		$this->wasTrashbinEnabled = $this->appManager->isEnabledForUser('files_trashbin');
90
		$this->wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
91
		$this->config->setSystemValue('maintenance', true);
92
		$this->appManager->disableApp('files_trashbin');
93
	}
94
95
	/**
96
	 * Reset the maintenance mode and re-enable the trashbin app
97
	 */
98
	protected function resetMaintenanceAndTrashbin() {
99
		$this->config->setSystemValue('maintenance', $this->wasMaintenanceModeEnabled);
100
		if ($this->wasTrashbinEnabled) {
101
			$this->appManager->enableApp('files_trashbin');
102
		}
103
	}
104
105
	protected function configure() {
106
		parent::configure();
107
108
		$this->setName('encryption:decrypt-all');
109
		$this->setDescription('Disable server-side encryption and decrypt all files');
110
		$this->setHelp(
111
			'This will disable server-side encryption and decrypt all files for '
112
			. 'all users if it is supported by your encryption module. '
113
			. 'Please make sure that no user access his files during this process!'
114
		);
115
		$this->addArgument(
116
			'user',
117
			InputArgument::OPTIONAL,
118
			'user for which you want to decrypt all files (optional)',
119
			''
120
		);
121
	}
122
123
	protected function execute(InputInterface $input, OutputInterface $output) {
124
125
		try {
126
			if ($this->encryptionManager->isEnabled() === true) {
127
				$output->write('Disable server side encryption... ');
128
				$this->config->setAppValue('core', 'encryption_enabled', 'no');
129
				$output->writeln('done.');
130
			} else {
131
				$output->writeln('Server side encryption not enabled. Nothing to do.');
132
				return;
133
			}
134
135
			$uid = $input->getArgument('user');
136
			if ($uid === '') {
137
				$message = 'your Nextcloud';
138
			} else {
139
				$message = "$uid's account";
140
			}
141
142
			$output->writeln("\n");
143
			$output->writeln("You are about to start to decrypt all files stored in $message.");
144
			$output->writeln('It will depend on the encryption module and your setup if this is possible.');
145
			$output->writeln('Depending on the number and size of your files this can take some time');
146
			$output->writeln('Please make sure that no user access his files during this process!');
147
			$output->writeln('');
148
			$question = new ConfirmationQuestion('Do you really want to continue? (y/n) ', false);
149
			if ($this->questionHelper->ask($input, $output, $question)) {
150
				$this->forceMaintenanceAndTrashbin();
151
				$user = $input->getArgument('user');
152
				$result = $this->decryptAll->decryptAll($input, $output, $user);
153
				if ($result === false) {
154
					$output->writeln(' aborted.');
155
					$output->writeln('Server side encryption remains enabled');
156
					$this->config->setAppValue('core', 'encryption_enabled', 'yes');
157
				} else if ($uid !== '') {
158
					$output->writeln('Server side encryption remains enabled');
159
					$this->config->setAppValue('core', 'encryption_enabled', 'yes');
160
				}
161
				$this->resetMaintenanceAndTrashbin();
162
			} else {
163
				$output->write('Enable server side encryption... ');
164
				$this->config->setAppValue('core', 'encryption_enabled', 'yes');
165
				$output->writeln('done.');
166
				$output->writeln('aborted');
167
			}
168
		} catch (\Exception $e) {
169
			// enable server side encryption again if something went wrong
170
			$this->config->setAppValue('core', 'encryption_enabled', 'yes');
171
			$this->resetMaintenanceAndTrashbin();
172
			throw $e;
173
		}
174
175
	}
176
}
177