src/EventSubscriber/LoginSubscriber.php line 21

  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  6. use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
  7. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  8. class LoginSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(private UrlGeneratorInterface $urlGenerator) {}
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             LoginSuccessEvent::class => 'onLoginSuccess',
  15.         ];
  16.     }
  17.     public function onLoginSuccess(LoginSuccessEvent $event): void
  18.     {
  19.         $user $event->getUser();
  20.         $response null;
  21.         if(in_array('ROLE_ADMIN',$user->getRoles())){
  22.             $response = new RedirectResponse($this->urlGenerator->generate('app_pos_home'),RedirectResponse::HTTP_SEE_OTHER);
  23.         }
  24.         elseif (in_array('ROLE_SALES_MANAGER',$user->getRoles()))
  25.         {
  26.             $response = new RedirectResponse($this->urlGenerator->generate('app_pos_home'),RedirectResponse::HTTP_SEE_OTHER);
  27.         }
  28.         else{
  29.             $response = new RedirectResponse($this->urlGenerator->generate('app_product_index'),RedirectResponse::HTTP_SEE_OTHER);
  30.         }
  31.         $event->setResponse($response);
  32.     }
  33. }