Skip to content

[Sf62] Use the new 'hash_property_path' option of the PasswordType field #1380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
namespace App\Controller;

use App\Entity\User;
use App\Form\Type\ChangePasswordType;
use App\Form\ChangePasswordType;
use App\Form\UserType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Security\Http\Attribute\IsGranted;
Expand Down Expand Up @@ -61,17 +60,12 @@ public function edit(
public function changePassword(
#[CurrentUser] User $user,
Request $request,
UserPasswordHasherInterface $passwordHasher,
EntityManagerInterface $entityManager,
): Response {
$form = $this->createForm(ChangePasswordType::class);
$form = $this->createForm(ChangePasswordType::class, $user);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
/** @var string $plainPassword */
$plainPassword = $form->get('newPassword')->getData();

$user->setPassword($passwordHasher->hashPassword($user, $plainPassword));
$entityManager->flush();

return $this->redirectToRoute('security_logout');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
* file that was distributed with this source code.
*/

namespace App\Form\Type;
namespace App\Form;

use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;

/**
* Defines the custom form field type used to change user's password.
* Defines the form used to change user's password.
*
* @author Romain Monteil <[email protected]>
*/
Expand All @@ -37,6 +39,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
new UserPassword(),
],
'label' => 'label.current_password',
'mapped' => false,
'attr' => [
'autocomplete' => 'off',
],
Expand All @@ -51,12 +54,24 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
),
],
'first_options' => [
'hash_property_path' => 'password',
'label' => 'label.new_password',
],
'mapped' => false,
'second_options' => [
'label' => 'label.new_password_confirm',
],
])
;
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}