1 | <?php |
||||||||
2 | |||||||||
3 | /** |
||||||||
4 | * (c) FSi sp. z o.o. <[email protected]> |
||||||||
5 | * |
||||||||
6 | * For the full copyright and license information, please view the LICENSE |
||||||||
7 | * file that was distributed with this source code. |
||||||||
8 | */ |
||||||||
9 | |||||||||
10 | declare(strict_types=1); |
||||||||
11 | |||||||||
12 | namespace FSi\Bundle\AdminSecurityBundle\Command; |
||||||||
13 | |||||||||
14 | use Exception; |
||||||||
15 | use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents; |
||||||||
16 | use FSi\Bundle\AdminSecurityBundle\Event\ChangePasswordEvent; |
||||||||
17 | use FSi\Bundle\AdminSecurityBundle\Security\User\ChangeablePasswordInterface; |
||||||||
18 | use FSi\Bundle\AdminSecurityBundle\Security\User\UserRepositoryInterface; |
||||||||
19 | use InvalidArgumentException; |
||||||||
20 | use Symfony\Component\Console\Command\Command; |
||||||||
21 | use Symfony\Component\Console\Helper\QuestionHelper; |
||||||||
22 | use Symfony\Component\Console\Input\InputArgument; |
||||||||
23 | use Symfony\Component\Console\Input\InputInterface; |
||||||||
24 | use Symfony\Component\Console\Output\OutputInterface; |
||||||||
25 | use Symfony\Component\Console\Question\Question; |
||||||||
26 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||||||||
27 | |||||||||
28 | class ChangePasswordCommand extends Command |
||||||||
29 | { |
||||||||
30 | /** |
||||||||
31 | * @var UserRepositoryInterface |
||||||||
32 | */ |
||||||||
33 | private $userRepository; |
||||||||
34 | |||||||||
35 | /** |
||||||||
36 | * @var EventDispatcherInterface |
||||||||
37 | */ |
||||||||
38 | private $eventDispatcher; |
||||||||
39 | |||||||||
40 | public function __construct( |
||||||||
41 | UserRepositoryInterface $userRepository, |
||||||||
42 | EventDispatcherInterface $eventDispatcher, |
||||||||
43 | $name = null |
||||||||
44 | ) { |
||||||||
45 | parent::__construct($name); |
||||||||
46 | |||||||||
47 | $this->userRepository = $userRepository; |
||||||||
48 | $this->eventDispatcher = $eventDispatcher; |
||||||||
49 | } |
||||||||
50 | |||||||||
51 | protected function configure(): void |
||||||||
52 | { |
||||||||
53 | $this |
||||||||
54 | ->setName('fsi:user:change-password') |
||||||||
55 | ->setDescription('Change password of a user.') |
||||||||
56 | ->setDefinition([ |
||||||||
57 | new InputArgument('email', InputArgument::REQUIRED, 'The email'), |
||||||||
58 | new InputArgument('password', InputArgument::REQUIRED, 'The password'), |
||||||||
59 | ]) |
||||||||
60 | ->setHelp(<<<EOT |
||||||||
61 | The <info>fsi:user:change-password</info> command changes user's password: |
||||||||
62 | |||||||||
63 | <info>php app/console fsi:user:change-password [email protected]</info> |
||||||||
64 | |||||||||
65 | This interactive shell will first ask you for a password. |
||||||||
66 | |||||||||
67 | You can alternatively specify the new password as a second argument: |
||||||||
68 | |||||||||
69 | <info>php app/console fsi:user:change-password [email protected] mynewpassword</info> |
||||||||
70 | |||||||||
71 | EOT |
||||||||
72 | ); |
||||||||
73 | } |
||||||||
74 | |||||||||
75 | protected function execute(InputInterface $input, OutputInterface $output): void |
||||||||
76 | { |
||||||||
77 | $email = $input->getArgument('email'); |
||||||||
78 | $password = $input->getArgument('password'); |
||||||||
79 | |||||||||
80 | $user = $this->userRepository->findUserByEmail($email); |
||||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||||
81 | if (false === $user instanceof ChangeablePasswordInterface) { |
||||||||
82 | throw new InvalidArgumentException(sprintf('User with email "%s" cannot be found', $email)); |
||||||||
0 ignored issues
–
show
It seems like
$email can also be of type string[] ; however, parameter $args of sprintf() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
83 | } |
||||||||
84 | $user->setPlainPassword($password); |
||||||||
0 ignored issues
–
show
It seems like
$password can also be of type null and string[] ; however, parameter $password of FSi\Bundle\AdminSecurity...ace::setPlainPassword() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
85 | $this->eventDispatcher->dispatch(AdminSecurityEvents::CHANGE_PASSWORD, new ChangePasswordEvent($user)); |
||||||||
0 ignored issues
–
show
The call to
Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with new FSi\Bundle\AdminSecu...ngePasswordEvent($user) .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() FSi\Bundle\AdminSecurity...Events::CHANGE_PASSWORD of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
86 | |||||||||
87 | $output->writeln(sprintf('Changed password of user <comment>%s</comment>', $email)); |
||||||||
88 | } |
||||||||
89 | |||||||||
90 | protected function interact(InputInterface $input, OutputInterface $output): void |
||||||||
91 | { |
||||||||
92 | if (!$input->getArgument('email')) { |
||||||||
93 | $this->askEmail($input, $output); |
||||||||
94 | } |
||||||||
95 | |||||||||
96 | if (!$input->getArgument('password')) { |
||||||||
97 | $this->askPassword($input, $output); |
||||||||
98 | } |
||||||||
99 | } |
||||||||
100 | |||||||||
101 | private function askEmail(InputInterface $input, OutputInterface $output): void |
||||||||
102 | { |
||||||||
103 | $question = new Question('Please choose an email:'); |
||||||||
104 | $question->setValidator(function (string $email): string { |
||||||||
105 | if (empty($email)) { |
||||||||
106 | throw new Exception('Email can not be empty'); |
||||||||
107 | } |
||||||||
108 | |||||||||
109 | return $email; |
||||||||
110 | }); |
||||||||
111 | |||||||||
112 | $email = $this->getQuestionHelper()->ask($input, $output, $question); |
||||||||
113 | $input->setArgument('email', $email); |
||||||||
114 | } |
||||||||
115 | |||||||||
116 | private function askPassword(InputInterface $input, OutputInterface $output): void |
||||||||
117 | { |
||||||||
118 | $question = new Question('Please choose a password:'); |
||||||||
119 | $question->setValidator(function (string $password): string { |
||||||||
120 | if (empty($password)) { |
||||||||
121 | throw new Exception('Password can not be empty'); |
||||||||
122 | } |
||||||||
123 | |||||||||
124 | return $password; |
||||||||
125 | }); |
||||||||
126 | |||||||||
127 | $password = $this->getQuestionHelper()->ask($input, $output, $question); |
||||||||
128 | $input->setArgument('password', $password); |
||||||||
129 | } |
||||||||
130 | |||||||||
131 | private function getQuestionHelper(): QuestionHelper |
||||||||
132 | { |
||||||||
133 | return $this->getHelper('question'); |
||||||||
134 | } |
||||||||
135 | } |
||||||||
136 |