Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

108 rindas
3.8KB

  1. <?php
  2. namespace App\Security;
  3. use App\Entity\User;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  10. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  11. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  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(EntityManagerInterface $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. dump($credentials);
  60. $user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $credentials['email']]);
  61. if (!$user) {
  62. die('ncncn');
  63. // fail authentication with a custom error
  64. throw new CustomUserMessageAuthenticationException('Email could not be found.');
  65. }
  66. return $user;
  67. }
  68. public function checkCredentials($credentials, UserInterface $user)
  69. {
  70. return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
  71. }
  72. /**
  73. * Used to upgrade (rehash) the user's password automatically over time.
  74. */
  75. public function getPassword($credentials): ?string
  76. {
  77. return $credentials['password'];
  78. }
  79. public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey)
  80. {
  81. if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
  82. return new RedirectResponse($targetPath);
  83. }
  84. return new RedirectResponse($this->urlGenerator->generate('lc_admin_dashboard'));
  85. }
  86. protected function getLoginUrl()
  87. {
  88. return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  89. }
  90. }