SecurityIdentityRetrievalStrategy.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\AuthenticationTrustResolverInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Acl\Model\SecurityIdentityRetrievalStrategyInterface;
  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 AuthenticationTrustResolverInterface $authenticationTrustResolver
  31. */
  32. public function __construct(RoleHierarchyInterface $roleHierarchy, AuthenticationTrustResolverInterface $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 $e) {
  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. }