Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

103 linhas
3.8KB

  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. }