<?php
namespace App\Security;
use App\Entity\Persona\Persona;
use App\Entity\Seguridades\Usuario;
use Doctrine\ORM\EntityManagerInterface;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use KnpU\OAuth2ClientBundle\Security\Authenticator\SocialAuthenticator;
use League\OAuth2\Client\Provider\GoogleUser;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class GoogleAuthenticator extends SocialAuthenticator
{
private $clientRegistry;
private $em;
private $router;
public function __construct(ClientRegistry $clientRegistry, EntityManagerInterface $em, RouterInterface $router)
{
$this->clientRegistry = $clientRegistry;
$this->em = $em;
$this->router = $router;
}
public function supports(Request $request)
{
return $request->getPathInfo() == '/connect/google/check' && $request->isMethod('GET');
}
public function getCredentials(Request $request)
{
return $this->fetchAccessToken($this->getGoogleClient());
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
/** @var GoogleUser $googleUser */
$googleUser = $this->getGoogleClient()
->fetchUserFromToken($credentials);
$email = $googleUser->getEmail();
//dump($googleUser);die();
$user = $this->em->getRepository(Usuario::class)->findOneBy(['usuario'=>$email,'usuarioGmail'=>true]);
if(!$user){
$newUser = new Usuario();
$newUser = $this->setUsuarioGmail($newUser, $email);
$this->em->persist($newUser);$this->em->flush();
return $newUser;
}
return $user;
}
private function setUsuarioGmail(Usuario $user,$email){
$user->setUsuario($email)
->setUsuarioGmail(true)
->setActivo(true)
->setBloqueado(false)
->setNuevaClave(false)
->setSuperAdmin(false)
->setIdUsuarioModificacion(0)
->setFechaModificacion(new \DateTime())
->setIpModificacion($_SERVER['REMOTE_ADDR']);
return $user;
}
/**
* @return \knpU\OAuth2ClientBundle\Client\OAuth2Client
*/
private function getGoogleClient()
{
return $this->clientRegistry
->getClient('google');
}
/**
* Return a response that directs the user to authenticate.
*
* This is called when an anonymous request accesses a resource that
* requires authentication. The job of this method is to return someone
* response that "helps" the user start into the authentication process.
*
* Examples:
* A) Form a form login, you might redirect to the login page
* return new RedirectResponse('/login');
* B) For an API token authentication system, you return 401 response return new Response('Auth header required', 401);
*
* @param Request $request The request that resulted in an AuthenticationException
* @param \Symfony\Component\Security\Core\Exception\AuthenticationException $authException The exception that started
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function start(Request $request, AuthenticationException $authException = null)
{
return new RedirectResponse('/');
}
/**
* Called when authentication executed, but failed (e.g. wrong username password).
*
* This should return the Response sent back to the user, like a RedirectResponse
* to the login page or a 403 response.
*
* If you return null, the request will continue, but the user will
* not be authenticated. This is probably not what you want to do.
*
* @param Request $request
* @param AuthenticationException $exception
*
* @return \Symfony\Component\HttpFoundation\Response|null
*/
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
//
}
/**
* Called when authentication is successful
*
* This should return the Response sent back to the user, like a RedirectResponse
* to the last page they visited.
*
* If you retrun null, the current request will continue, and the user
* will be authenticated. This makes sense, for example, with an API.
*
* @param Request $request
* @param \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token
* @param string $providerKey The provider (i.e. firewall) key
*
* @return void
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey)
{
//
}
}