vendor/symfony/security-http/Authenticator/Passport/Passport.php line 52

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  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.  * A Passport contains all security-related information that needs to be
  17.  * validated during authentication.
  18.  *
  19.  * A passport badge can be used to add any additional information to the
  20.  * passport.
  21.  *
  22.  * @author Wouter de Jong <[email protected]>
  23.  */
  24. class Passport implements UserPassportInterface
  25. {
  26.     protected $user;
  27.     private $badges = [];
  28.     private $attributes = [];
  29.     /**
  30.      * @param CredentialsInterface $credentials the credentials to check for this authentication, use
  31.      *                                          SelfValidatingPassport if no credentials should be checked
  32.      * @param BadgeInterface[]     $badges
  33.      */
  34.     public function __construct(UserBadge $userBadgeCredentialsInterface $credentials, array $badges = [])
  35.     {
  36.         $this->addBadge($userBadge);
  37.         $this->addBadge($credentials);
  38.         foreach ($badges as $badge) {
  39.             $this->addBadge($badge);
  40.         }
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function getUser(): UserInterface
  46.     {
  47.         if (null === $this->user) {
  48.             if (!$this->hasBadge(UserBadge::class)) {
  49.                 throw new \LogicException('Cannot get the Security user, no username or UserBadge configured for this passport.');
  50.             }
  51.             $this->user $this->getBadge(UserBadge::class)->getUser();
  52.         }
  53.         return $this->user;
  54.     }
  55.     /**
  56.      * Adds a new security badge.
  57.      *
  58.      * A passport can hold only one instance of the same security badge.
  59.      * This method replaces the current badge if it is already set on this
  60.      * passport.
  61.      *
  62.      * @return $this
  63.      */
  64.     public function addBadge(BadgeInterface $badge): PassportInterface
  65.     {
  66.         $this->badges[\get_class($badge)] = $badge;
  67.         return $this;
  68.     }
  69.     public function hasBadge(string $badgeFqcn): bool
  70.     {
  71.         return isset($this->badges[$badgeFqcn]);
  72.     }
  73.     public function getBadge(string $badgeFqcn): ?BadgeInterface
  74.     {
  75.         return $this->badges[$badgeFqcn] ?? null;
  76.     }
  77.     /**
  78.      * @return array<class-string<BadgeInterface>, BadgeInterface>
  79.      */
  80.     public function getBadges(): array
  81.     {
  82.         return $this->badges;
  83.     }
  84.     /**
  85.      * @param mixed $value
  86.      */
  87.     public function setAttribute(string $name$value): void
  88.     {
  89.         $this->attributes[$name] = $value;
  90.     }
  91.     /**
  92.      * @param mixed $default
  93.      *
  94.      * @return mixed
  95.      */
  96.     public function getAttribute(string $name$default null)
  97.     {
  98.         return $this->attributes[$name] ?? $default;
  99.     }
  100.     public function getAttributes(): array
  101.     {
  102.         return $this->attributes;
  103.     }
  104. }