src/Security/GoogleAuthenticator.php line 18

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