Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

107 lines
3.8KB

  1. <?php
  2. namespace Lc\SovBundle\Authenticator;
  3. use Lc\SovBundle\Model\User\UserInterface;
  4. use Lc\SovBundle\Doctrine\EntityManager;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Security\Core\User\UserInterface as SfUserInterface;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  12. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Security\Core\User\UserProviderInterface;
  15. use Symfony\Component\Security\Csrf\CsrfToken;
  16. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  17. use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
  18. use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;
  19. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  20. class LoginFormAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface
  21. {
  22. use TargetPathTrait;
  23. public const LOGIN_ROUTE = 'login';
  24. private $entityManager;
  25. private $urlGenerator;
  26. private $csrfTokenManager;
  27. private $passwordEncoder;
  28. public function __construct(EntityManager $entityManager, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $passwordEncoder)
  29. {
  30. $this->entityManager = $entityManager;
  31. $this->urlGenerator = $urlGenerator;
  32. $this->csrfTokenManager = $csrfTokenManager;
  33. $this->passwordEncoder = $passwordEncoder;
  34. }
  35. public function supports(Request $request)
  36. {
  37. return self::LOGIN_ROUTE === $request->attributes->get('_route')
  38. && $request->isMethod('POST');
  39. }
  40. public function getCredentials(Request $request)
  41. {
  42. $credentials = [
  43. 'email' => $request->request->get('email'),
  44. 'password' => $request->request->get('password'),
  45. 'csrf_token' => $request->request->get('_csrf_token'),
  46. ];
  47. $request->getSession()->set(
  48. Security::LAST_USERNAME,
  49. $credentials['email']
  50. );
  51. return $credentials;
  52. }
  53. public function getUser($credentials, UserProviderInterface $userProvider)
  54. {
  55. $token = new CsrfToken('authenticate', $credentials['csrf_token']);
  56. if (!$this->csrfTokenManager->isTokenValid($token)) {
  57. throw new InvalidCsrfTokenException();
  58. }
  59. $user = $this->entityManager->getRepository(UserInterface::class)->findOneBy(['email' => $credentials['email']]);
  60. if (!$user) {
  61. // fail authentication with a custom error
  62. throw new CustomUserMessageAuthenticationException('Email could not be found.');
  63. }
  64. return $user;
  65. }
  66. public function checkCredentials($credentials, SfUserInterface $user)
  67. {
  68. return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
  69. }
  70. /**
  71. * Used to upgrade (rehash) the user's password automatically over time.
  72. */
  73. public function getPassword($credentials): ?string
  74. {
  75. return $credentials['password'];
  76. }
  77. public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey)
  78. {
  79. if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
  80. return new RedirectResponse($targetPath);
  81. }
  82. return new RedirectResponse($this->urlGenerator->generate('lc_admin_dashboard'));
  83. }
  84. protected function getLoginUrl()
  85. {
  86. return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  87. }
  88. }