vendor/symfony/security-core/Authentication/Provider/DaoAuthenticationProvider.php line 85

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Core\Authentication\Provider;
  11. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  12. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  13. use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
  14. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  15. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  16. use Symfony\Component\Security\Core\User\UserCheckerInterface;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. use Symfony\Component\Security\Core\User\UserProviderInterface;
  19. /**
  20.  * DaoAuthenticationProvider uses a UserProviderInterface to retrieve the user
  21.  * for a UsernamePasswordToken.
  22.  *
  23.  * @author Fabien Potencier <fabien@symfony.com>
  24.  */
  25. class DaoAuthenticationProvider extends UserAuthenticationProvider
  26. {
  27.     private $encoderFactory;
  28.     private $userProvider;
  29.     public function __construct(UserProviderInterface $userProviderUserCheckerInterface $userCheckerstring $providerKeyEncoderFactoryInterface $encoderFactorybool $hideUserNotFoundExceptions true)
  30.     {
  31.         parent::__construct($userChecker$providerKey$hideUserNotFoundExceptions);
  32.         $this->encoderFactory $encoderFactory;
  33.         $this->userProvider $userProvider;
  34.     }
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     protected function checkAuthentication(UserInterface $userUsernamePasswordToken $token)
  39.     {
  40.         $currentUser $token->getUser();
  41.         if ($currentUser instanceof UserInterface) {
  42.             if ($currentUser->getPassword() !== $user->getPassword()) {
  43.                 throw new BadCredentialsException('The credentials were changed from another session.');
  44.             }
  45.         } else {
  46.             if ('' === ($presentedPassword $token->getCredentials())) {
  47.                 throw new BadCredentialsException('The presented password cannot be empty.');
  48.             }
  49.             if (!$this->encoderFactory->getEncoder($user)->isPasswordValid($user->getPassword(), $presentedPassword$user->getSalt())) {
  50.                 throw new BadCredentialsException('The presented password is invalid.');
  51.             }
  52.         }
  53.     }
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     protected function retrieveUser($usernameUsernamePasswordToken $token)
  58.     {
  59.         $user $token->getUser();
  60.         if ($user instanceof UserInterface) {
  61.             return $user;
  62.         }
  63.         try {
  64.             $user $this->userProvider->loadUserByUsername($username);
  65.             if (!$user instanceof UserInterface) {
  66.                 throw new AuthenticationServiceException('The user provider must return a UserInterface object.');
  67.             }
  68.             return $user;
  69.         } catch (UsernameNotFoundException $e) {
  70.             $e->setUsername($username);
  71.             throw $e;
  72.         } catch (\Exception $e) {
  73.             $e = new AuthenticationServiceException($e->getMessage(), 0$e);
  74.             $e->setToken($token);
  75.             throw $e;
  76.         }
  77.     }
  78. }