src/EventSubscriber/EasyAdminSubscriber.php line 31
<?php /** @noinspection ALL */
namespace App\EventSubscriber;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class EasyAdminSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
{
private $entityManager;
private $passwordEncoder;
public function __construct(EntityManagerInterface $entityManager, UserPasswordHasherInterface $passwordEncoder)
{
$this->entityManager = $entityManager;
$this->passwordEncoder = $passwordEncoder;
}
public static function getSubscribedEvents(): array
{
return [
BeforeEntityPersistedEvent::class => ['addUser'],
BeforeEntityUpdatedEvent::class => ['updateUser'], //surtout utile lors d'un reset de mot passe plutôt qu'un réel update, car l'update va de nouveau encrypter le mot de passe DEJA encrypté ...
];
}
public function updateUser(BeforeEntityUpdatedEvent $event): void
{
$entity = $event->getEntityInstance();
if (!($entity instanceof User)) {
return;
}
$this->setPassword($entity);
}
public function addUser(BeforeEntityPersistedEvent $event): void
{
$entity = $event->getEntityInstance();
if (!($entity instanceof User)) {
return;
}
$this->setPassword($entity);
}
/**
* @param User $entity
*/
public function setPassword(User $entity): void
{
$pass = $entity->getPlainPassword();
$entity->setPassword(
$this->passwordEncoder->hashPassword(
$entity,
$pass
)
);
$this->entityManager->persist($entity);
$this->entityManager->flush();
}
}