UserChecker.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\User;
  11. use Symfony\Component\Security\Core\Exception\AccountExpiredException;
  12. use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
  13. use Symfony\Component\Security\Core\Exception\DisabledException;
  14. use Symfony\Component\Security\Core\Exception\LockedException;
  15. /**
  16. * UserChecker checks the user account flags.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class UserChecker implements UserCheckerInterface
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function checkPreAuth(UserInterface $user)
  26. {
  27. if (!$user instanceof User) {
  28. return;
  29. }
  30. if (!$user->isAccountNonLocked()) {
  31. $ex = new LockedException('User account is locked.');
  32. $ex->setUser($user);
  33. throw $ex;
  34. }
  35. if (!$user->isEnabled()) {
  36. $ex = new DisabledException('User account is disabled.');
  37. $ex->setUser($user);
  38. throw $ex;
  39. }
  40. if (!$user->isAccountNonExpired()) {
  41. $ex = new AccountExpiredException('User account has expired.');
  42. $ex->setUser($user);
  43. throw $ex;
  44. }
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function checkPostAuth(UserInterface $user)
  50. {
  51. if (!$user instanceof User) {
  52. return;
  53. }
  54. if (!$user->isCredentialsNonExpired()) {
  55. $ex = new CredentialsExpiredException('User credentials have expired.');
  56. $ex->setUser($user);
  57. throw $ex;
  58. }
  59. }
  60. }