DaoAuthenticationProvider.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Symfony\Component\Security\Authentication\Provider;
  3. use Symfony\Component\Security\User\UserProviderInterface;
  4. use Symfony\Component\Security\User\AccountCheckerInterface;
  5. use Symfony\Component\Security\User\AccountInterface;
  6. use Symfony\Component\Security\Encoder\PasswordEncoderInterface;
  7. use Symfony\Component\Security\Encoder\PlaintextPasswordEncoder;
  8. use Symfony\Component\Security\Exception\UsernameNotFoundException;
  9. use Symfony\Component\Security\Exception\AuthenticationServiceException;
  10. use Symfony\Component\Security\Exception\BadCredentialsException;
  11. use Symfony\Component\Security\Authentication\Token\UsernamePasswordToken;
  12. /*
  13. * This file is part of the Symfony package.
  14. *
  15. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  16. *
  17. * For the full copyright and license information, please view the LICENSE
  18. * file that was distributed with this source code.
  19. */
  20. /**
  21. * DaoAuthenticationProvider uses a UserProviderInterface to retrieve the user
  22. * for a UsernamePasswordToken.
  23. *
  24. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  25. */
  26. class DaoAuthenticationProvider extends UserAuthenticationProvider
  27. {
  28. protected $passwordEncoder;
  29. protected $userProvider;
  30. /**
  31. * Constructor.
  32. *
  33. * @param UserProviderInterface $userProvider A UserProviderInterface instance
  34. * @param AccountCheckerInterface $accountChecker An AccountCheckerInterface instance
  35. * @param PasswordEncoderInterface $passwordEncoder A PasswordEncoderInterface instance
  36. */
  37. public function __construct(UserProviderInterface $userProvider, AccountCheckerInterface $accountChecker, PasswordEncoderInterface $passwordEncoder = null, $hideUserNotFoundExceptions = true)
  38. {
  39. parent::__construct($accountChecker, $hideUserNotFoundExceptions);
  40. if (null === $passwordEncoder) {
  41. $passwordEncoder = new PlaintextPasswordEncoder();
  42. }
  43. $this->passwordEncoder = $passwordEncoder;
  44. $this->userProvider = $userProvider;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function checkAuthentication(AccountInterface $account, UsernamePasswordToken $token)
  50. {
  51. if (!$presentedPassword = (string) $token->getCredentials()) {
  52. throw new BadCredentialsException('Bad credentials');
  53. }
  54. if (!$this->passwordEncoder->isPasswordValid($account->getPassword(), $presentedPassword, $account->getSalt())) {
  55. throw new BadCredentialsException('Bad credentials');
  56. }
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function retrieveUser($username, UsernamePasswordToken $token)
  62. {
  63. $user = null;
  64. try {
  65. $user = $this->userProvider->loadUserByUsername($username);
  66. } catch (UsernameNotFoundException $notFound) {
  67. throw $notFound;
  68. } catch (\Exception $repositoryProblem) {
  69. throw new AuthenticationServiceException($repositoryProblem->getMessage(), $token, 0, $repositoryProblem);
  70. }
  71. if (!$user instanceof AccountInterface) {
  72. throw new AuthenticationServiceException('The user provider must return an AccountInterface object.');
  73. }
  74. return $user;
  75. }
  76. }