<?php
# src/EventSubscriber/EasyAdminSubscriber.php
namespace App\EventSubscriber;
use App\Entity\User;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class EasyAdminSubscriber implements EventSubscriberInterface
{
private $passwordHasher;
public function __construct(UserPasswordHasherInterface $passwordHasher)
{
$this->passwordHasher = $passwordHasher;
}
public static function getSubscribedEvents()
{
return [
BeforeEntityPersistedEvent::class => ['rwHashPassword'],
];
}
public function rwHashPassword(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof User)) {
return;
}
$plainTextPassword = $entity->getPlainTextPassword();
$password = $this->passwordHasher->hashPassword($entity, $plainTextPassword);
$entity->setPassword($password);
}
}