Completed
Push — master ( 812bae...fa8bfe )
by Thomas
13:53
created

ResetPassword   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 81
rs 10
c 1
b 0
f 0
wmc 11
lcom 1
cbo 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 17 1
C execute() 0 52 9
1
<?php
2
/**
3
 * @author Andreas Fischer <[email protected]>
4
 * @author Christopher Schäpers <[email protected]>
5
 * @author Clark Tomlinson <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Laurens Post <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 *
10
 * @copyright Copyright (c) 2016, ownCloud GmbH.
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\User;
28
29
use OCP\IUserManager;
30
use Symfony\Component\Console\Command\Command;
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Input\InputArgument;
33
use Symfony\Component\Console\Input\InputOption;
34
use Symfony\Component\Console\Output\OutputInterface;
35
use Symfony\Component\Console\Question\Question;
36
37
class ResetPassword extends Command {
38
39
	/** @var IUserManager */
40
	protected $userManager;
41
42
	public function __construct(IUserManager $userManager) {
43
		$this->userManager = $userManager;
44
		parent::__construct();
45
	}
46
47
	protected function configure() {
48
		$this
49
			->setName('user:resetpassword')
50
			->setDescription('Resets the password of the named user')
51
			->addArgument(
52
				'user',
53
				InputArgument::REQUIRED,
54
				'Username to reset password'
55
			)
56
			->addOption(
57
				'password-from-env',
58
				null,
59
				InputOption::VALUE_NONE,
60
				'read password from environment variable OC_PASS'
61
			)
62
		;
63
	}
64
65
	protected function execute(InputInterface $input, OutputInterface $output) {
66
		$username = $input->getArgument('user');
67
68
		/** @var $user \OCP\IUser */
69
		$user = $this->userManager->get($username);
70
		if (is_null($user)) {
71
			$output->writeln('<error>User does not exist</error>');
72
			return 1;
73
		}
74
75
		if ($input->getOption('password-from-env')) {
76
			$password = getenv('OC_PASS');
77
			if (!$password) {
78
				$output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>');
79
				return 1;
80
			}
81
		} elseif ($input->isInteractive()) {
82
			/** @var $dialog \Symfony\Component\Console\Helper\QuestionHelper */
83
			$dialog = $this->getHelperSet()->get('question');
84
85
			if (\OCP\App::isEnabled('encryption')) {
86
				$output->writeln(
87
					'<error>Warning: Resetting the password when using encryption will result in data loss!</error>'
88
				);
89
				if (!$dialog->ask($input, $output, new Question('<question>Do you want to continue?</question>', true))) {
90
					return 1;
91
				}
92
			}
93
94
			$q = new Question('<question>Enter a new password: </question>', false);
95
			$q->setHidden(true);
96
			$password = $dialog->ask($input, $output, $q);
97
			$q = new Question('<question>Confirm the new password: </question>', false);
98
			$q->setHidden(true);
99
			$confirm = $dialog->ask($input, $output, $q);
100
			if ($password !== $confirm) {
101
				$output->writeln("<error>Passwords did not match!</error>");
102
				return 1;
103
			}
104
		} else {
105
			$output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>");
106
			return 1;
107
		}
108
109
		$success = $user->setPassword($password);
110
		if ($success) {
111
			$output->writeln("<info>Successfully reset password for " . $username . "</info>");
112
		} else {
113
			$output->writeln("<error>Error while resetting password!</error>");
114
			return 1;
115
		}
116
	}
117
}
118