Anes Gongi Anes Gongi - 1 month ago 23
PHP Question

Symfony 3 - FOSUserBundle SonataAdminBundle hash problems

Good evening everyone

During the development of admin page, I found a password hash problem users using both bundles and FOSUserBundle SonataAdminBunlde.

Ok, I begin by showing you my user class


namespace AF\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;

* Etudiant
* @ORM\Table(name="etudiant")
* @ORM\Entity(repositoryClass="AF\UserBundle\Repository\EtudiantRepository")
class Etudiant extends BaseUser
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
protected $id;

* @ORM\Column(name="nom", type="text")
private $nom;

* @ORM\Column(name="prenom", type="text")
private $prenom;

* @ORM\Column(name="cin", type="integer")
private $cin;

* @ORM\Column(name="num_inscri", type="text")
private $numInscri;

* @ORM\Column(name="date_naissance", type="date")
private $dateNaissance;

* @ORM\Column(name="num_tel", type="integer")
private $numTel;

* @ORM\Column(name="adresse", type="text")
private $adresse;

* @ORM\ManyToOne(targetEntity="AF\EnsiBundle\Entity\Classes")
* @ORM\JoinColumn(nullable=false)
private $classes;

As you can see, I created the Student class based on the FOS User Bundle

and here is the file security.yml

AF\UserBundle\Entity\Etudiant: sha512

Now I have created an admin page using the template provided by SonataAdminBundle.

here is a part of the class EtudiantAdmin


namespace AF\UserBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;

class EtudiantAdmin extends AbstractAdmin
protected function configureFormFields(FormMapper $formMapper)
->with('Ajouter Etudiant')
->add('nom', 'text')
->add('prenom', 'text')
->add('username', 'text')
->add('dateNaissance', 'birthday')
->add('password', 'text')
->add('cin', 'text')
->add('numInscri', 'text')
->add('classes', 'sonata_type_model', array(
'class' => 'AF\EnsiBundle\Entity\Classes',
'property' => 'nom',
->add('numTel', 'text')
->add('adresse', 'text')
->add('roles', 'collection')

the function configureFormFields create a form in the admin page with which I may add a student.

and now here's the admin service

class: AF\UserBundle\Admin\EtudiantAdmin
arguments: [~, AF\UserBundle\Entity\Etudiant, ~]
- { name: sonata.admin, manager_type: orm, label: Etudiant, group: "Gestionnaire Etudiants"}

When I add a student, all working well and the student is added to the database unless I found the password I typed without hashing

I know that I need to write some instructions in a controller, but I have no idea what should I do.

There's someone who does it help me


Just injects @fos_user.user_manager service to EtudiantAdmin service/class and implements prePersist()/preUpdate() methods to update the canonical* and password (hash) fields:

public function prePersist($user)
    $this->userManager->updateUser($user, false);

public function preUpdate($user)
    $this->userManager->updateUser($user, false);

Important! use $formMapper->add('plainPassword', 'text') instead of password in your form.