vendor/symfony/security-bundle/DataCollector/SecurityDataCollector.php line 176

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\Bundle\SecurityBundle\DataCollector;
  11. use Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener;
  12. use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  16. use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  19. use Symfony\Component\Security\Core\Authorization\TraceableAccessDecisionManager;
  20. use Symfony\Component\Security\Core\Authorization\Voter\TraceableVoter;
  21. use Symfony\Component\Security\Core\Role\Role;
  22. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  23. use Symfony\Component\Security\Core\Role\SwitchUserRole;
  24. use Symfony\Component\Security\Http\Firewall\SwitchUserListener;
  25. use Symfony\Component\Security\Http\FirewallMapInterface;
  26. use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
  27. use Symfony\Component\VarDumper\Caster\ClassStub;
  28. use Symfony\Component\VarDumper\Cloner\Data;
  29. /**
  30.  * @author Fabien Potencier <fabien@symfony.com>
  31.  */
  32. class SecurityDataCollector extends DataCollector implements LateDataCollectorInterface
  33. {
  34.     private $tokenStorage;
  35.     private $roleHierarchy;
  36.     private $logoutUrlGenerator;
  37.     private $accessDecisionManager;
  38.     private $firewallMap;
  39.     private $firewall;
  40.     private $hasVarDumper;
  41.     public function __construct(TokenStorageInterface $tokenStorage nullRoleHierarchyInterface $roleHierarchy nullLogoutUrlGenerator $logoutUrlGenerator nullAccessDecisionManagerInterface $accessDecisionManager nullFirewallMapInterface $firewallMap nullTraceableFirewallListener $firewall null)
  42.     {
  43.         $this->tokenStorage $tokenStorage;
  44.         $this->roleHierarchy $roleHierarchy;
  45.         $this->logoutUrlGenerator $logoutUrlGenerator;
  46.         $this->accessDecisionManager $accessDecisionManager;
  47.         $this->firewallMap $firewallMap;
  48.         $this->firewall $firewall;
  49.         $this->hasVarDumper class_exists(ClassStub::class);
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      */
  54.     public function collect(Request $requestResponse $response, \Exception $exception null)
  55.     {
  56.         if (null === $this->tokenStorage) {
  57.             $this->data = [
  58.                 'enabled' => false,
  59.                 'authenticated' => false,
  60.                 'impersonated' => false,
  61.                 'impersonator_user' => null,
  62.                 'impersonation_exit_path' => null,
  63.                 'token' => null,
  64.                 'token_class' => null,
  65.                 'logout_url' => null,
  66.                 'user' => '',
  67.                 'roles' => [],
  68.                 'inherited_roles' => [],
  69.                 'supports_role_hierarchy' => null !== $this->roleHierarchy,
  70.             ];
  71.         } elseif (null === $token $this->tokenStorage->getToken()) {
  72.             $this->data = [
  73.                 'enabled' => true,
  74.                 'authenticated' => false,
  75.                 'impersonated' => false,
  76.                 'impersonator_user' => null,
  77.                 'impersonation_exit_path' => null,
  78.                 'token' => null,
  79.                 'token_class' => null,
  80.                 'logout_url' => null,
  81.                 'user' => '',
  82.                 'roles' => [],
  83.                 'inherited_roles' => [],
  84.                 'supports_role_hierarchy' => null !== $this->roleHierarchy,
  85.             ];
  86.         } else {
  87.             $inheritedRoles = [];
  88.             $assignedRoles $token->getRoles();
  89.             $impersonatorUser null;
  90.             foreach ($assignedRoles as $role) {
  91.                 if ($role instanceof SwitchUserRole) {
  92.                     $impersonatorUser $role->getSource()->getUsername();
  93.                     break;
  94.                 }
  95.             }
  96.             if (null !== $this->roleHierarchy) {
  97.                 $allRoles $this->roleHierarchy->getReachableRoles($assignedRoles);
  98.                 foreach ($allRoles as $role) {
  99.                     if (!\in_array($role$assignedRolestrue)) {
  100.                         $inheritedRoles[] = $role;
  101.                     }
  102.                 }
  103.             }
  104.             $logoutUrl null;
  105.             try {
  106.                 if (null !== $this->logoutUrlGenerator) {
  107.                     $logoutUrl $this->logoutUrlGenerator->getLogoutPath();
  108.                 }
  109.             } catch (\Exception $e) {
  110.                 // fail silently when the logout URL cannot be generated
  111.             }
  112.             $this->data = [
  113.                 'enabled' => true,
  114.                 'authenticated' => $token->isAuthenticated(),
  115.                 'impersonated' => null !== $impersonatorUser,
  116.                 'impersonator_user' => $impersonatorUser,
  117.                 'impersonation_exit_path' => null,
  118.                 'token' => $token,
  119.                 'token_class' => $this->hasVarDumper ? new ClassStub(\get_class($token)) : \get_class($token),
  120.                 'logout_url' => $logoutUrl,
  121.                 'user' => $token->getUsername(),
  122.                 'roles' => array_map(function (Role $role) { return $role->getRole(); }, $assignedRoles),
  123.                 'inherited_roles' => array_unique(array_map(function (Role $role) { return $role->getRole(); }, $inheritedRoles)),
  124.                 'supports_role_hierarchy' => null !== $this->roleHierarchy,
  125.             ];
  126.         }
  127.         // collect voters and access decision manager information
  128.         if ($this->accessDecisionManager instanceof TraceableAccessDecisionManager) {
  129.             $this->data['voter_strategy'] = $this->accessDecisionManager->getStrategy();
  130.             foreach ($this->accessDecisionManager->getVoters() as $voter) {
  131.                 if ($voter instanceof TraceableVoter) {
  132.                     $voter $voter->getDecoratedVoter();
  133.                 }
  134.                 $this->data['voters'][] = $this->hasVarDumper ? new ClassStub(\get_class($voter)) : \get_class($voter);
  135.             }
  136.             // collect voter details
  137.             $decisionLog $this->accessDecisionManager->getDecisionLog();
  138.             foreach ($decisionLog as $key => $log) {
  139.                 $decisionLog[$key]['voter_details'] = [];
  140.                 foreach ($log['voterDetails'] as $voterDetail) {
  141.                     $voterClass = \get_class($voterDetail['voter']);
  142.                     $classData $this->hasVarDumper ? new ClassStub($voterClass) : $voterClass;
  143.                     $decisionLog[$key]['voter_details'][] = [
  144.                         'class' => $classData,
  145.                         'attributes' => $voterDetail['attributes'], // Only displayed for unanimous strategy
  146.                         'vote' => $voterDetail['vote'],
  147.                     ];
  148.                 }
  149.                 unset($decisionLog[$key]['voterDetails']);
  150.             }
  151.             $this->data['access_decision_log'] = $decisionLog;
  152.         } else {
  153.             $this->data['access_decision_log'] = [];
  154.             $this->data['voter_strategy'] = 'unknown';
  155.             $this->data['voters'] = [];
  156.         }
  157.         // collect firewall context information
  158.         $this->data['firewall'] = null;
  159.         if ($this->firewallMap instanceof FirewallMap) {
  160.             $firewallConfig $this->firewallMap->getFirewallConfig($request);
  161.             if (null !== $firewallConfig) {
  162.                 $this->data['firewall'] = [
  163.                     'name' => $firewallConfig->getName(),
  164.                     'allows_anonymous' => $firewallConfig->allowsAnonymous(),
  165.                     'request_matcher' => $firewallConfig->getRequestMatcher(),
  166.                     'security_enabled' => $firewallConfig->isSecurityEnabled(),
  167.                     'stateless' => $firewallConfig->isStateless(),
  168.                     'provider' => $firewallConfig->getProvider(),
  169.                     'context' => $firewallConfig->getContext(),
  170.                     'entry_point' => $firewallConfig->getEntryPoint(),
  171.                     'access_denied_handler' => $firewallConfig->getAccessDeniedHandler(),
  172.                     'access_denied_url' => $firewallConfig->getAccessDeniedUrl(),
  173.                     'user_checker' => $firewallConfig->getUserChecker(),
  174.                     'listeners' => $firewallConfig->getListeners(),
  175.                 ];
  176.                 // generate exit impersonation path from current request
  177.                 if ($this->data['impersonated'] && null !== $switchUserConfig $firewallConfig->getSwitchUser()) {
  178.                     $exitPath $request->getRequestUri();
  179.                     $exitPath .= null === $request->getQueryString() ? '?' '&';
  180.                     $exitPath .= sprintf('%s=%s'urlencode($switchUserConfig['parameter']), SwitchUserListener::EXIT_VALUE);
  181.                     $this->data['impersonation_exit_path'] = $exitPath;
  182.                 }
  183.             }
  184.         }
  185.         // collect firewall listeners information
  186.         $this->data['listeners'] = [];
  187.         if ($this->firewall) {
  188.             $this->data['listeners'] = $this->firewall->getWrappedListeners();
  189.         }
  190.     }
  191.     /**
  192.      * {@inheritdoc}
  193.      */
  194.     public function reset()
  195.     {
  196.         $this->data = [];
  197.     }
  198.     public function lateCollect()
  199.     {
  200.         $this->data $this->cloneVar($this->data);
  201.     }
  202.     /**
  203.      * Checks if security is enabled.
  204.      *
  205.      * @return bool true if security is enabled, false otherwise
  206.      */
  207.     public function isEnabled()
  208.     {
  209.         return $this->data['enabled'];
  210.     }
  211.     /**
  212.      * Gets the user.
  213.      *
  214.      * @return string The user
  215.      */
  216.     public function getUser()
  217.     {
  218.         return $this->data['user'];
  219.     }
  220.     /**
  221.      * Gets the roles of the user.
  222.      *
  223.      * @return array The roles
  224.      */
  225.     public function getRoles()
  226.     {
  227.         return $this->data['roles'];
  228.     }
  229.     /**
  230.      * Gets the inherited roles of the user.
  231.      *
  232.      * @return array The inherited roles
  233.      */
  234.     public function getInheritedRoles()
  235.     {
  236.         return $this->data['inherited_roles'];
  237.     }
  238.     /**
  239.      * Checks if the data contains information about inherited roles. Still the inherited
  240.      * roles can be an empty array.
  241.      *
  242.      * @return bool true if the profile was contains inherited role information
  243.      */
  244.     public function supportsRoleHierarchy()
  245.     {
  246.         return $this->data['supports_role_hierarchy'];
  247.     }
  248.     /**
  249.      * Checks if the user is authenticated or not.
  250.      *
  251.      * @return bool true if the user is authenticated, false otherwise
  252.      */
  253.     public function isAuthenticated()
  254.     {
  255.         return $this->data['authenticated'];
  256.     }
  257.     public function isImpersonated()
  258.     {
  259.         return $this->data['impersonated'];
  260.     }
  261.     public function getImpersonatorUser()
  262.     {
  263.         return $this->data['impersonator_user'];
  264.     }
  265.     public function getImpersonationExitPath()
  266.     {
  267.         return $this->data['impersonation_exit_path'];
  268.     }
  269.     /**
  270.      * Get the class name of the security token.
  271.      *
  272.      * @return string The token
  273.      */
  274.     public function getTokenClass()
  275.     {
  276.         return $this->data['token_class'];
  277.     }
  278.     /**
  279.      * Get the full security token class as Data object.
  280.      *
  281.      * @return Data
  282.      */
  283.     public function getToken()
  284.     {
  285.         return $this->data['token'];
  286.     }
  287.     /**
  288.      * Get the logout URL.
  289.      *
  290.      * @return string The logout URL
  291.      */
  292.     public function getLogoutUrl()
  293.     {
  294.         return $this->data['logout_url'];
  295.     }
  296.     /**
  297.      * Returns the FQCN of the security voters enabled in the application.
  298.      *
  299.      * @return string[]
  300.      */
  301.     public function getVoters()
  302.     {
  303.         return $this->data['voters'];
  304.     }
  305.     /**
  306.      * Returns the strategy configured for the security voters.
  307.      *
  308.      * @return string
  309.      */
  310.     public function getVoterStrategy()
  311.     {
  312.         return $this->data['voter_strategy'];
  313.     }
  314.     /**
  315.      * Returns the log of the security decisions made by the access decision manager.
  316.      *
  317.      * @return array
  318.      */
  319.     public function getAccessDecisionLog()
  320.     {
  321.         return $this->data['access_decision_log'];
  322.     }
  323.     /**
  324.      * Returns the configuration of the current firewall context.
  325.      *
  326.      * @return array
  327.      */
  328.     public function getFirewall()
  329.     {
  330.         return $this->data['firewall'];
  331.     }
  332.     public function getListeners()
  333.     {
  334.         return $this->data['listeners'];
  335.     }
  336.     /**
  337.      * {@inheritdoc}
  338.      */
  339.     public function getName()
  340.     {
  341.         return 'security';
  342.     }
  343. }