src/EventSubscriber/SSOSubscriber.php line 27

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Repository\Pmi\UserRepository;
  4. use App\Security\PmiAuthenticator;
  5. use Symfony\Bundle\SecurityBundle\Security;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. class SSOSubscriber implements EventSubscriberInterface
  11. {
  12.     public function __construct(
  13.         private UserRepository $userRepository,
  14.         private Security $security,
  15.     ){}
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             KernelEvents::RESPONSE => [['onKernelRequest'10]],
  20.         ];
  21.     }
  22.     public function onKernelRequest(ResponseEvent $event)
  23.     {
  24.         $request $event->getRequest();
  25.         if (!$request->hasPreviousSession()) {
  26.             return;
  27.         }
  28.         if (null === $this->security->getUser() && null !== $hash $request->query->get('sso')) {
  29.             if (null !== $user $this->userRepository->findBySSO($hash)) {
  30.                 $this->security->login($userPmiAuthenticator::class);
  31.                 $event->setResponse(new RedirectResponse(preg_replace('#\?sso=.+#'''$request->getRequestUri())));
  32.             }
  33.         }
  34.     }
  35. }