Passport.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;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\BadgeInterface;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CredentialsInterface;
  15. /**
  16. * The default implementation for passports.
  17. *
  18. * @author Wouter de Jong <wouter@wouterj.nl>
  19. *
  20. * @experimental in 5.2
  21. */
  22. class Passport implements UserPassportInterface
  23. {
  24. use PassportTrait;
  25. protected $user;
  26. private $attributes = [];
  27. /**
  28. * @param UserBadge $userBadge
  29. * @param CredentialsInterface $credentials the credentials to check for this authentication, use
  30. * SelfValidatingPassport if no credentials should be checked
  31. * @param BadgeInterface[] $badges
  32. */
  33. public function __construct($userBadge, CredentialsInterface $credentials, array $badges = [])
  34. {
  35. if ($userBadge instanceof UserInterface) {
  36. trigger_deprecation('symfony/security-http', '5.2', 'The 1st argument of "%s" must be an instance of "%s", support for "%s" will be removed in symfony/security-http 5.3.', __CLASS__, UserBadge::class, UserInterface::class);
  37. $this->user = $userBadge;
  38. } elseif ($userBadge instanceof UserBadge) {
  39. $this->addBadge($userBadge);
  40. } else {
  41. throw new \TypeError(sprintf('Argument 1 of "%s" must be an instance of "%s", "%s" given.', __METHOD__, UserBadge::class, get_debug_type($userBadge)));
  42. }
  43. $this->addBadge($credentials);
  44. foreach ($badges as $badge) {
  45. $this->addBadge($badge);
  46. }
  47. }
  48. public function getUser(): UserInterface
  49. {
  50. if (null === $this->user) {
  51. if (!$this->hasBadge(UserBadge::class)) {
  52. throw new \LogicException('Cannot get the Security user, no username or UserBadge configured for this passport.');
  53. }
  54. $this->user = $this->getBadge(UserBadge::class)->getUser();
  55. }
  56. return $this->user;
  57. }
  58. /**
  59. * @param mixed $value
  60. */
  61. public function setAttribute(string $name, $value): void
  62. {
  63. $this->attributes[$name] = $value;
  64. }
  65. /**
  66. * @param mixed $default
  67. *
  68. * @return mixed
  69. */
  70. public function getAttribute(string $name, $default = null)
  71. {
  72. return $this->attributes[$name] ?? $default;
  73. }
  74. }