vendor/symfony/security/Core/Authorization/TraceableAccessDecisionManager.php line 61

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;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  13. /**
  14.  * Decorates the original AccessDecisionManager class to log information
  15.  * about the security voters and the decisions made by them.
  16.  *
  17.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  18.  *
  19.  * @internal
  20.  */
  21. class TraceableAccessDecisionManager implements AccessDecisionManagerInterface
  22. {
  23.     private $manager;
  24.     private $strategy;
  25.     private $voters = [];
  26.     private $decisionLog = []; // All decision logs
  27.     private $currentLog = [];  // Logs being filled in
  28.     public function __construct(AccessDecisionManagerInterface $manager)
  29.     {
  30.         $this->manager $manager;
  31.         if ($this->manager instanceof AccessDecisionManager) {
  32.             // The strategy and voters are stored in a private properties of the decorated service
  33.             $reflection = new \ReflectionProperty(AccessDecisionManager::class, 'strategy');
  34.             $reflection->setAccessible(true);
  35.             $this->strategy $reflection->getValue($manager);
  36.             $reflection = new \ReflectionProperty(AccessDecisionManager::class, 'voters');
  37.             $reflection->setAccessible(true);
  38.             $this->voters $reflection->getValue($manager);
  39.         }
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function decide(TokenInterface $token, array $attributes$object null)
  45.     {
  46.         $currentDecisionLog = [
  47.             'attributes' => $attributes,
  48.             'object' => $object,
  49.             'voterDetails' => [],
  50.         ];
  51.         $this->currentLog[] = &$currentDecisionLog;
  52.         $result $this->manager->decide($token$attributes$object);
  53.         $currentDecisionLog['result'] = $result;
  54.         $this->decisionLog[] = array_pop($this->currentLog); // Using a stack since decide can be called by voters
  55.         return $result;
  56.     }
  57.     /**
  58.      * Adds voter vote and class to the voter details.
  59.      *
  60.      * @param VoterInterface $voter      voter
  61.      * @param array          $attributes attributes used for the vote
  62.      * @param int            $vote       vote of the voter
  63.      */
  64.     public function addVoterVote(VoterInterface $voter, array $attributesint $vote)
  65.     {
  66.         $currentLogIndex = \count($this->currentLog) - 1;
  67.         $this->currentLog[$currentLogIndex]['voterDetails'][] = [
  68.             'voter' => $voter,
  69.             'attributes' => $attributes,
  70.             'vote' => $vote,
  71.         ];
  72.     }
  73.     /**
  74.      * @return string
  75.      */
  76.     public function getStrategy()
  77.     {
  78.         // The $strategy property is misleading because it stores the name of its
  79.         // method (e.g. 'decideAffirmative') instead of the original strategy name
  80.         // (e.g. 'affirmative')
  81.         return null === $this->strategy '-' strtolower(substr($this->strategy6));
  82.     }
  83.     /**
  84.      * @return iterable|VoterInterface[]
  85.      */
  86.     public function getVoters()
  87.     {
  88.         return $this->voters;
  89.     }
  90.     /**
  91.      * @return array
  92.      */
  93.     public function getDecisionLog()
  94.     {
  95.         return $this->decisionLog;
  96.     }
  97. }
  98. class_alias(TraceableAccessDecisionManager::class, DebugAccessDecisionManager::class);