src/WpBundle/Listener/WPAuthListener.php line 18

Open in your IDE?
  1. <?php
  2. namespace WpBundle\Listener;
  3. use CoreBundle\Service\UserService;
  4. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  5. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
  7. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  8. use Symfony\Component\Security\Http\Firewall\ListenerInterface;
  9. use CoreBundle\Entity\User;
  10. use WpBundle\Security\WPAuthToken;
  11. use WpBundle\Service\WPAuthService;
  12. /**
  13.  * Class WPAuthListener
  14.  */
  15. class WPAuthListener implements ListenerInterface
  16. {
  17.     /** @var TokenStorage */
  18.     protected $tokenStorage;
  19.     /** @var AuthenticationManagerInterface */
  20.     protected $authenticationManager;
  21.     /** @var WPAuthService */
  22.     protected $wpAuthService;
  23.     /** @var UserService */
  24.     protected $userService;
  25.     /**
  26.      * WPAuthListener constructor.
  27.      * @param TokenStorage $tokenStorage
  28.      * @param AuthenticationManagerInterface $authenticationManager
  29.      * @param WPAuthService $wpAuthService
  30.      * @param UserService $userManager
  31.      */
  32.     public function __construct(
  33.         TokenStorage $tokenStorage,
  34.         AuthenticationManagerInterface $authenticationManager,
  35.         WPAuthService $wpAuthService,
  36.         UserService $userManager
  37.     ) {
  38.         $this->tokenStorage $tokenStorage;
  39.         $this->authenticationManager $authenticationManager;
  40.         $this->wpAuthService $wpAuthService;
  41.         $this->userService $userManager;
  42.     }
  43.     /**
  44.      * @param GetResponseEvent $event
  45.      * @throws \Exception
  46.      */
  47.     public function handle(GetResponseEvent $event)
  48.     {
  49.         try {
  50.             $this->wpAuthService->initUserAndAuthCookie();
  51.             /**
  52.              * @var \WpBundle\Entity\User $wpUser
  53.              */
  54.             if ($wpUser $this->wpAuthService->getUser()) {
  55.                 /**
  56.                  * @var User $user
  57.                  */
  58.                 $user $this->userService->findUserByEmail($wpUser->getEmail());
  59.                 if (!$user) {
  60.                     $user $this->userService->createUser(
  61.                         $wpUser->getId(),
  62.                         $wpUser->getEmail(),
  63.                         $wpUser->getEmail(),
  64.                         uniqid('pass_'true)
  65.                     );
  66.                 }
  67.                 $this->loginUser($user);
  68.             } else {
  69.                 throw new AuthenticationException();
  70.             }
  71.         } catch (\Exception $e) {
  72.             $this->loginUser(null);
  73.         }
  74.     }
  75.     /**
  76.      * @param User $user
  77.      */
  78.     private function loginUser(?User $user)
  79.     {
  80.         if (!$user) {
  81.             $token = new WPAuthToken([
  82.                 'IS_AUTHENTICATED_ANONYMOUSLY'
  83.             ]);
  84.             $token $this->authenticationManager->authenticate($token);
  85.         } else {
  86.             $token = new WPAuthToken($user->getRoles());
  87.             $token->setUser($user);
  88.             $token $this->authenticationManager->authenticate($token);
  89.         }
  90.         $this->tokenStorage->setToken($token);
  91.     }
  92. }