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

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