vendor/symfony/security-core/Authorization/Voter/TraceableVoter.php line 38

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\Core\Authorization\Voter;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Event\VoteEvent;
  13. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  14. /**
  15. * Decorates voter classes to send result events.
  16. *
  17. * @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
  18. *
  19. * @internal
  20. */
  21. class TraceableVoter implements CacheableVoterInterface
  22. {
  23. private $voter;
  24. private $eventDispatcher;
  25. public function __construct(VoterInterface $voter, EventDispatcherInterface $eventDispatcher)
  26. {
  27. $this->voter = $voter;
  28. $this->eventDispatcher = $eventDispatcher;
  29. }
  30. public function vote(TokenInterface $token, $subject, array $attributes): int
  31. {
  32. $result = $this->voter->vote($token, $subject, $attributes);
  33. $this->eventDispatcher->dispatch(new VoteEvent($this->voter, $subject, $attributes, $result), 'debug.security.authorization.vote');
  34. return $result;
  35. }
  36. public function getDecoratedVoter(): VoterInterface
  37. {
  38. return $this->voter;
  39. }
  40. public function supportsAttribute(string $attribute): bool
  41. {
  42. return !$this->voter instanceof CacheableVoterInterface || $this->voter->supportsAttribute($attribute);
  43. }
  44. public function supportsType(string $subjectType): bool
  45. {
  46. return !$this->voter instanceof CacheableVoterInterface || $this->voter->supportsType($subjectType);
  47. }
  48. }