src/Subscriber/ApiAuthenticationSubscriber.php line 73

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber;
  3. use App\Service\User\UserPermissionsHelper;
  4. use Oz\ApiNvl\Model\User;
  5. use App\Service\User\UserDecorator;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  10. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  11. use Symfony\Component\Security\Http\SecurityEvents;
  12. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  13. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  14. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  15. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  16. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  17. /**
  18.  *
  19.  */
  20. class ApiAuthenticationSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var UserDecorator
  24.      */
  25.     private $userDecorator;
  26.     /**
  27.      * @var UserPermissionsHelper
  28.      */
  29.     private $userPermissionsHelper;
  30.     /**
  31.      * @var TokenStorageInterface
  32.      */
  33.     private $tokenStorage;
  34.     /**
  35.      * @param UserDecorator $userDecorator
  36.      * @param UserPermissionsHelper $userPermissionsHelper
  37.      * @param TokenStorageInterface $tokenStorage
  38.      */
  39.     public function __construct(
  40.         UserDecorator         $userDecorator,
  41.         UserPermissionsHelper $userPermissionsHelper,
  42.         TokenStorageInterface $tokenStorage)
  43.     {
  44.         $this->userDecorator $userDecorator;
  45.         $this->userPermissionsHelper $userPermissionsHelper;
  46.         $this->tokenStorage $tokenStorage;
  47.     }
  48.     /**
  49.      * @return array[]
  50.      */
  51.     public static function getSubscribedEvents(): array
  52.     {
  53.         return [
  54.             SecurityEvents::INTERACTIVE_LOGIN => ['interactiveLogin'50],
  55.         ];
  56.     }
  57.     /**
  58.      * @param InteractiveLoginEvent $interactiveLoginEvent
  59.      *
  60.      * @throws ClientExceptionInterface
  61.      * @throws DecodingExceptionInterface
  62.      * @throws RedirectionExceptionInterface
  63.      * @throws ServerExceptionInterface
  64.      * @throws TransportExceptionInterface
  65.      */
  66.     public function interactiveLogin(InteractiveLoginEvent $interactiveLoginEvent): void
  67.     {
  68.         /** @var User $user */
  69.         $user $interactiveLoginEvent->getAuthenticationToken()->getUser();
  70.         $this->userPermissionsHelper->setUserPermissionsRoles($user);
  71.         $roles $user->getRoles();
  72.         $token $interactiveLoginEvent->getAuthenticationToken();
  73.         $firewallName 'main';
  74.         $newToken = new UsernamePasswordToken($user$token->getCredentials(), $firewallName$roles);
  75.         $this->tokenStorage->setToken($newToken);
  76.         $request $interactiveLoginEvent->getRequest();
  77.         $request->getSession()->set('_security_' $firewallNameserialize($newToken));
  78.         if (!$user instanceof User) {
  79.             return;
  80.         }
  81.         // j'ai décommenté car si je laisse ça déconnecte mon user
  82.         // $this->userDecorator->setUserInfo($user);
  83.         // $this->userDecorator->setUserPermissions($user);
  84.     }
  85. }