SecurityDataCollector.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  16. use Symfony\Component\Security\Core\Role\RoleInterface;
  17. use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
  18. /**
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class SecurityDataCollector extends DataCollector
  22. {
  23. private $tokenStorage;
  24. private $roleHierarchy;
  25. private $logoutUrlGenerator;
  26. public function __construct(TokenStorageInterface $tokenStorage = null, RoleHierarchyInterface $roleHierarchy = null, LogoutUrlGenerator $logoutUrlGenerator = null)
  27. {
  28. $this->tokenStorage = $tokenStorage;
  29. $this->roleHierarchy = $roleHierarchy;
  30. $this->logoutUrlGenerator = $logoutUrlGenerator;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function collect(Request $request, Response $response, \Exception $exception = null)
  36. {
  37. if (null === $this->tokenStorage) {
  38. $this->data = array(
  39. 'enabled' => false,
  40. 'authenticated' => false,
  41. 'token_class' => null,
  42. 'logout_url' => null,
  43. 'user' => '',
  44. 'roles' => array(),
  45. 'inherited_roles' => array(),
  46. 'supports_role_hierarchy' => null !== $this->roleHierarchy,
  47. );
  48. } elseif (null === $token = $this->tokenStorage->getToken()) {
  49. $this->data = array(
  50. 'enabled' => true,
  51. 'authenticated' => false,
  52. 'token_class' => null,
  53. 'logout_url' => null,
  54. 'user' => '',
  55. 'roles' => array(),
  56. 'inherited_roles' => array(),
  57. 'supports_role_hierarchy' => null !== $this->roleHierarchy,
  58. );
  59. } else {
  60. $inheritedRoles = array();
  61. $assignedRoles = $token->getRoles();
  62. if (null !== $this->roleHierarchy) {
  63. $allRoles = $this->roleHierarchy->getReachableRoles($assignedRoles);
  64. foreach ($allRoles as $role) {
  65. if (!\in_array($role, $assignedRoles, true)) {
  66. $inheritedRoles[] = $role;
  67. }
  68. }
  69. }
  70. $logoutUrl = null;
  71. try {
  72. if (null !== $this->logoutUrlGenerator) {
  73. $logoutUrl = $this->logoutUrlGenerator->getLogoutPath();
  74. }
  75. } catch (\Exception $e) {
  76. // fail silently when the logout URL cannot be generated
  77. }
  78. $this->data = array(
  79. 'enabled' => true,
  80. 'authenticated' => $token->isAuthenticated(),
  81. 'token_class' => \get_class($token),
  82. 'logout_url' => $logoutUrl,
  83. 'user' => $token->getUsername(),
  84. 'roles' => array_map(function (RoleInterface $role) { return $role->getRole(); }, $assignedRoles),
  85. 'inherited_roles' => array_unique(array_map(function (RoleInterface $role) { return $role->getRole(); }, $inheritedRoles)),
  86. 'supports_role_hierarchy' => null !== $this->roleHierarchy,
  87. );
  88. }
  89. }
  90. /**
  91. * Checks if security is enabled.
  92. *
  93. * @return bool true if security is enabled, false otherwise
  94. */
  95. public function isEnabled()
  96. {
  97. return $this->data['enabled'];
  98. }
  99. /**
  100. * Gets the user.
  101. *
  102. * @return string The user
  103. */
  104. public function getUser()
  105. {
  106. return $this->data['user'];
  107. }
  108. /**
  109. * Gets the roles of the user.
  110. *
  111. * @return array The roles
  112. */
  113. public function getRoles()
  114. {
  115. return $this->data['roles'];
  116. }
  117. /**
  118. * Gets the inherited roles of the user.
  119. *
  120. * @return array The inherited roles
  121. */
  122. public function getInheritedRoles()
  123. {
  124. return $this->data['inherited_roles'];
  125. }
  126. /**
  127. * Checks if the data contains information about inherited roles. Still the inherited
  128. * roles can be an empty array.
  129. *
  130. * @return bool true if the profile was contains inherited role information
  131. */
  132. public function supportsRoleHierarchy()
  133. {
  134. return $this->data['supports_role_hierarchy'];
  135. }
  136. /**
  137. * Checks if the user is authenticated or not.
  138. *
  139. * @return bool true if the user is authenticated, false otherwise
  140. */
  141. public function isAuthenticated()
  142. {
  143. return $this->data['authenticated'];
  144. }
  145. /**
  146. * Get the class name of the security token.
  147. *
  148. * @return string The token
  149. */
  150. public function getTokenClass()
  151. {
  152. return $this->data['token_class'];
  153. }
  154. /**
  155. * Get the logout URL.
  156. *
  157. * @return string The logout URL
  158. */
  159. public function getLogoutUrl()
  160. {
  161. return $this->data['logout_url'];
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function getName()
  167. {
  168. return 'security';
  169. }
  170. }