AclVoter.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\Voter;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Security\Acl\Exception\NoAceFoundException;
  13. use Symfony\Component\Security\Acl\Exception\AclNotFoundException;
  14. use Symfony\Component\Security\Acl\Model\AclProviderInterface;
  15. use Symfony\Component\Security\Acl\Model\ObjectIdentityInterface;
  16. use Symfony\Component\Security\Acl\Permission\PermissionMapInterface;
  17. use Symfony\Component\Security\Acl\Model\SecurityIdentityRetrievalStrategyInterface;
  18. use Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  20. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  21. /**
  22. * This voter can be used as a base class for implementing your own permissions.
  23. *
  24. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  25. */
  26. class AclVoter implements VoterInterface
  27. {
  28. private $aclProvider;
  29. private $permissionMap;
  30. private $objectIdentityRetrievalStrategy;
  31. private $securityIdentityRetrievalStrategy;
  32. private $allowIfObjectIdentityUnavailable;
  33. private $logger;
  34. public function __construct(AclProviderInterface $aclProvider, ObjectIdentityRetrievalStrategyInterface $oidRetrievalStrategy, SecurityIdentityRetrievalStrategyInterface $sidRetrievalStrategy, PermissionMapInterface $permissionMap, LoggerInterface $logger = null, $allowIfObjectIdentityUnavailable = true)
  35. {
  36. $this->aclProvider = $aclProvider;
  37. $this->permissionMap = $permissionMap;
  38. $this->objectIdentityRetrievalStrategy = $oidRetrievalStrategy;
  39. $this->securityIdentityRetrievalStrategy = $sidRetrievalStrategy;
  40. $this->logger = $logger;
  41. $this->allowIfObjectIdentityUnavailable = $allowIfObjectIdentityUnavailable;
  42. }
  43. public function supportsAttribute($attribute)
  44. {
  45. return $this->permissionMap->contains($attribute);
  46. }
  47. public function vote(TokenInterface $token, $object, array $attributes)
  48. {
  49. foreach ($attributes as $attribute) {
  50. if (null === $masks = $this->permissionMap->getMasks($attribute, $object)) {
  51. continue;
  52. }
  53. if (null === $object) {
  54. if (null !== $this->logger) {
  55. $this->logger->debug(sprintf('Object identity unavailable. Voting to %s', $this->allowIfObjectIdentityUnavailable? 'grant access' : 'abstain'));
  56. }
  57. return $this->allowIfObjectIdentityUnavailable ? self::ACCESS_GRANTED : self::ACCESS_ABSTAIN;
  58. } elseif ($object instanceof FieldVote) {
  59. $field = $object->getField();
  60. $object = $object->getDomainObject();
  61. } else {
  62. $field = null;
  63. }
  64. if ($object instanceof ObjectIdentityInterface) {
  65. $oid = $object;
  66. } elseif (null === $oid = $this->objectIdentityRetrievalStrategy->getObjectIdentity($object)) {
  67. if (null !== $this->logger) {
  68. $this->logger->debug(sprintf('Object identity unavailable. Voting to %s', $this->allowIfObjectIdentityUnavailable? 'grant access' : 'abstain'));
  69. }
  70. return $this->allowIfObjectIdentityUnavailable ? self::ACCESS_GRANTED : self::ACCESS_ABSTAIN;
  71. }
  72. if (!$this->supportsClass($oid->getType())) {
  73. return self::ACCESS_ABSTAIN;
  74. }
  75. $sids = $this->securityIdentityRetrievalStrategy->getSecurityIdentities($token);
  76. try {
  77. $acl = $this->aclProvider->findAcl($oid, $sids);
  78. if (null === $field && $acl->isGranted($masks, $sids, false)) {
  79. if (null !== $this->logger) {
  80. $this->logger->debug('ACL found, permission granted. Voting to grant access');
  81. }
  82. return self::ACCESS_GRANTED;
  83. } elseif (null !== $field && $acl->isFieldGranted($field, $masks, $sids, false)) {
  84. if (null !== $this->logger) {
  85. $this->logger->debug('ACL found, permission granted. Voting to grant access');
  86. }
  87. return self::ACCESS_GRANTED;
  88. }
  89. if (null !== $this->logger) {
  90. $this->logger->debug('ACL found, insufficient permissions. Voting to deny access.');
  91. }
  92. return self::ACCESS_DENIED;
  93. } catch (AclNotFoundException $noAcl) {
  94. if (null !== $this->logger) {
  95. $this->logger->debug('No ACL found for the object identity. Voting to deny access.');
  96. }
  97. return self::ACCESS_DENIED;
  98. } catch (NoAceFoundException $noAce) {
  99. if (null !== $this->logger) {
  100. $this->logger->debug('ACL found, no ACE applicable. Voting to deny access.');
  101. }
  102. return self::ACCESS_DENIED;
  103. }
  104. }
  105. // no attribute was supported
  106. return self::ACCESS_ABSTAIN;
  107. }
  108. /**
  109. * You can override this method when writing a voter for a specific domain
  110. * class.
  111. *
  112. * @param string $class The class name
  113. *
  114. * @return Boolean
  115. */
  116. public function supportsClass($class)
  117. {
  118. return true;
  119. }
  120. }