src/EventSubscriber/UserAdminSubscriber.php line 27

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User;
  4. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  7. class UserAdminSubscriber implements EventSubscriberInterface
  8. {
  9.     private UserPasswordHasherInterface $userPasswordHasher;
  10.     public function __construct(UserPasswordHasherInterface $userPasswordHasher)
  11.     {
  12.         $this->userPasswordHasher $userPasswordHasher;
  13.     }
  14.     // Ajout du type de retour 'array' pour supprimer le message de dépréciation
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             BeforeEntityPersistedEvent::class => ['beforeEntityPersisted'],
  19.         ];
  20.     }
  21.     public function beforeEntityPersisted(BeforeEntityPersistedEvent $event)
  22.     {
  23.         $entity $event->getEntityInstance();
  24.         if (!($entity instanceof User)) {
  25.             return;
  26.         }
  27.         if ($entity->getBeforeRole()) {
  28.             if ($entity->getBeforeRole() == User::GOD_ADMIN) {
  29.                 $entity->setRoles([User::GOD_ADMINUser::ADMINUser::COMPTABLE]);
  30.             } else {
  31.                 $entity->setRoles([$entity->getBeforeRole()]);
  32.             }
  33.         }
  34.     }
  35. }