src/EventSubscriber/UserAdminSubscriber.php line 27
<?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 UserAdminSubscriber implements EventSubscriberInterface
{
private UserPasswordHasherInterface $userPasswordHasher;
public function __construct(UserPasswordHasherInterface $userPasswordHasher)
{
$this->userPasswordHasher = $userPasswordHasher;
}
// Ajout du type de retour 'array' pour supprimer le message de dépréciation
public static function getSubscribedEvents(): array
{
return [
BeforeEntityPersistedEvent::class => ['beforeEntityPersisted'],
];
}
public function beforeEntityPersisted(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof User)) {
return;
}
if ($entity->getBeforeRole()) {
if ($entity->getBeforeRole() == User::GOD_ADMIN) {
$entity->setRoles([User::GOD_ADMIN, User::ADMIN, User::COMPTABLE]);
} else {
$entity->setRoles([$entity->getBeforeRole()]);
}
}
}
}