vendor/kematjaya/user-bundle/src/Security/KmjLoginAuthenticator.php line 141

Open in your IDE?
  1. <?php
  2. namespace Kematjaya\UserBundle\Security;
  3. use Kematjaya\UserBundle\Form\LoginType;
  4. use Kematjaya\UserBundle\Repo\KmjUserRepoInterface;
  5. use Kematjaya\UserBundle\Config\RoutingConfigurationFactoryInterface;
  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\PasswordHasher\Hasher\UserPasswordHasherInterface;
  12. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. use Symfony\Component\Security\Core\User\UserProviderInterface;
  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. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  21. use Symfony\Component\DependencyInjection\ContainerInterface;
  22. use Symfony\Component\Form\FormInterface;
  23. use Symfony\Component\Form\FormError;
  24. /**
  25.  * @deprecated since version 4.2.0, use Kematjaya\UserBundle\Security\FormLoginAuthenticator instead
  26.  */
  27. class KmjLoginAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface
  28. {
  29.     use TargetPathTrait;
  30.     public const LOGIN_ROUTE 'kmj_user_login';
  31.     /**
  32.      * 
  33.      * @var array
  34.      */
  35.     private $config;
  36.     
  37.     /**
  38.      * 
  39.      * @var KmjUserRepoInterface
  40.      */
  41.     private $kmjUserRepo;
  42.     
  43.     /**
  44.      * 
  45.      * @var UrlGeneratorInterface
  46.      */
  47.     private $urlGenerator;
  48.     
  49.     /**
  50.      * 
  51.      * @var CsrfTokenManagerInterface
  52.      */
  53.     private $csrfTokenManager;
  54.     
  55.     /**
  56.      * 
  57.      * @var UserPasswordHasherInterface
  58.      */
  59.     private $passwordEncoder;
  60.     
  61.     /**
  62.      * 
  63.      * @var ContainerInterface
  64.      */
  65.     private $container;
  66.     
  67.     private $loginRoute;
  68.     
  69.     /**
  70.      * 
  71.      * @var string
  72.      */
  73.     private $error;
  74.     /**
  75.      * 
  76.      * @var RoutingConfigurationFactoryInterface
  77.      */
  78.     private $routingConfigurationFactory;
  79.     
  80.     public function __construct(
  81.         ContainerBagInterface $containerBag,
  82.         ContainerInterface $container,
  83.         KmjUserRepoInterface $kmjUserRepo
  84.         UrlGeneratorInterface $urlGenerator
  85.         CsrfTokenManagerInterface $csrfTokenManager
  86.         UserPasswordHasherInterface $passwordEncoder,
  87.             RoutingConfigurationFactoryInterface $routingConfigurationFactory
  88.     ) {
  89.         $this->container $container;
  90.         $this->config $containerBag->get('user');
  91.         $this->kmjUserRepo $kmjUserRepo;
  92.         $this->urlGenerator $urlGenerator;
  93.         $this->csrfTokenManager $csrfTokenManager;
  94.         $this->passwordEncoder $passwordEncoder;
  95.         $this->loginRoute $this->config['route']['login'];
  96.         $this->routingConfigurationFactory $routingConfigurationFactory;
  97.     }
  98.     public function supports(Request $request):bool
  99.     {
  100.         return $this->loginRoute === $request->attributes->get('_route')
  101.             && $request->isMethod(Request::METHOD_POST);
  102.     }
  103.     /**
  104.      * 
  105.      * @param Request $request
  106.      * @return mixed
  107.      */
  108.     public function getCredentials(Request $request)
  109.     {
  110.         $form $this->createForm(LoginType::class);
  111.         $form->handleRequest($request);
  112.         if (!$form->isValid()) {
  113.             
  114.             $this->error $this->getErrors($form);
  115.         }
  116.         
  117.         $credentials $form->getData();
  118.         
  119.         $request->getSession()->set(
  120.             Security::LAST_USERNAME,
  121.             $credentials['username']
  122.         );
  123.         return $credentials;
  124.     }
  125.     public function getUser($credentialsUserProviderInterface $userProvider):?UserInterface
  126.     {
  127.         if (null !== $this->error) {
  128.             throw new CustomUserMessageAuthenticationException($this->error);
  129.         }
  130.         
  131.         $user $this->kmjUserRepo->findOneByUsernameAndActive($credentials['username']);
  132.         if (!$user) {
  133.             
  134.             throw new CustomUserMessageAuthenticationException('Username could not be found.');
  135.         }
  136.         return $user;
  137.     }
  138.     public function checkCredentials($credentialsUserInterface $user):bool
  139.     {
  140.         return $this->passwordEncoder->isPasswordValid($user$credentials['password']);
  141.     }
  142.     /**
  143.      * Used to upgrade (rehash) the user's password automatically over time.
  144.      */
  145.     public function getPassword($credentials): ?string
  146.     {
  147.         return $credentials['password'];
  148.     }
  149.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$providerKey):?Response
  150.     {
  151.         $redirectPath $this->routingConfigurationFactory->getLoginSuccessRedirectPath($token->getUser()->getRoles());
  152.         $this->saveTargetPath($request->getSession(), $providerKey$this->urlGenerator->generate($redirectPath));
  153.         $targetPath $this->getTargetPath($request->getSession(), $providerKey);
  154.         if ($targetPath) {
  155.             
  156.             return new RedirectResponse($targetPath);
  157.         }
  158.         throw new RedirectResponse('homepage');
  159.     }
  160.     protected function getLoginUrl():string
  161.     {
  162.         return $this->urlGenerator->generate($this->loginRoute);
  163.     }
  164.     
  165.     protected function createForm(string $className$data null): FormInterface
  166.     {
  167.         return $this->container->get('form.factory')->create($className$data);
  168.     }
  169.     
  170.     protected function getErrors(FormInterface $form):?string 
  171.     {
  172.         $errors = [];
  173.         foreach ($form->getErrors(true) as $error) {
  174.             if (!$error instanceof FormError) {
  175.                 
  176.                 continue;
  177.             }
  178.             $errors[] = sprintf("%s %s"$error->getOrigin() ? $error->getOrigin()->getName() . ': ' ''$error->getMessage());
  179.         }
  180.         
  181.         return implode(", "$errors);
  182.     }
  183. }