Completed
Push — master ( bcc1a5...3a44cc )
by Björn
19:20
created

RecoverUser::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 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
namespace OCA\Encryption\Command;
23
24
25
use OC\Files\Filesystem;
26
use OC\User\NoUserException;
27
use OCA\Encryption\Crypto\Crypt;
28
use OCA\Encryption\KeyManager;
29
use OCA\Encryption\Recovery;
30
use OCA\Encryption\Util;
31
use OCP\IConfig;
32
use OCP\IUserManager;
33
use Symfony\Component\Console\Command\Command;
34
use Symfony\Component\Console\Helper\QuestionHelper;
35
use Symfony\Component\Console\Input\InputArgument;
36
use Symfony\Component\Console\Input\InputInterface;
37
use Symfony\Component\Console\Output\OutputInterface;
38
use Symfony\Component\Console\Question\Question;
39
40
class RecoverUser extends Command {
41
42
	/** @var Util */
43
	protected $util;
44
45
	/** @var IUserManager */
46
	protected $userManager;
47
48
	/** @var  QuestionHelper */
49
	protected $questionHelper;
50
51
	/**
52
	 * @param Util $util
53
	 * @param IConfig $config
54
	 * @param IUserManager $userManager
55
	 * @param QuestionHelper $questionHelper
56
	 */
57
	public function __construct(Util $util,
58
								IConfig $config,
59
								IUserManager $userManager,
60
								QuestionHelper $questionHelper) {
61
62
		$this->util = $util;
63
		$this->questionHelper = $questionHelper;
64
		$this->userManager = $userManager;
65
		parent::__construct();
66
	}
67
68
	protected function configure() {
69
		$this
70
			->setName('encryption:recover-user')
71
			->setDescription('Recover user data in case of password lost. This only works if the user enabled the recovery key.');
72
73
		$this->addArgument(
74
			'user',
75
			InputArgument::REQUIRED,
76
			'user which should be recovered'
77
		);
78
	}
79
80
	protected function execute(InputInterface $input, OutputInterface $output) {
81
82
		$isMasterKeyEnabled = $this->util->isMasterKeyEnabled();
83
84
		if($isMasterKeyEnabled) {
85
			$output->writeln('You use the master key, no individual user recovery needed.');
86
			return;
87
		}
88
89
		$uid = $input->getArgument('user');
90
		$userExists = $this->userManager->userExists($uid);
91
		if ($userExists === false) {
92
			$output->writeln('User "' . $uid . '" unknown.');
93
			return;
94
		}
95
96
		$recoveryKeyEnabled = $this->util->isRecoveryEnabledForUser($uid);
97
		if($recoveryKeyEnabled === false) {
98
			$output->writeln('Recovery key is not enabled for: ' . $uid);
99
			return;
100
		}
101
102
		$question = new Question('Please enter the recovery key password: ');
103
		$question->setHidden(true);
104
		$question->setHiddenFallback(false);
105
		$recoveryPassword = $this->questionHelper->ask($input, $output, $question);
106
107
		$question = new Question('Please enter the new login password for the user: ');
108
		$question->setHidden(true);
109
		$question->setHiddenFallback(false);
110
		$newLoginPassword = $this->questionHelper->ask($input, $output, $question);
111
112
		$output->write('Start to recover users files... This can take some time...');
113
		$this->userManager->get($uid)->setPassword($newLoginPassword, $recoveryPassword);
114
		$output->writeln('Done.');
115
116
	}
117
118
}
119