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