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

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