AclCollectionCache.php 2.2 KB

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