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

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