1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the awurth/silex-user package. |
5
|
|
|
* |
6
|
|
|
* (c) Alexis Wurth <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace AWurth\Silex\User\Command; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
use Symfony\Component\Console\Question\Question; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ChangePasswordCommand. |
22
|
|
|
*/ |
23
|
|
|
class ChangePasswordCommand extends ContainerAwareCommand |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
protected function configure() |
29
|
|
|
{ |
30
|
|
|
$this |
31
|
|
|
->setName('silex-user:change-password') |
32
|
|
|
->setDescription('Change the password of a user.') |
33
|
|
|
->setDefinition([ |
34
|
|
|
new InputArgument('username', InputArgument::REQUIRED, 'The username'), |
35
|
|
|
new InputArgument('password', InputArgument::REQUIRED, 'The password') |
36
|
|
|
]) |
37
|
|
|
->setHelp(<<<'EOT' |
38
|
|
|
The <info>silex-user:change-password</info> command changes the password of a user: |
39
|
|
|
|
40
|
|
|
<info>php %command.full_name% matthieu</info> |
41
|
|
|
|
42
|
|
|
This interactive shell will first ask you for a password. |
43
|
|
|
|
44
|
|
|
You can alternatively specify the password as a second argument: |
45
|
|
|
|
46
|
|
|
<info>php %command.full_name% matthieu mypassword</info> |
47
|
|
|
|
48
|
|
|
EOT |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
56
|
|
|
{ |
57
|
|
|
$username = $input->getArgument('username'); |
58
|
|
|
$password = $input->getArgument('password'); |
59
|
|
|
|
60
|
|
|
$manipulator = $this->container['silex_user.util.user_manipulator']; |
61
|
|
|
$manipulator->changePassword($username, $password); |
62
|
|
|
|
63
|
|
|
$output->writeln(sprintf('Changed password for user <comment>%s</comment>', $username)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
protected function interact(InputInterface $input, OutputInterface $output) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$questions = []; |
72
|
|
|
|
73
|
|
|
if (!$input->getArgument('username')) { |
74
|
|
|
$question = new Question('Please give the username:'); |
75
|
|
|
$question->setValidator(function ($username) { |
76
|
|
|
if (empty($username)) { |
77
|
|
|
throw new Exception('Username can not be empty'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $username; |
81
|
|
|
}); |
82
|
|
|
$questions['username'] = $question; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (!$input->getArgument('password')) { |
86
|
|
|
$question = new Question('Please enter the new password:'); |
87
|
|
|
$question->setValidator(function ($password) { |
88
|
|
|
if (empty($password)) { |
89
|
|
|
throw new Exception('Password can not be empty'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $password; |
93
|
|
|
}); |
94
|
|
|
$question->setHidden(true); |
95
|
|
|
$questions['password'] = $question; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
foreach ($questions as $name => $question) { |
99
|
|
|
$answer = $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
100
|
|
|
$input->setArgument($name, $answer); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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.