SecurityIdentityRetrievalStrategy.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\Component\Security\Acl\Domain;
  11. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Acl\Model\SecurityIdentityRetrievalStrategyInterface;
  14. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
  15. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  16. use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
  17. /**
  18. * Strategy for retrieving security identities
  19. *
  20. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  21. */
  22. class SecurityIdentityRetrievalStrategy implements SecurityIdentityRetrievalStrategyInterface
  23. {
  24. private $roleHierarchy;
  25. private $authenticationTrustResolver;
  26. /**
  27. * Constructor
  28. *
  29. * @param RoleHierarchyInterface $roleHierarchy
  30. * @param AuthenticationTrustResolver $authenticationTrustResolver
  31. */
  32. public function __construct(RoleHierarchyInterface $roleHierarchy, AuthenticationTrustResolver $authenticationTrustResolver)
  33. {
  34. $this->roleHierarchy = $roleHierarchy;
  35. $this->authenticationTrustResolver = $authenticationTrustResolver;
  36. }
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public function getSecurityIdentities(TokenInterface $token)
  41. {
  42. $sids = array();
  43. // add user security identity
  44. if (!$token instanceof AnonymousToken) {
  45. try {
  46. $sids[] = UserSecurityIdentity::fromToken($token);
  47. } catch (\InvalidArgumentException $invalid) {
  48. // ignore, user has no user security identity
  49. }
  50. }
  51. // add all reachable roles
  52. foreach ($this->roleHierarchy->getReachableRoles($token->getRoles()) as $role) {
  53. $sids[] = new RoleSecurityIdentity($role);
  54. }
  55. // add built-in special roles
  56. if ($this->authenticationTrustResolver->isFullFledged($token)) {
  57. $sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_FULLY);
  58. $sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED);
  59. $sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY);
  60. } elseif ($this->authenticationTrustResolver->isRememberMe($token)) {
  61. $sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED);
  62. $sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY);
  63. } elseif ($this->authenticationTrustResolver->isAnonymous($token)) {
  64. $sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY);
  65. }
  66. return $sids;
  67. }
  68. }