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

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\AnonymousToken;
  18. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
  20. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  21. use Symfony\Component\Security\Core\Authorization\TraceableAccessDecisionManager;
  22. use Symfony\Component\Security\Core\Authorization\Voter\TraceableVoter;
  23. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  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.  * @final
  33.  */
  34. class SecurityDataCollector extends DataCollector implements LateDataCollectorInterface
  35. {
  36.     private $tokenStorage;
  37.     private $roleHierarchy;
  38.     private $logoutUrlGenerator;
  39.     private $accessDecisionManager;
  40.     private $firewallMap;
  41.     private $firewall;
  42.     private $hasVarDumper;
  43.     private $authenticatorManagerEnabled;
  44.     public function __construct(TokenStorageInterface $tokenStorage nullRoleHierarchyInterface $roleHierarchy nullLogoutUrlGenerator $logoutUrlGenerator nullAccessDecisionManagerInterface $accessDecisionManager nullFirewallMapInterface $firewallMap nullTraceableFirewallListener $firewall nullbool $authenticatorManagerEnabled false)
  45.     {
  46.         $this->tokenStorage $tokenStorage;
  47.         $this->roleHierarchy $roleHierarchy;
  48.         $this->logoutUrlGenerator $logoutUrlGenerator;
  49.         $this->accessDecisionManager $accessDecisionManager;
  50.         $this->firewallMap $firewallMap;
  51.         $this->firewall $firewall;
  52.         $this->hasVarDumper class_exists(ClassStub::class);
  53.         $this->authenticatorManagerEnabled $authenticatorManagerEnabled;
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      */
  58.     public function collect(Request $requestResponse $response, \Throwable $exception null)
  59.     {
  60.         if (null === $this->tokenStorage) {
  61.             $this->data = [
  62.                 'enabled' => false,
  63.                 'authenticated' => false,
  64.                 'impersonated' => false,
  65.                 'impersonator_user' => null,
  66.                 'impersonation_exit_path' => null,
  67.                 'token' => null,
  68.                 'token_class' => null,
  69.                 'logout_url' => null,
  70.                 'user' => '',
  71.                 'roles' => [],
  72.                 'inherited_roles' => [],
  73.                 'supports_role_hierarchy' => null !== $this->roleHierarchy,
  74.             ];
  75.         } elseif (null === $token $this->tokenStorage->getToken()) {
  76.             $this->data = [
  77.                 'enabled' => true,
  78.                 'authenticated' => false,
  79.                 'impersonated' => false,
  80.                 'impersonator_user' => null,
  81.                 'impersonation_exit_path' => null,
  82.                 'token' => null,
  83.                 'token_class' => null,
  84.                 'logout_url' => null,
  85.                 'user' => '',
  86.                 'roles' => [],
  87.                 'inherited_roles' => [],
  88.                 'supports_role_hierarchy' => null !== $this->roleHierarchy,
  89.             ];
  90.         } else {
  91.             $inheritedRoles = [];
  92.             $assignedRoles $token->getRoleNames();
  93.             $impersonatorUser null;
  94.             if ($token instanceof SwitchUserToken) {
  95.                 $impersonatorUser $token->getOriginalToken()->getUsername();
  96.             }
  97.             if (null !== $this->roleHierarchy) {
  98.                 foreach ($this->roleHierarchy->getReachableRoleNames($assignedRoles) as $role) {
  99.                     if (!\in_array($role$assignedRolestrue)) {
  100.                         $inheritedRoles[] = $role;
  101.                     }
  102.                 }
  103.             }
  104.             $logoutUrl null;
  105.             try {
  106.                 if (null !== $this->logoutUrlGenerator && !$token instanceof AnonymousToken) {
  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' => $assignedRoles,
  123.                 'inherited_roles' => array_unique($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.         $this->data['authenticator_manager_enabled'] = $this->authenticatorManagerEnabled;
  191.     }
  192.     /**
  193.      * {@inheritdoc}
  194.      */
  195.     public function reset()
  196.     {
  197.         $this->data = [];
  198.     }
  199.     public function lateCollect()
  200.     {
  201.         $this->data $this->cloneVar($this->data);
  202.     }
  203.     /**
  204.      * Checks if security is enabled.
  205.      *
  206.      * @return bool true if security is enabled, false otherwise
  207.      */
  208.     public function isEnabled()
  209.     {
  210.         return $this->data['enabled'];
  211.     }
  212.     /**
  213.      * Gets the user.
  214.      *
  215.      * @return string The user
  216.      */
  217.     public function getUser()
  218.     {
  219.         return $this->data['user'];
  220.     }
  221.     /**
  222.      * Gets the roles of the user.
  223.      *
  224.      * @return array|Data
  225.      */
  226.     public function getRoles()
  227.     {
  228.         return $this->data['roles'];
  229.     }
  230.     /**
  231.      * Gets the inherited roles of the user.
  232.      *
  233.      * @return array|Data
  234.      */
  235.     public function getInheritedRoles()
  236.     {
  237.         return $this->data['inherited_roles'];
  238.     }
  239.     /**
  240.      * Checks if the data contains information about inherited roles. Still the inherited
  241.      * roles can be an empty array.
  242.      *
  243.      * @return bool true if the profile was contains inherited role information
  244.      */
  245.     public function supportsRoleHierarchy()
  246.     {
  247.         return $this->data['supports_role_hierarchy'];
  248.     }
  249.     /**
  250.      * Checks if the user is authenticated or not.
  251.      *
  252.      * @return bool true if the user is authenticated, false otherwise
  253.      */
  254.     public function isAuthenticated()
  255.     {
  256.         return $this->data['authenticated'];
  257.     }
  258.     /**
  259.      * @return bool
  260.      */
  261.     public function isImpersonated()
  262.     {
  263.         return $this->data['impersonated'];
  264.     }
  265.     /**
  266.      * @return string|null
  267.      */
  268.     public function getImpersonatorUser()
  269.     {
  270.         return $this->data['impersonator_user'];
  271.     }
  272.     /**
  273.      * @return string|null
  274.      */
  275.     public function getImpersonationExitPath()
  276.     {
  277.         return $this->data['impersonation_exit_path'];
  278.     }
  279.     /**
  280.      * Get the class name of the security token.
  281.      *
  282.      * @return string|Data|null The token
  283.      */
  284.     public function getTokenClass()
  285.     {
  286.         return $this->data['token_class'];
  287.     }
  288.     /**
  289.      * Get the full security token class as Data object.
  290.      *
  291.      * @return Data|null
  292.      */
  293.     public function getToken()
  294.     {
  295.         return $this->data['token'];
  296.     }
  297.     /**
  298.      * Get the logout URL.
  299.      *
  300.      * @return string|null The logout URL
  301.      */
  302.     public function getLogoutUrl()
  303.     {
  304.         return $this->data['logout_url'];
  305.     }
  306.     /**
  307.      * Returns the FQCN of the security voters enabled in the application.
  308.      *
  309.      * @return string[]|Data
  310.      */
  311.     public function getVoters()
  312.     {
  313.         return $this->data['voters'];
  314.     }
  315.     /**
  316.      * Returns the strategy configured for the security voters.
  317.      *
  318.      * @return string
  319.      */
  320.     public function getVoterStrategy()
  321.     {
  322.         return $this->data['voter_strategy'];
  323.     }
  324.     /**
  325.      * Returns the log of the security decisions made by the access decision manager.
  326.      *
  327.      * @return array|Data
  328.      */
  329.     public function getAccessDecisionLog()
  330.     {
  331.         return $this->data['access_decision_log'];
  332.     }
  333.     /**
  334.      * Returns the configuration of the current firewall context.
  335.      *
  336.      * @return array|Data|null
  337.      */
  338.     public function getFirewall()
  339.     {
  340.         return $this->data['firewall'];
  341.     }
  342.     /**
  343.      * @return array|Data
  344.      */
  345.     public function getListeners()
  346.     {
  347.         return $this->data['listeners'];
  348.     }
  349.     /**
  350.      * {@inheritdoc}
  351.      */
  352.     public function getName()
  353.     {
  354.         return 'security';
  355.     }
  356.     public function isAuthenticatorManagerEnabled(): bool
  357.     {
  358.         return $this->data['authenticator_manager_enabled'];
  359.     }
  360. }