No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

118 líneas
4.3KB

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