/vendor/symfony/symfony/src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php

https://gitlab.com/pr0055/symfonypizza · PHP · 246 lines · 138 code · 23 blank · 85 comment · 7 complexity · 976d35c8c0273eb2d09392358d3995f3 MD5 · raw file

  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\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  12. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  16. use Symfony\Component\Security\Core\Role\RoleInterface;
  17. use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
  18. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  19. use Symfony\Component\Security\Core\Authorization\DebugAccessDecisionManager;
  20. /**
  21. * SecurityDataCollector.
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. */
  25. class SecurityDataCollector extends DataCollector
  26. {
  27. private $tokenStorage;
  28. private $roleHierarchy;
  29. private $logoutUrlGenerator;
  30. private $accessDecisionManager;
  31. /**
  32. * Constructor.
  33. *
  34. * @param TokenStorageInterface|null $tokenStorage
  35. * @param RoleHierarchyInterface|null $roleHierarchy
  36. * @param LogoutUrlGenerator|null $logoutUrlGenerator
  37. * @param AccessDecisionManagerInterface|null $accessDecisionManager
  38. */
  39. public function __construct(TokenStorageInterface $tokenStorage = null, RoleHierarchyInterface $roleHierarchy = null, LogoutUrlGenerator $logoutUrlGenerator = null, AccessDecisionManagerInterface $accessDecisionManager = null)
  40. {
  41. $this->tokenStorage = $tokenStorage;
  42. $this->roleHierarchy = $roleHierarchy;
  43. $this->logoutUrlGenerator = $logoutUrlGenerator;
  44. $this->accessDecisionManager = $accessDecisionManager;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function collect(Request $request, Response $response, \Exception $exception = null)
  50. {
  51. if (null === $this->tokenStorage) {
  52. $this->data = array(
  53. 'enabled' => false,
  54. 'authenticated' => false,
  55. 'token_class' => null,
  56. 'logout_url' => null,
  57. 'user' => '',
  58. 'roles' => array(),
  59. 'inherited_roles' => array(),
  60. 'supports_role_hierarchy' => null !== $this->roleHierarchy,
  61. );
  62. } elseif (null === $token = $this->tokenStorage->getToken()) {
  63. $this->data = array(
  64. 'enabled' => true,
  65. 'authenticated' => false,
  66. 'token_class' => null,
  67. 'logout_url' => null,
  68. 'user' => '',
  69. 'roles' => array(),
  70. 'inherited_roles' => array(),
  71. 'supports_role_hierarchy' => null !== $this->roleHierarchy,
  72. );
  73. } else {
  74. $inheritedRoles = array();
  75. $assignedRoles = $token->getRoles();
  76. if (null !== $this->roleHierarchy) {
  77. $allRoles = $this->roleHierarchy->getReachableRoles($assignedRoles);
  78. foreach ($allRoles as $role) {
  79. if (!in_array($role, $assignedRoles, true)) {
  80. $inheritedRoles[] = $role;
  81. }
  82. }
  83. }
  84. $logoutUrl = null;
  85. try {
  86. if (null !== $this->logoutUrlGenerator) {
  87. $logoutUrl = $this->logoutUrlGenerator->getLogoutPath();
  88. }
  89. } catch (\Exception $e) {
  90. // fail silently when the logout URL cannot be generated
  91. }
  92. $this->data = array(
  93. 'enabled' => true,
  94. 'authenticated' => $token->isAuthenticated(),
  95. 'token_class' => get_class($token),
  96. 'logout_url' => $logoutUrl,
  97. 'user' => $token->getUsername(),
  98. 'roles' => array_map(function (RoleInterface $role) { return $role->getRole();}, $assignedRoles),
  99. 'inherited_roles' => array_map(function (RoleInterface $role) { return $role->getRole(); }, $inheritedRoles),
  100. 'supports_role_hierarchy' => null !== $this->roleHierarchy,
  101. );
  102. }
  103. // collect voters and access decision manager information
  104. if ($this->accessDecisionManager instanceof DebugAccessDecisionManager) {
  105. $this->data['access_decision_log'] = $this->accessDecisionManager->getDecisionLog();
  106. $this->data['voter_strategy'] = $this->accessDecisionManager->getStrategy();
  107. foreach ($this->accessDecisionManager->getVoters() as $voter) {
  108. $this->data['voters'][] = get_class($voter);
  109. }
  110. } else {
  111. $this->data['access_decision_log'] = array();
  112. $this->data['voter_strategy'] = 'unknown';
  113. $this->data['voters'] = array();
  114. }
  115. }
  116. /**
  117. * Checks if security is enabled.
  118. *
  119. * @return bool true if security is enabled, false otherwise
  120. */
  121. public function isEnabled()
  122. {
  123. return $this->data['enabled'];
  124. }
  125. /**
  126. * Gets the user.
  127. *
  128. * @return string The user
  129. */
  130. public function getUser()
  131. {
  132. return $this->data['user'];
  133. }
  134. /**
  135. * Gets the roles of the user.
  136. *
  137. * @return array The roles
  138. */
  139. public function getRoles()
  140. {
  141. return $this->data['roles'];
  142. }
  143. /**
  144. * Gets the inherited roles of the user.
  145. *
  146. * @return array The inherited roles
  147. */
  148. public function getInheritedRoles()
  149. {
  150. return $this->data['inherited_roles'];
  151. }
  152. /**
  153. * Checks if the data contains information about inherited roles. Still the inherited
  154. * roles can be an empty array.
  155. *
  156. * @return bool true if the profile was contains inherited role information
  157. */
  158. public function supportsRoleHierarchy()
  159. {
  160. return $this->data['supports_role_hierarchy'];
  161. }
  162. /**
  163. * Checks if the user is authenticated or not.
  164. *
  165. * @return bool true if the user is authenticated, false otherwise
  166. */
  167. public function isAuthenticated()
  168. {
  169. return $this->data['authenticated'];
  170. }
  171. /**
  172. * Get the class name of the security token.
  173. *
  174. * @return string The token
  175. */
  176. public function getTokenClass()
  177. {
  178. return $this->data['token_class'];
  179. }
  180. /**
  181. * Get the provider key (i.e. the name of the active firewall).
  182. *
  183. * @return string The provider key
  184. */
  185. public function getLogoutUrl()
  186. {
  187. return $this->data['logout_url'];
  188. }
  189. /**
  190. * Returns the FQCN of the security voters enabled in the application.
  191. *
  192. * @return string[]
  193. */
  194. public function getVoters()
  195. {
  196. return $this->data['voters'];
  197. }
  198. /**
  199. * Returns the strategy configured for the security voters.
  200. *
  201. * @return string
  202. */
  203. public function getVoterStrategy()
  204. {
  205. return $this->data['voter_strategy'];
  206. }
  207. /**
  208. * Returns the log of the security decisions made by the access decision manager.
  209. *
  210. * @return array
  211. */
  212. public function getAccessDecisionLog()
  213. {
  214. return $this->data['access_decision_log'];
  215. }
  216. /**
  217. * {@inheritdoc}
  218. */
  219. public function getName()
  220. {
  221. return 'security';
  222. }
  223. }