src/Security/GoogleAuthenticator.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Seguridades\Usuario;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  6. use KnpU\OAuth2ClientBundle\Security\Authenticator\SocialAuthenticator;
  7. use League\OAuth2\Client\Provider\GoogleUser;
  8. use Symfony\Component\HttpFoundation\{RedirectResponseRequest};
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Symfony\Component\Security\Core\User\UserProviderInterface;
  11. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. class GoogleAuthenticator extends SocialAuthenticator
  14. {
  15.     private $clientRegistry;
  16.     private $em;
  17.     private $router;
  18.     public function __construct(ClientRegistry $clientRegistryEntityManagerInterface $emRouterInterface $router)
  19.     {
  20.         $this->clientRegistry $clientRegistry;
  21.         $this->em $em;
  22.         $this->router $router;
  23.     }
  24.     public function supports(Request $request)
  25.     {
  26.         return $request->getPathInfo() == '/connect/google/check' && $request->isMethod('GET');
  27.     }
  28.     public function getCredentials(Request $request)
  29.     {
  30.         return $this->fetchAccessToken($this->getGoogleClient());
  31.     }
  32.     public function getUser($credentialsUserProviderInterface $userProvider)
  33.     {
  34.         /** @var GoogleUser $googleUser */
  35.         $googleUser $this->getGoogleClient()
  36.             ->fetchUserFromToken($credentials);
  37.         $email $googleUser->getEmail();
  38.         //dump($googleUser);die();
  39.         $user $this->em->getRepository(Usuario::class)->findOneBy(['usuario'=>$email,'usuarioGmail'=>true]);
  40.         if(!$user){
  41.             $newUser = new Usuario();
  42.             $newUser $this->setUsuarioGmail($newUser$email);
  43.             $this->em->persist($newUser);$this->em->flush();
  44.             return $newUser;
  45.         }
  46.         return $user;
  47.     }
  48.     private function setUsuarioGmail(Usuario $user,$email){
  49.         $user->setUsuario($email)
  50.             ->setUsuarioGmail(true)
  51.             ->setActivo(true)
  52.             ->setBloqueado(false)
  53.             ->setNuevaClave(false)
  54.             ->setSuperAdmin(false)
  55.             ->setIdUsuarioModificacion(0)
  56.             ->setFechaModificacion(new \DateTime())
  57.             ->setIpModificacion($_SERVER['REMOTE_ADDR']);
  58.         return $user;
  59.     }
  60.     /**
  61.      * @return \knpU\OAuth2ClientBundle\Client\OAuth2Client
  62.      */
  63.     private function getGoogleClient()
  64.     {
  65.         return $this->clientRegistry
  66.             ->getClient('google');
  67.     } 
  68.     /**
  69.      * Return a response that directs the user to authenticate.
  70.      * 
  71.      * This is called when an anonymous request accesses a resource that
  72.      * requires authentication. The job of this method is to return someone
  73.      * response that "helps" the user start into the authentication process.
  74.      * 
  75.      * Examples:
  76.      * A) Form a form login, you might redirect to the login page
  77.      *  return new RedirectResponse('/login');
  78.      * B) For an API token authentication system, you return 401 response return new Response('Auth header required', 401);
  79.      * 
  80.      * @param Request $request The request that resulted in an AuthenticationException
  81.      * @param \Symfony\Component\Security\Core\Exception\AuthenticationException $authException The exception that started
  82.      * 
  83.      * @return \Symfony\Component\HttpFoundation\Response
  84.      */
  85.     public function start(Request $requestAuthenticationException $authException null)
  86.     {
  87.         return new RedirectResponse('/');
  88.     }
  89.     /**
  90.      * Called when authentication executed, but failed (e.g. wrong username password).
  91.      *  
  92.      * This should return the Response sent back to the user, like a RedirectResponse
  93.      * to the login page or a 403 response.
  94.      * 
  95.      * If you return null, the request will continue, but the user will
  96.      * not be authenticated. This is probably not what you want to do.
  97.      * 
  98.      * @param Request $request
  99.      * @param AuthenticationException $exception
  100.      * 
  101.      * @return \Symfony\Component\HttpFoundation\Response|null
  102.      */
  103.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception)
  104.     {
  105.         //
  106.     }
  107.     /**
  108.      * Called when authentication is successful
  109.      * 
  110.      * This should return the Response sent back to the user, like a RedirectResponse
  111.      * to the last page they visited. 
  112.      * 
  113.      * If you retrun null, the current request will continue, and the user  
  114.      * will be authenticated. This makes sense, for example, with an API. 
  115.      * 
  116.      * @param Request $request 
  117.      * @param \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token
  118.      * @param string $providerKey The provider (i.e. firewall) key
  119.      * 
  120.      * @return void  
  121.      */
  122.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $providerKey)
  123.     {
  124.         //
  125.     }
  126. }