Skip to content

Commit 56f7fd8

Browse files
committed
use password hasher in reset password controller if available
1 parent 0064c28 commit 56f7fd8

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/Maker/MakeResetPassword.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
use Symfony\Component\Console\Command\Command;
3232
use Symfony\Component\Console\Input\InputInterface;
3333
use Symfony\Component\Mailer\MailerInterface;
34+
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
35+
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
3436
use Symfony\Component\Yaml\Yaml;
3537
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface;
3638
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestTrait;
@@ -186,6 +188,16 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
186188
'Form\\'
187189
);
188190

191+
/*
192+
* @legacy Conditional can be removed when MakerBundle no longer
193+
* supports Symfony < 6.0.
194+
*/
195+
$passwordHasher = UserPasswordEncoderInterface::class;
196+
197+
if (interface_exists(UserPasswordHasherInterface::class)) {
198+
$passwordHasher = UserPasswordHasherInterface::class;
199+
}
200+
189201
$generator->generateController(
190202
$controllerClassNameDetails->getFullName(),
191203
'resetPassword/ResetPasswordController.tpl.php',
@@ -202,6 +214,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
202214
'from_email_name' => $this->fromEmailName,
203215
'email_getter' => $this->emailGetterMethodName,
204216
'email_field' => $this->emailPropertyName,
217+
'password_class_details' => ($passwordClassDetails = $generator->createClassNameDetails($passwordHasher, '\\')),
218+
'password_variable_name' => sprintf('$%s', lcfirst($passwordClassDetails->getShortName())), // @legacy see passwordHasher conditional above
219+
'use_password_hasher' => UserPasswordHasherInterface::class === $passwordHasher, // @legacy see passwordHasher conditional above
205220
]
206221
);
207222

src/Resources/skeleton/resetPassword/ResetPasswordController.tpl.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function checkEmail(): Response
9696
* @Route("/reset/{token}", name="app_reset_password")
9797
*/
9898
<?php } ?>
99-
public function reset(Request $request, UserPasswordEncoderInterface $passwordEncoder, string $token = null): Response
99+
public function reset(Request $request, <?= $password_class_details->getShortName() ?> <?= $password_variable_name ?>, string $token = null): Response
100100
{
101101
if ($token) {
102102
// We store the token in session and remove it from the URL, to avoid the URL being
@@ -130,8 +130,8 @@ public function reset(Request $request, UserPasswordEncoderInterface $passwordEn
130130
// A password reset token should be used only once, remove it.
131131
$this->resetPasswordHelper->removeResetRequest($token);
132132

133-
// Encode the plain password, and set it.
134-
$encodedPassword = $passwordEncoder->encodePassword(
133+
// Encode(hash) the plain password, and set it.
134+
$encodedPassword = <?= $password_variable_name ?>-><?= $use_password_hasher ? 'hashPassword' : 'encodePassword' ?>(
135135
$user,
136136
$form->get('plainPassword')->getData()
137137
);

0 commit comments

Comments
 (0)