Completed
Pull Request — master (#5174)
by Björn
17:50
created

DisableMasterKey::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 16

Duplication

Lines 6
Ratio 27.27 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 2
dl 6
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Bjoern Schiessle <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
23
namespace OCA\Encryption\Command;
24
25
26
use OCA\Encryption\Util;
27
use OCP\IConfig;
28
use Symfony\Component\Console\Command\Command;
29
use Symfony\Component\Console\Helper\QuestionHelper;
30
use Symfony\Component\Console\Input\InputInterface;
31
use Symfony\Component\Console\Output\OutputInterface;
32
use Symfony\Component\Console\Question\ConfirmationQuestion;
33
34
class DisableMasterKey extends Command {
35
36
	/** @var Util */
37
	protected $util;
38
39
	/** @var IConfig */
40
	protected $config;
41
42
	/** @var  QuestionHelper */
43
	protected $questionHelper;
44
45
	/**
46
	 * @param Util $util
47
	 * @param IConfig $config
48
	 * @param QuestionHelper $questionHelper
49
	 */
50 View Code Duplication
	public function __construct(Util $util,
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...
51
								IConfig $config,
52
								QuestionHelper $questionHelper) {
53
54
		$this->util = $util;
55
		$this->config = $config;
56
		$this->questionHelper = $questionHelper;
57
		parent::__construct();
58
	}
59
60
	protected function configure() {
61
		$this
62
			->setName('encryption:disable-master-key')
63
			->setDescription('Disable the master key and use per-user keys instead. Only available for fresh installations with no existing encrypted data! There is no way to enable it again.');
64
	}
65
66
	protected function execute(InputInterface $input, OutputInterface $output) {
67
68
		$isMasterKeyEnabled = $this->util->isMasterKeyEnabled();
69
70
		if(!$isMasterKeyEnabled) {
71
			$output->writeln('Master key already disabled');
72
		} else {
73
			$question = new ConfirmationQuestion(
74
				'Warning: Only perform this operation for a fresh installations with no existing encrypted data! '
75
				. 'There is no way to enable the master key again. '
76
				. 'We strongly recommend to keep the master key, it provides significant performance improvements '
77
				. 'and is easier to handle for both, users and administrators. '
78
				. 'Do you really want to switch to per-user keys? (y/n) ', false);
79 View Code Duplication
			if ($this->questionHelper->ask($input, $output, $question)) {
80
				$this->config->setAppValue('encryption', 'useMasterKey', '0');
81
				$output->writeln('Master key successfully disabled.');
82
			} else {
83
				$output->writeln('aborted.');
84
			}
85
		}
86
87
	}
88
89
}
90