vendor/symfony/security-http/Authenticator/Passport/Badge/UserBadge.php line 28

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\Http\Authenticator\Passport\Badge;
  11. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  12. use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
  13. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. use Symfony\Component\Security\Http\EventListener\UserProviderListener;
  16. /**
  17. * Represents the user in the authentication process.
  18. *
  19. * It uses an identifier (e.g. email, or username) and
  20. * "user loader" to load the related User object.
  21. *
  22. * @author Wouter de Jong <wouter@wouterj.nl>
  23. */
  24. class UserBadge implements BadgeInterface
  25. {
  26. private $userIdentifier;
  27. private $userLoader;
  28. private $user;
  29. /**
  30. * Initializes the user badge.
  31. *
  32. * You must provide a $userIdentifier. This is a unique string representing the
  33. * user for this authentication (e.g. the email if authentication is done using
  34. * email + password; or a string combining email+company if authentication is done
  35. * based on email *and* company name). This string can be used for e.g. login throttling.
  36. *
  37. * Optionally, you may pass a user loader. This callable receives the $userIdentifier
  38. * as argument and must return a UserInterface object (otherwise an AuthenticationServiceException
  39. * is thrown). If this is not set, the default user provider will be used with
  40. * $userIdentifier as username.
  41. */
  42. public function __construct(string $userIdentifier, ?callable $userLoader = null)
  43. {
  44. $this->userIdentifier = $userIdentifier;
  45. $this->userLoader = $userLoader;
  46. }
  47. public function getUserIdentifier(): string
  48. {
  49. return $this->userIdentifier;
  50. }
  51. /**
  52. * @throws AuthenticationException when the user cannot be found
  53. */
  54. public function getUser(): UserInterface
  55. {
  56. if (null !== $this->user) {
  57. return $this->user;
  58. }
  59. if (null === $this->userLoader) {
  60. throw new \LogicException(sprintf('No user loader is configured, did you forget to register the "%s" listener?', UserProviderListener::class));
  61. }
  62. $user = ($this->userLoader)($this->userIdentifier);
  63. // No user has been found via the $this->userLoader callback
  64. if (null === $user) {
  65. $exception = new UserNotFoundException();
  66. $exception->setUserIdentifier($this->userIdentifier);
  67. throw $exception;
  68. }
  69. if (!$user instanceof UserInterface) {
  70. throw new AuthenticationServiceException(sprintf('The user provider must return a UserInterface object, "%s" given.', get_debug_type($user)));
  71. }
  72. return $this->user = $user;
  73. }
  74. public function getUserLoader(): ?callable
  75. {
  76. return $this->userLoader;
  77. }
  78. public function setUserLoader(callable $userLoader): void
  79. {
  80. $this->userLoader = $userLoader;
  81. }
  82. public function isResolved(): bool
  83. {
  84. return true;
  85. }
  86. }