vendor/symfony/security-acl/Voter/AclVoter.php line 30

Open in your IDE?
  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 $aclProviderObjectIdentityRetrievalStrategyInterface $oidRetrievalStrategySecurityIdentityRetrievalStrategyInterface $sidRetrievalStrategyPermissionMapInterface $permissionMapLoggerInterface $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 is_string($attribute) && $this->permissionMap->contains($attribute);
  46.     }
  47.     public function vote(TokenInterface $token$object, array $attributes)
  48.     {
  49.         foreach ($attributes as $attribute) {
  50.             if (!$this->supportsAttribute($attribute)) {
  51.                 continue;
  52.             }
  53.             if (null === $masks $this->permissionMap->getMasks($attribute$object)) {
  54.                 continue;
  55.             }
  56.             if (null === $object) {
  57.                 if (null !== $this->logger) {
  58.                     $this->logger->debug(sprintf('Object identity unavailable. Voting to %s.'$this->allowIfObjectIdentityUnavailable 'grant access' 'abstain'));
  59.                 }
  60.                 return $this->allowIfObjectIdentityUnavailable self::ACCESS_GRANTED self::ACCESS_ABSTAIN;
  61.             } elseif ($object instanceof FieldVote) {
  62.                 $field $object->getField();
  63.                 $object $object->getDomainObject();
  64.             } else {
  65.                 $field null;
  66.             }
  67.             if ($object instanceof ObjectIdentityInterface) {
  68.                 $oid $object;
  69.             } elseif (null === $oid $this->objectIdentityRetrievalStrategy->getObjectIdentity($object)) {
  70.                 if (null !== $this->logger) {
  71.                     $this->logger->debug(sprintf('Object identity unavailable. Voting to %s.'$this->allowIfObjectIdentityUnavailable 'grant access' 'abstain'));
  72.                 }
  73.                 return $this->allowIfObjectIdentityUnavailable self::ACCESS_GRANTED self::ACCESS_ABSTAIN;
  74.             }
  75.             if (!$this->supportsClass($oid->getType())) {
  76.                 return self::ACCESS_ABSTAIN;
  77.             }
  78.             $sids $this->securityIdentityRetrievalStrategy->getSecurityIdentities($token);
  79.             try {
  80.                 $acl $this->aclProvider->findAcl($oid$sids);
  81.                 if (null === $field && $acl->isGranted($masks$sidsfalse)) {
  82.                     if (null !== $this->logger) {
  83.                         $this->logger->debug('ACL found, permission granted. Voting to grant access.');
  84.                     }
  85.                     return self::ACCESS_GRANTED;
  86.                 } elseif (null !== $field && $acl->isFieldGranted($field$masks$sidsfalse)) {
  87.                     if (null !== $this->logger) {
  88.                         $this->logger->debug('ACL found, permission granted. Voting to grant access.');
  89.                     }
  90.                     return self::ACCESS_GRANTED;
  91.                 }
  92.                 if (null !== $this->logger) {
  93.                     $this->logger->debug('ACL found, insufficient permissions. Voting to deny access.');
  94.                 }
  95.                 return self::ACCESS_DENIED;
  96.             } catch (AclNotFoundException $e) {
  97.                 if (null !== $this->logger) {
  98.                     $this->logger->debug('No ACL found for the object identity. Voting to deny access.');
  99.                 }
  100.                 return self::ACCESS_DENIED;
  101.             } catch (NoAceFoundException $e) {
  102.                 if (null !== $this->logger) {
  103.                     $this->logger->debug('ACL found, no ACE applicable. Voting to deny access.');
  104.                 }
  105.                 return self::ACCESS_DENIED;
  106.             }
  107.         }
  108.         // no attribute was supported
  109.         return self::ACCESS_ABSTAIN;
  110.     }
  111.     /**
  112.      * You can override this method when writing a voter for a specific domain
  113.      * class.
  114.      *
  115.      * @param string $class The class name
  116.      *
  117.      * @return bool
  118.      */
  119.     public function supportsClass($class)
  120.     {
  121.         return true;
  122.     }
  123. }