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

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 implements UserPassportInterface
  21. {
  22.     protected $user;
  23.     private $badges = [];
  24.     private $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.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function getUser(): UserInterface
  42.     {
  43.         if (null === $this->user) {
  44.             if (!$this->hasBadge(UserBadge::class)) {
  45.                 throw new \LogicException('Cannot get the Security user, no username or UserBadge configured for this passport.');
  46.             }
  47.             $this->user $this->getBadge(UserBadge::class)->getUser();
  48.         }
  49.         return $this->user;
  50.     }
  51.     /**
  52.      * @return $this
  53.      */
  54.     public function addBadge(BadgeInterface $badge): PassportInterface
  55.     {
  56.         $this->badges[\get_class($badge)] = $badge;
  57.         return $this;
  58.     }
  59.     public function hasBadge(string $badgeFqcn): bool
  60.     {
  61.         return isset($this->badges[$badgeFqcn]);
  62.     }
  63.     public function getBadge(string $badgeFqcn): ?BadgeInterface
  64.     {
  65.         return $this->badges[$badgeFqcn] ?? null;
  66.     }
  67.     /**
  68.      * @return array<class-string<BadgeInterface>, BadgeInterface>
  69.      */
  70.     public function getBadges(): array
  71.     {
  72.         return $this->badges;
  73.     }
  74.     /**
  75.      * @param mixed $value
  76.      */
  77.     public function setAttribute(string $name$value): void
  78.     {
  79.         $this->attributes[$name] = $value;
  80.     }
  81.     /**
  82.      * @param mixed $default
  83.      *
  84.      * @return mixed
  85.      */
  86.     public function getAttribute(string $name$default null)
  87.     {
  88.         return $this->attributes[$name] ?? $default;
  89.     }
  90.     public function getAttributes(): array
  91.     {
  92.         return $this->attributes;
  93.     }
  94. }