vendor/nellapp/sdk-bundle/src/Auth/EventSubscriber/LoginSubscriber.php line 59

Open in your IDE?
  1. <?php
  2. namespace Nellapp\Bundle\SDKBundle\Auth\EventSubscriber;
  3. use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface;
  4. use Nellapp\Bundle\SDKBundle\Routing\Utils\DomainsRoutingUtils;
  5. use Nellapp\Bundle\SDKBundle\Sync\ApiClient\Account\SsoClient;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  11. class LoginSubscriber implements EventSubscriberInterface
  12. {
  13. private ?array $pendingPayload = null;
  14. public function __construct(
  15. private DomainsRoutingUtils $domainsRoutingUtils,
  16. private SsoClient $ssoClient,
  17. private LoggerInterface $logger,
  18. )
  19. {
  20. }
  21. public static function getSubscribedEvents(): array
  22. {
  23. return [
  24. LoginSuccessEvent::class => 'onLoginSuccess',
  25. KernelEvents::TERMINATE => 'onTerminate',
  26. ];
  27. }
  28. public function onLoginSuccess(LoginSuccessEvent $event): void
  29. {
  30. $request = $event->getRequest();
  31. $user = $event->getUser();
  32. $session = $request->getSession();
  33. if (!$user instanceof UserInterface || !$session) {
  34. return;
  35. }
  36. // DeviceId cookie should already been set through DeviceIdSubscriber on pre-oauth requests
  37. $deviceId = $request->cookies->get('DEVICE_ID');
  38. if (!$deviceId) {
  39. return;
  40. }
  41. $this->pendingPayload = [
  42. 'appName' => $this->domainsRoutingUtils->getAppName(),
  43. 'userId' => $user->getId(),
  44. 'sessionId' => $session->getId(),
  45. 'deviceId' => $deviceId,
  46. 'userAgent' => $request->headers->get('User-Agent'),
  47. 'ipAddress' => $request->getClientIp(),
  48. ];
  49. }
  50. public function onTerminate(TerminateEvent $event): void
  51. {
  52. if (!$this->pendingPayload) {
  53. return;
  54. }
  55. try {
  56. $this->ssoClient->postRegister($this->pendingPayload);
  57. } catch (\Exception $e) {
  58. $this->logger->error($e);
  59. }
  60. $this->pendingPayload = null;
  61. }
  62. }