src/EventSubscriber/LoginSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\FormTemplate;
  4. use App\Entity\LinkCreditToVersion;
  5. use App\Entity\LogConnection;
  6. use App\Entity\Notification;
  7. use App\Entity\User;
  8. use App\Entity\UserMindpulseKey;
  9. use App\Service\FormService;
  10. use App\Services\AxonautApiService;
  11. use App\Services\ClinicianCreditService;
  12. use App\Services\NotificationService;
  13. use DateTimeImmutable;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Psr\Log\LoggerInterface;
  16. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\Security\Core\User\UserInterface;
  19. use Symfony\Component\Security\Http\Event\LoginFailureEvent;
  20. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. class LoginSubscriber implements EventSubscriberInterface
  23. {
  24.     public function __construct(
  25.         private EntityManagerInterface $em,
  26.         private FormService $formService
  27.     ) {}
  28.     public function onLogin(LoginSuccessEvent $event)
  29.     {
  30.         /** @var User|UserInterface $user */
  31.         $user $event->getUser();
  32.         if (
  33.             in_array('ROLE_MEMBER'$user->getRoles()) ||
  34.             in_array('ROLE_SHOP'$user->getRoles())
  35.         ) {
  36.             $forms $this->em->getRepository(FormTemplate::class)->findActualityFormForUser($user);
  37.             foreach ($forms as $form) {
  38.                 $this->formService->buildFormForUser($user$form);
  39.             }
  40.         }
  41.         $logConnection = new LogConnection();
  42.         $logConnection
  43.             ->setUser($user)
  44.             ->setLoggedAt(new DateTimeImmutable())
  45.         ;
  46.         $this->em->persist($logConnection);
  47.         $this->em->flush();
  48.     }
  49.     public function onLoginFailure(LoginFailureEvent $event)
  50.     {
  51.         $logConnection = new LogConnection();
  52.         $logConnection
  53.             ->setLoggedAt(new DateTimeImmutable())
  54.             ->setEmail($event->getRequest()->request->get('email'null))
  55.             ->setPassword($event->getRequest()->request->get('password'null))
  56.             ->setMessage($event->getException()->getMessage())
  57.         ;
  58.         $this->em->persist($logConnection);
  59.         $this->em->flush();
  60.     }
  61.     public static function getSubscribedEvents(): array
  62.     {
  63.          return [
  64.             LoginSuccessEvent::class => 'onLogin',
  65.             LoginFailureEvent::class => 'onLoginFailure',
  66.         ];
  67.     }
  68. }