简体   繁体   中英

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

<?php

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

security:
  encoders:
    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

<?php

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)
    {
    $formMapper
    ->with('Ajouter Etudiant')
    ->add('nom', 'text')
    ->add('prenom', 'text')
    ->add('username', 'text')
    ->add('dateNaissance', 'birthday')
    ->add('email','email')
    ->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')
    ->end()
    ->end();
}
}

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

and now here's the admin service

services:
admin.etudiant:
    class: AF\UserBundle\Admin\EtudiantAdmin
    arguments: [~, AF\UserBundle\Entity\Etudiant, ~]
    tags:
        - { 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.

我仅用plainpassword替换了密码,并且可以使用,我不知道这是否是预期的结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM