AclCollectionCache.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\Acl\Model\AclProviderInterface;
  12. use Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface;
  13. use Symfony\Component\Security\Acl\Model\SecurityIdentityRetrievalStrategyInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  15. /**
  16. * This service caches ACLs for an entire collection of objects.
  17. *
  18. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  19. */
  20. class AclCollectionCache
  21. {
  22. private $aclProvider;
  23. private $objectIdentityRetrievalStrategy;
  24. private $securityIdentityRetrievalStrategy;
  25. /**
  26. * Constructor.
  27. *
  28. * @param AclProviderInterface $aclProvider
  29. * @param ObjectIdentityRetrievalStrategyInterface $oidRetrievalStrategy
  30. * @param SecurityIdentityRetrievalStrategyInterface $sidRetrievalStrategy
  31. */
  32. public function __construct(AclProviderInterface $aclProvider, ObjectIdentityRetrievalStrategyInterface $oidRetrievalStrategy, SecurityIdentityRetrievalStrategyInterface $sidRetrievalStrategy)
  33. {
  34. $this->aclProvider = $aclProvider;
  35. $this->objectIdentityRetrievalStrategy = $oidRetrievalStrategy;
  36. $this->securityIdentityRetrievalStrategy = $sidRetrievalStrategy;
  37. }
  38. /**
  39. * Batch loads ACLs for an entire collection; thus, it reduces the number
  40. * of required queries considerably.
  41. *
  42. * @param mixed $collection anything that can be passed to foreach()
  43. * @param TokenInterface[] $tokens an array of TokenInterface implementations
  44. */
  45. public function cache($collection, array $tokens = array())
  46. {
  47. $sids = array();
  48. foreach ($tokens as $token) {
  49. $sids = array_merge($sids, $this->securityIdentityRetrievalStrategy->getSecurityIdentities($token));
  50. }
  51. $oids = array();
  52. foreach ($collection as $domainObject) {
  53. $oids[] = $this->objectIdentityRetrievalStrategy->getObjectIdentity($domainObject);
  54. }
  55. $this->aclProvider->findAcls($oids, $sids);
  56. }
  57. }