You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoginFormAuthenticator.php 3.8KB

3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Lc\SovBundle\Authenticator;
  3. use Lc\SovBundle\Repository\User\UserStore;
  4. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  5. use Symfony\Component\Form\FormFactoryInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Security;
  12. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  17. use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
  18. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  19. class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
  20. {
  21. use TargetPathTrait;
  22. public const LOGIN_ROUTE = 'sov_login';
  23. protected UrlGeneratorInterface $urlGenerator;
  24. protected UserStore $userStore;
  25. protected FormFactoryInterface $formFactory;
  26. protected ParameterBagInterface $parameterBag;
  27. public function __construct(
  28. UrlGeneratorInterface $urlGenerator,
  29. UserStore $userStore,
  30. FormFactoryInterface $formFactory,
  31. ParameterBagInterface $parameterBag
  32. ) {
  33. $this->urlGenerator = $urlGenerator;
  34. $this->userStore = $userStore;
  35. $this->formFactory = $formFactory;
  36. $this->parameterBag = $parameterBag;
  37. }
  38. public function supports(Request $request): bool
  39. {
  40. return $request->isMethod('POST') && $this->getLoginUrl($request) === $request->getPathInfo();
  41. }
  42. public function authenticate(Request $request): PassportInterface
  43. {
  44. $email = $request->request->get('email');
  45. $password = $request->request->get('password');
  46. $csrfToken = $request->request->get('_csrf_token');
  47. return new Passport(
  48. new UserBadge($email, function ($userIdentifier) {
  49. return $this->userStore->getOneByEmail($userIdentifier);
  50. }),
  51. new PasswordCredentials($password),
  52. [new CsrfTokenBadge('authenticate', $csrfToken)]
  53. );
  54. }
  55. public function onAuthenticationSuccess(
  56. Request $request,
  57. TokenInterface $token,
  58. string $providerKey
  59. ): RedirectResponse {
  60. $routeName = 'home';
  61. $email = $request->request->get('email');
  62. $loginRedirection = $this->parameterBag->get('lc_sov.login_redirection');
  63. $useReferer = $loginRedirection['redirect_referer'];
  64. $rolesRedirection = $loginRedirection['roles_redirection'];
  65. if (isset($useReferer) && $useReferer == true) {
  66. $url = $request->request->get('_target_path');
  67. } else {
  68. $user = $this->userStore->getOneByEmail($email);
  69. if (!empty($user)) {
  70. $roles = $user->getRoles();
  71. foreach ($rolesRedirection as $roleRedirect) {
  72. if (in_array($roleRedirect['role'], $roles)) {
  73. $routeName = $roleRedirect['redirect'];
  74. }
  75. }
  76. }
  77. }
  78. if (isset($url) && !empty($url)) {
  79. return new RedirectResponse($url);
  80. } else {
  81. return new RedirectResponse($this->urlGenerator->generate($routeName));
  82. }
  83. }
  84. protected function getLoginUrl(Request $request): string
  85. {
  86. return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  87. }
  88. }