src/EventSubscriber/EasyAdminSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\PortalUser;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\EventDispatcher\GenericEvent;
  6. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  7. class EasyAdminSubscriber implements EventSubscriberInterface {
  8.     private $passwordEncoder;
  9.     public function __construct(UserPasswordEncoderInterface $passwordEncoder)
  10.     {
  11.         $this->passwordEncoder $passwordEncoder;
  12.     }
  13.     public static function getSubscribedEvents()
  14.     {
  15.         return array(
  16.             'easy_admin.pre_persist' => array('encodePassword'),
  17.             'easy_admin.pre_update' => array('encodePassword'),
  18.         );
  19.     }
  20.     public function encodePassword(GenericEvent $event)
  21.     {
  22.         $entity $event->getSubject();
  23.         if (!($entity instanceof PortalUser)) {
  24.             return;
  25.         }
  26.         if (!$entity->getPlainPassword()) {
  27.             return;
  28.         }
  29.         $encoded $this->passwordEncoder->encodePassword(
  30.             $entity,
  31.             $entity->getPlainPassword()
  32.         );
  33.         $entity->setPassword($encoded);
  34.         // necessary to force the update to see the change
  35.         $event['entity'] = $entity;
  36.     }
  37. }