src/Security/AdminAuthenticator.php line 85

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\User;
  4. use DateTime;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  12. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. use Symfony\Component\Security\Core\User\UserProviderInterface;
  16. use Symfony\Component\Security\Csrf\CsrfToken;
  17. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  18. use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
  19. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. class AdminAuthenticator extends AbstractFormLoginAuthenticator
  22. {
  23.     use TargetPathTrait;
  24.     private EntityManagerInterface $em;
  25.     private RouterInterface $router;
  26.     private CsrfTokenManagerInterface $csrfTokenManager;
  27.     private UserPasswordEncoderInterface $passwordEncoder;
  28.     private TranslatorInterface $translator;
  29.     public function __construct(
  30.         EntityManagerInterface $em,
  31.         RouterInterface $router,
  32.         CsrfTokenManagerInterface $csrfTokenManager,
  33.         UserPasswordEncoderInterface $passwordEncoder,
  34.         TranslatorInterface $translator
  35.     ) {
  36.         $this->em $em;
  37.         $this->router $router;
  38.         $this->csrfTokenManager $csrfTokenManager;
  39.         $this->passwordEncoder $passwordEncoder;
  40.         $this->translator $translator;
  41.     }
  42.     public function supports(Request $request): bool
  43.     {
  44.         return 'admin_login' === $request->attributes->get('_route')
  45.             && $request->isMethod('POST');
  46.     }
  47.     public function getCredentials(Request $request): array
  48.     {
  49.         $credentials = [
  50.             'email' => $request->request->get('email'),
  51.             'password' => $request->request->get('password'),
  52.             'csrf_token' => $request->request->get('_csrf_token'),
  53.         ];
  54.         $request->getSession()->set(
  55.             Security::LAST_USERNAME,
  56.             $credentials['email']
  57.         );
  58.         return $credentials;
  59.     }
  60.     public function getUser($credentialsUserProviderInterface $userProvider)
  61.     {
  62.         $token = new CsrfToken('authenticate'$credentials['csrf_token']);
  63.         if (!$this->csrfTokenManager->isTokenValid($token)) {
  64.             throw new InvalidCsrfTokenException();
  65.         }
  66.         $user $this->em->getRepository(User::class)->findOneBy(['email' => $credentials['email']]);
  67.         if (!$user or !$user->isActive() or (!$user->isAdmin() and !$user->isContributor() and !$user->isCreator())) {
  68.             throw new CustomUserMessageAuthenticationException($this->translator->trans('authenticator.invalidGrant'));
  69.         }
  70.         return $user;
  71.     }
  72.     public function checkCredentials($credentialsUserInterface $user): bool
  73.     {
  74.         return $this->passwordEncoder->isPasswordValid($user$credentials['password']);
  75.     }
  76.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$providerKey): RedirectResponse
  77.     {
  78.         /** @var User $user */
  79.         $user $token->getUser();
  80.         $user->setLastConnection(new DateTime());
  81.         $this->em->flush();
  82.         if ($targetPath $this->getTargetPath($request->getSession(), $providerKey)) {
  83.             return new RedirectResponse($targetPath);
  84.         }
  85.         return new RedirectResponse($this->router->generate('admin_index'));
  86.     }
  87.     protected function getLoginUrl(): string
  88.     {
  89.         return $this->router->generate('admin_login');
  90.     }
  91. }