src/EventListener/SecurityEventListener.php line 96

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Commons\OAuth2\AccessToken;
  4. use App\Exception\Security as SecurityException;
  5. use App\Service\CdpService;
  6. use App\Service\UserManager;
  7. use Psr\Log\LoggerAwareInterface;
  8. use Psr\Log\LoggerAwareTrait;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\Security\Core\AuthenticationEvents;
  16. use Symfony\Component\Security\Core\Event\AuthenticationEvent;
  17. use Trikoder\Bundle\OAuth2Bundle\Security\Authentication\Token\OAuth2Token;
  18. class SecurityEventListener implements EventSubscriberInterfaceLoggerAwareInterface
  19. {
  20.     use LoggerAwareTrait;
  21.     /**
  22.      * @var CdpService
  23.      */
  24.     protected $cdp;
  25.     /**
  26.      * @var UserManager
  27.      */
  28.     protected $userManager;
  29.     public function __construct(CdpService $cdpUserManager $userManagerRequestStack $requestStack)
  30.     {
  31.         $this->cdp $cdp;
  32.         $this->userManager $userManager;
  33.     }
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             AuthenticationEvents::AUTHENTICATION_SUCCESS => [['onOAuth2Authentication'20]],
  38.             KernelEvents::EXCEPTION => [['onSecurityException']],
  39.         ];
  40.     }
  41.     public function onOAuth2Authentication(AuthenticationEvent $event)
  42.     {
  43.         $token $event->getAuthenticationToken();
  44.         if (!$token instanceof OAuth2Token) {
  45.             return;
  46.         }
  47.         $accessTokenId $token->getAttribute('server_request')->getAttribute('oauth_access_token_id');
  48.         $clientId $token->getAttribute('server_request')->getAttribute('oauth_client_id');
  49.         $accessTokenEntity $this->cdp->getCommonsEntityManager()->createQueryBuilder()
  50.             ->from(AccessToken::class, 'access_token')
  51.             ->select('access_token')
  52.             ->innerJoin('access_token.account''account')
  53.             ->innerJoin('account.fond''fond')
  54.             ->addSelect('account')
  55.             ->addSelect('fond')
  56.             ->andWhere('access_token.identifier = :identifier')
  57.             ->setParameter('identifier'$accessTokenId)
  58.             ->getQuery()
  59.             ->getOneOrNullResult()
  60.         ;
  61.         if (!$accessTokenEntity) {
  62.             throw new SecurityException("Token entity not found"SecurityException::CODE_GENERAL);
  63.         }
  64.         $account $accessTokenEntity->getAccount();
  65.         $user $token->getUser(); // Ou bien $accessTokenEntity.account.user
  66.         if (!$user) {
  67.             throw new SecurityException("Token without user"SecurityException::CODE_GENERAL);
  68.         }
  69.         $this->userManager->updateConnectedAt($user);
  70.         $this->cdp
  71.             ->setCurrentUser($user)
  72.             ->setCurrentAccount($account)
  73.             ->setCurrentFond($account->getFond())
  74.         ;
  75.     }
  76.     public function onSecurityException(ExceptionEvent $event)
  77.     {
  78.         $exception $event->getThrowable();
  79.         if (!$exception instanceof SecurityException\SecurityException) {
  80.             return;
  81.         }
  82.         $this->logger->error($exception->getMessage(), [
  83.             'code' => $exception->getCode(),
  84.         ]);
  85.         $response = new JsonResponse([
  86.             'error' => $exception->getMessage(),
  87.             'code' => $exception->getCode(),
  88.         ], Response::HTTP_FORBIDDEN);
  89.         $event->setResponse($response);
  90.     }
  91. }