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

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;
  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. class Passport
  21. {
  22.     protected $user;
  23.     private array $badges = [];
  24.     private array $attributes = [];
  25.     /**
  26.      * @param CredentialsInterface $credentials the credentials to check for this authentication, use
  27.      *                                          SelfValidatingPassport if no credentials should be checked
  28.      * @param BadgeInterface[]     $badges
  29.      */
  30.     public function __construct(UserBadge $userBadgeCredentialsInterface $credentials, array $badges = [])
  31.     {
  32.         $this->addBadge($userBadge);
  33.         $this->addBadge($credentials);
  34.         foreach ($badges as $badge) {
  35.             $this->addBadge($badge);
  36.         }
  37.     }
  38.     public function getUser(): UserInterface
  39.     {
  40.         if (null === $this->user) {
  41.             if (!$this->hasBadge(UserBadge::class)) {
  42.                 throw new \LogicException('Cannot get the Security user, no username or UserBadge configured for this passport.');
  43.             }
  44.             $this->user $this->getBadge(UserBadge::class)->getUser();
  45.         }
  46.         return $this->user;
  47.     }
  48.     public function addBadge(BadgeInterface $badge): static
  49.     {
  50.         $this->badges[\get_class($badge)] = $badge;
  51.         return $this;
  52.     }
  53.     public function hasBadge(string $badgeFqcn): bool
  54.     {
  55.         return isset($this->badges[$badgeFqcn]);
  56.     }
  57.     public function getBadge(string $badgeFqcn): ?BadgeInterface
  58.     {
  59.         return $this->badges[$badgeFqcn] ?? null;
  60.     }
  61.     /**
  62.      * @return array<class-string<BadgeInterface>, BadgeInterface>
  63.      */
  64.     public function getBadges(): array
  65.     {
  66.         return $this->badges;
  67.     }
  68.     public function setAttribute(string $namemixed $value): void
  69.     {
  70.         $this->attributes[$name] = $value;
  71.     }
  72.     public function getAttribute(string $namemixed $default null): mixed
  73.     {
  74.         return $this->attributes[$name] ?? $default;
  75.     }
  76.     public function getAttributes(): array
  77.     {
  78.         return $this->attributes;
  79.     }
  80. }