UserBadge.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\UsernameNotFoundException;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. use Symfony\Component\Security\Http\EventListener\UserProviderListener;
  14. /**
  15. * Represents the user in the authentication process.
  16. *
  17. * It uses an identifier (e.g. email, or username) and
  18. * "user loader" to load the related User object.
  19. *
  20. * @author Wouter de Jong <wouter@wouterj.nl>
  21. *
  22. * @experimental in 5.2
  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 a UsernameNotFoundException
  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. public function getUser(): UserInterface
  52. {
  53. if (null === $this->user) {
  54. if (null === $this->userLoader) {
  55. throw new \LogicException(sprintf('No user loader is configured, did you forget to register the "%s" listener?', UserProviderListener::class));
  56. }
  57. $this->user = ($this->userLoader)($this->userIdentifier);
  58. if (!$this->user instanceof UserInterface) {
  59. throw new UsernameNotFoundException();
  60. }
  61. }
  62. return $this->user;
  63. }
  64. public function getUserLoader(): ?callable
  65. {
  66. return $this->userLoader;
  67. }
  68. public function setUserLoader(callable $userLoader): void
  69. {
  70. $this->userLoader = $userLoader;
  71. }
  72. public function isResolved(): bool
  73. {
  74. return true;
  75. }
  76. }