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

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
  25. {
  26.     protected $user;
  27.     private array $badges = [];
  28.     private array $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.     public function getUser(): UserInterface
  43.     {
  44.         if (null === $this->user) {
  45.             if (!$this->hasBadge(UserBadge::class)) {
  46.                 throw new \LogicException('Cannot get the Security user, no username or UserBadge configured for this passport.');
  47.             }
  48.             $this->user $this->getBadge(UserBadge::class)->getUser();
  49.         }
  50.         return $this->user;
  51.     }
  52.     /**
  53.      * Adds a new security badge.
  54.      *
  55.      * A passport can hold only one instance of the same security badge.
  56.      * This method replaces the current badge if it is already set on this
  57.      * passport.
  58.      *
  59.      * @return $this
  60.      */
  61.     public function addBadge(BadgeInterface $badge): static
  62.     {
  63.         $this->badges[\get_class($badge)] = $badge;
  64.         return $this;
  65.     }
  66.     public function hasBadge(string $badgeFqcn): bool
  67.     {
  68.         return isset($this->badges[$badgeFqcn]);
  69.     }
  70.     public function getBadge(string $badgeFqcn): ?BadgeInterface
  71.     {
  72.         return $this->badges[$badgeFqcn] ?? null;
  73.     }
  74.     /**
  75.      * @return array<class-string<BadgeInterface>, BadgeInterface>
  76.      */
  77.     public function getBadges(): array
  78.     {
  79.         return $this->badges;
  80.     }
  81.     public function setAttribute(string $namemixed $value): void
  82.     {
  83.         $this->attributes[$name] = $value;
  84.     }
  85.     public function getAttribute(string $namemixed $default null): mixed
  86.     {
  87.         return $this->attributes[$name] ?? $default;
  88.     }
  89.     public function getAttributes(): array
  90.     {
  91.         return $this->attributes;
  92.     }
  93. }