<?php
namespace App\Security;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
class UserAppAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface
{
use TargetPathTrait;
public const LOGIN_ROUTE = 'app_login';
private $entityManager;
private $urlGenerator;
private $csrfTokenManager;
private $passwordEncoder;
/**
* @var SessionInterface
*/
private $session;
public function __construct(EntityManagerInterface $entityManager, SessionInterface $session, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $passwordEncoder)
{
$this->entityManager = $entityManager;
$this->urlGenerator = $urlGenerator;
$this->csrfTokenManager = $csrfTokenManager;
$this->passwordEncoder = $passwordEncoder;
$this->session = $session;
}
public function supports(Request $request)
{
if (self::LOGIN_ROUTE === $request->attributes->get('_route') && $request->get('user') === null && $request->isMethod('POST')) {
return self::LOGIN_ROUTE === $request->attributes->get('_route');
}
}
public function getCredentials(Request $request)
{
$credentials = [
'email' => $request->request->get('email'),
'password' => $request->request->get('password'),
'csrf_token' => $request->request->get('_csrf_token'),
'objectif' => $request->request->get('objectif'),
];
$request->getSession()->set(
Security::LAST_USERNAME,
$credentials['email']
);
return $credentials;
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
$token = new CsrfToken('authenticate', $credentials['csrf_token']);
if (!$this->csrfTokenManager->isTokenValid($token)) {
throw new InvalidCsrfTokenException();
}
//$this->entityManager->getUnitOfWork()->clear(User::class);
$user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $credentials['email']]);
if (!$user) {
// fail authentication with a custom error
throw new CustomUserMessageAuthenticationException('Adresse mail non trouvée.');
}
if (!$user->getIsValid()) {
throw new CustomUserMessageAuthenticationException('Votre compte est en attente de validation.');
}
return $user;
}
public function checkCredentials($credentials, UserInterface $user)
{
return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
}
/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function getPassword($credentials): ?string
{
return $credentials['password'];
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
$redirectUrl = $request->query->get('redirect', null);
if ($redirectUrl) {
return new RedirectResponse($redirectUrl);
}
$objectif = $request->request->get('objectif');
if ($objectif) {
$this->session->set('objectif', $objectif);
}
if ($this->session->get('commandInCurse')) {
$this->session->remove('commandInCurse');
return new RedirectResponse('/commande/livraison');
}
if ($token->getUser()->getRoles()[0] === 'ROLE_USER') {
$user = $token->getUser();
$user->setStatuCon(true);
$this->entityManager->persist($user);
$this->entityManager->flush();
if ($objectif === "experience") {
return new RedirectResponse('/reseau-sociaux');
}
return new RedirectResponse('/mon-espace-client/dashboard');
} elseif ($token->getUser()->getRoles()[0] === 'ROLE_ADMIN') {
return new RedirectResponse('/admin/dashboard');
} else {
return new RedirectResponse('/');
}
}
protected function getLoginUrl()
{
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
}
}