src/EventSubscriber/SecuritySubscriber.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  8. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  9. use Symfony\Component\Security\Http\Event\LoginFailureEvent;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use App\Service\Kernel\MailService;
  12. use App\Service\Kernel\KernelService;
  13. class SecuritySubscriber implements EventSubscriberInterface
  14. {
  15.     
  16.     private $em;
  17.     private $mailService;
  18.     private $kernelService;
  19.     public function __construct(EntityManagerInterface $entityManagerKernelService $kernelServiceMailService $mailService) {
  20.         $this->em $entityManager;
  21.         $this->mailService $mailService;
  22.         $this->kernelService $kernelService;
  23.     }
  24.     
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         // return the subscribed events, their methods and priorities
  28.         return [
  29.             /*KernelEvents::EXCEPTION => [
  30.                 ['processException', 10],
  31.                 ['logException', 0],
  32.                 ['notifyException', -10],
  33.             ],*/
  34.             LoginSuccessEvent::class => 'onLoginSuccess',
  35.             LoginFailureEvent::class => 'onLoginFailure',
  36.             
  37.         ];
  38.     }
  39.     public function onLoginSuccess(LoginSuccessEvent $event): void
  40.     {
  41.         $user $event->getUser();
  42.         $organization =  $user->getOrganization();
  43.         $loginmail $this->kernelService->getApplicationInfoValue("Kernel.LoginMail"$organization);
  44.         $applicationname $this->kernelService->getApplicationInfoValue("Kernel.Applicationame"$organization);
  45.             
  46.         if($loginmail == true)
  47.         {
  48.                 $tomail $this->kernelService->getApplicationInfoValue("Kernel.Loginemail"$organization);
  49.                 $this->mailService->createMail($tomail"Login success in " $applicationname'login from '$user' using ip:'.$event->getRequest()->getClientIp(), nullnullnull$organization);
  50.         }
  51.         // ...
  52.         //$this->mailService->createMail("hannes.vanderheyden@kreonta.com", "login success: ".$event->getUser(), "login success: ".$event->getUser());
  53.         
  54.     }
  55.     public function onLoginFailure(LoginFailureEvent  $event): void
  56.     {
  57.         // ...
  58.         //$info = "".$event->getException();
  59.         /*$info = "LoginFailure";
  60.         $info2 = "LoginFailure: " . $event->getRequest()->get("uuid");
  61.         $this->mailService->createMail("hannes.vanderheyden@kreonta.com",$info, $info2);*/
  62.         
  63.         $organization =  null;
  64.         $loginmail $this->kernelService->getApplicationInfoValue("Kernel.LoginMail"$organization);
  65.         $applicationname $this->kernelService->getApplicationInfoValue("Kernel.Applicationame"$organization);
  66.             
  67.         if($loginmail == true)
  68.         {
  69.                 $tomail $this->kernelService->getApplicationInfoValue("Kernel.Loginemail"$organization);
  70.                 $this->mailService->createMail($tomail"Login failure " $applicationname'login from '$event->getRequest()->get("uuid"). ' using ip:'.$event->getRequest()->getClientIp(), nullnullnull$organization);
  71.         }
  72.         
  73.     }
  74. }