Bonjour,

J'essaie de chiffrer les mots de passe avec la version 3.5.9 de Symfony mais cela ne fonctionne pas, je vous remercie de votre aide:

Dans mon controller:

Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
 
namespace App\Controller;
 
use App\Entity\User;
use App\Form\RegisterType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
 
class SecurityController extends AbstractController
{
    private $passwordHasher;
 
    public function __construct(UserPasswordHasherInterface $passwordHasher)
    {
        $this->passwordHasher = $passwordHasher;
    }
 
    #[Route('/register', name: 'security_register')]
    public function register(Request $request, UserPasswordHasherInterface $encodage): Response
    {
        $user = new User();
        $form = $this->createForm(RegisterType::class, $user);
 
        if ($form->isSubmitted() && $form->isValid()) {
            // $user->setRoles(['ROLE_USER']);
 
            $user->setPassword($this->passwordHasher->hashPassword($user, $user->getPassword()));
 
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($user);
            $entityManager->flush();
 
            return $this->redirectToRoute('home');
        }
 
        return $this->render('security/index.html.twig', [
            'controller_name' => "Formulaire d'inscription",
            'form' => $form->createView(),
        ]);
    }
}

Mon form:

Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
 
namespace App\Form;
 
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
 
class RegisterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('username')
            ->add('firstname')
            ->add('lastname')
            ->add('email')
            ->add('password',PasswordType::class)
            ->add('passwordConfirm',PasswordType::class)
            // ->add('createdAt')
        ;
    }
 
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => User::class,
        ]);
    }
}

Mon Entity:

Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
 
namespace App\Entity;
 
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
//Pour la validation du formulaire d'inscription:
use Symfony\Component\Validator\Constraints as Assert;
 
 /**
 * @ORM\Entity(repositoryClass=UserRepository::class)
 * @method string getUserIdentifier()
 */
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;
 
    /**
     * @Assert\Length(min=3,max=50)
     * @ORM\Column(type="string", length=255)
     */
    private $username;
 
    /**
     * @Assert\Length(min=3,max=50)
     * @ORM\Column(type="string", length=255)
     */
    private $firstname;
 
    /**
     * @Assert\Length(min=3,max=50)
     * @ORM\Column(type="string", length=255)
     */
    private $lastname;
 
    /**
     * @Assert\Email(message="L'email saisi n'est pas valide")
     * @ORM\Column(type="string", length=255)
     */
    private $email;
 
    /**
     * @Assert\Length(min=8,max=50)
     * @ORM\Column(type="string", length=255)
     */
    private $password;
 
    /**
     * @ORM\Column(type="datetime_immutable")
     */
    private $createdAt;
 
    /**
     * @ORM\OneToMany(targetEntity=Article::class, mappedBy="author")
     */
    private $articles;
 
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public function __toString()
    {
    /*Pour accepter la sélection dans les zones de liste (Fiche article->author...)*/
    return $this->firstname. ' ' .$this->lastname;
    //Ajout pour l'erreur de conversion en chaine à l'affichage du nom de l'auteur en page home ({{ article.author }})
    return (string) $this->getUsername();
    }
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
    /**
     * @Assert\EqualTo(propertyPath="password", message="Les 2 mots de passe doivent être identiques")
     */
    private $passwordConfirm;
 
    public function __construct()
    {
        $this->articles = new ArrayCollection();
 
        //Pour insérer la date par défaut en création d'un user
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        $this->createdAt = new \DatetimeImmutable();
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        }
 
        //Pour insérer la confirmation du mot de passe
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public function getPasswordConfirm(): ?string
        {
            return $this->passwordConfirm;
        }
 
        public function setPasswordConfirm(string $passwordConfirm): string
        {
            $this->passwordConfirm = $passwordConfirm;
 
            return $passwordConfirm;
        }
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
    public function getId(): ?int
    {
        return $this->id;
    }
 
    public function getUsername(): ?string
    {
        return $this->username;
    }
 
    public function setUsername(string $username): self
    {
        $this->username = $username;
 
        return $this;
    }
 
    public function getFirstname(): ?string
    {
        return $this->firstname;
    }
 
    public function setFirstname(string $firstname): self
    {
        $this->firstname = $firstname;
 
        return $this;
    }
 
    public function getLastname(): ?string
    {
        return $this->lastname;
    }
 
    public function setLastname(string $lastname): self
    {
        $this->lastname = $lastname;
 
        return $this;
    }
 
    public function getEmail(): ?string
    {
        return $this->email;
    }
 
    public function setEmail(string $email): self
    {
        $this->email = $email;
 
        return $this;
    }
 
    public function getPassword(): ?string
    {
        return $this->password;
    }
 
    public function setPassword(string $password): self
    {
        $this->password = $password;
 
        return $this;
    }
 
    public function getCreatedAt(): ?\DateTimeImmutable
    {
        return $this->createdAt;
    }
 
    public function setCreatedAt(\DateTimeImmutable $createdAt): self
    {
        $this->createdAt = $createdAt;
 
        return $this;
    }
 
    /**
     * @return Collection|Article[]
     */
    public function getArticles(): Collection
    {
        return $this->articles;
    }
 
    public function addArticle(Article $article): self
    {
        if (!$this->articles->contains($article)) {
            $this->articles[] = $article;
            $article->setAuthor($this);
        }
 
        return $this;
    }
 
    public function removeArticle(Article $article): self
    {
        if ($this->articles->removeElement($article)) {
            // set the owning side to null (unless already changed)
            if ($article->getAuthor() === $this) {
                $article->setAuthor(null);
            }
        }
 
        return $this;
    }
 
    public function getRoles()
    {
        return ['ROLE_USER'];
    }
    public function setRoles()
    {
        return ['ROLE_USER'];
    }
    /**
     * Returning a salt is only needed, if you are not using a modern
     * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
     *
     * @see UserInterface
     */
    public function getSalt(): ?string
    {
        return null;
    }
 
    /**
     * @see UserInterface
     */
    public function eraseCredentials()
    {
        // If you store any temporary, sensitive data on the user, clear it here
        // $this->plainPassword = null;
    }
}

Mon security.yaml:

Code yaml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# config/packages/security.yaml
# Attention à l'indentation
security:
    password_hashers:
        App\Entity\User:
            algorithm: auto
    # https://symfony.com/doc/current/security/authenticator_manager.html
    enable_authenticator_manager: true
    # https://symfony.com/doc/current/security.html#c-hashing-passwords
    # Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
    # Symfony\Component\Security\Core\User\InMemoryUser: bcrypt
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        users_in_memory: { memory: null }
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            lazy: true
            provider: users_in_memory
 
            # activate different ways to authenticate
            # https://symfony.com/doc/current/security.html#firewalls-authentication
 
            # https://symfony.com/doc/current/security/impersonating_user.html
            # switch_user: true
 
    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        # - { path: ^/admin, roles: ROLE_ADMIN }
        # - { path: ^/profile, roles: ROLE_USER }