src/Diplix/KMGBundle/Service/LoginSuccessHandler.php line 69

Open in your IDE?
  1. <?php
  2. namespace Diplix\KMGBundle\Service;
  3. use Diplix\Commons\DataHandlingBundle\Entity\SysLogEntry;
  4. use Diplix\Commons\DataHandlingBundle\Repository\SysLogRepository;
  5. use Diplix\KMGBundle\Controller\User\WorkTimeController;
  6. use Diplix\KMGBundle\Entity\Role;
  7. use Diplix\KMGBundle\Entity\User;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\Security\Http\Authenticator\InteractiveAuthenticatorInterface;
  13. use Symfony\Component\Security\Http\Authenticator\JsonLoginAuthenticator;
  14. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  15. use Symfony\Component\Security\Http\Event\LogoutEvent;
  16. class LoginSuccessHandler implements
  17.     \Symfony\Component\EventDispatcher\EventSubscriberInterface
  18. {
  19.     public function __construct(
  20.         protected RouterInterface $router,
  21.         protected AuthorizationCheckerInterface $authChecker,
  22.         protected EntityManagerInterface $em)
  23.     {
  24.     }
  25.     public function onAuthenticationSuccess(LoginSuccessEvent $event): void
  26.     {
  27.         $authenticator $event->getAuthenticator();
  28.         if (method_exists($authenticator,'getAuthenticator')) // happens for example with TraceableAuthenticator in debug mode
  29.         {
  30.             $authenticator $authenticator->getAuthenticator();
  31.         }
  32.         if ($authenticator instanceof JsonLoginAuthenticator)
  33.         {
  34.             // no redirect for json login
  35.             return;
  36.         }
  37.         /** @var User $u */
  38.         $u $event->getUser();
  39.         SysLogRepository::logMessage($this->em->getConnection(),SysLogEntry::SYS_LOGIN,"Login",[
  40.             "ip"=>$event->getRequest()->getClientIp(),
  41.             "userAgent"=>$event->getRequest()->headers->get('User-Agent')
  42.         ],$u->getId());
  43.         WorkTimeController::fromLogin($this->em,$u);
  44.         $home $this->router->generate("kmg_home");
  45.         if ($this->authChecker->isGranted(Role::DISPO))
  46.         {
  47.             $home $this->router->generate('dispatch-dashboard2');
  48.         }
  49.         if ($u->getCustomer() !== null)
  50.         {
  51.             $event->setResponse(new RedirectResponse($this->router->generate("admin-switch-customer",
  52.                         array(  "customerId"=>$u->getCustomer()->getId(),
  53.                                 "referTo"=>$home))));
  54.             return;
  55.         }
  56.         $event->setResponse(new RedirectResponse($home));
  57.         return;
  58.     }
  59.     public function onLogout(LogoutEvent $logoutEvent): void
  60.     {
  61.         $request $logoutEvent->getRequest();
  62.         $token $logoutEvent->getToken();
  63.         /** @var User $u */
  64.         $u $token->getUser();
  65.         SysLogRepository::logMessage($this->em->getConnection(),SysLogEntry::SYS_LOGOUT,"Logout",[
  66.             "ip"=>$request->getClientIp()
  67.         ],$u->getId());
  68.         WorkTimeController::fromLogout($this->em,$u);
  69.     }
  70.     /**
  71.      * @return array<string, mixed>
  72.      */
  73.     public static function getSubscribedEvents(): array
  74.     {
  75.         return [
  76.             LoginSuccessEvent::class => 'onAuthenticationSuccess',
  77.             LogoutEvent::class => 'onLogout'
  78.         ];
  79.     }
  80. }