PassportTrait.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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\Exception\BadCredentialsException;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\BadgeInterface;
  13. /**
  14. * @author Wouter de Jong <wouter@wouterj.nl>
  15. *
  16. * @experimental in 5.2
  17. */
  18. trait PassportTrait
  19. {
  20. /**
  21. * @var BadgeInterface[]
  22. */
  23. private $badges = [];
  24. public function addBadge(BadgeInterface $badge): PassportInterface
  25. {
  26. $this->badges[\get_class($badge)] = $badge;
  27. return $this;
  28. }
  29. public function hasBadge(string $badgeFqcn): bool
  30. {
  31. return isset($this->badges[$badgeFqcn]);
  32. }
  33. public function getBadge(string $badgeFqcn): ?BadgeInterface
  34. {
  35. return $this->badges[$badgeFqcn] ?? null;
  36. }
  37. public function checkIfCompletelyResolved(): void
  38. {
  39. foreach ($this->badges as $badge) {
  40. if (!$badge->isResolved()) {
  41. throw new BadCredentialsException(sprintf('Authentication failed security badge "%s" is not resolved, did you forget to register the correct listeners?', \get_class($badge)));
  42. }
  43. }
  44. }
  45. }