<?php
namespace App\EventSubscriber;
use App\Entity\PortalUser;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class EasyAdminSubscriber implements EventSubscriberInterface {
private $passwordEncoder;
public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
$this->passwordEncoder = $passwordEncoder;
}
public static function getSubscribedEvents()
{
return array(
'easy_admin.pre_persist' => array('encodePassword'),
'easy_admin.pre_update' => array('encodePassword'),
);
}
public function encodePassword(GenericEvent $event)
{
$entity = $event->getSubject();
if (!($entity instanceof PortalUser)) {
return;
}
if (!$entity->getPlainPassword()) {
return;
}
$encoded = $this->passwordEncoder->encodePassword(
$entity,
$entity->getPlainPassword()
);
$entity->setPassword($encoded);
// necessary to force the update to see the change
$event['entity'] = $entity;
}
}