|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Component\Security\Acl\Domain; |
|
13 |
|
14 use Symfony\Component\Security\Acl\Model\AclProviderInterface; |
|
15 use Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface; |
|
16 use Symfony\Component\Security\Acl\Model\SecurityIdentityRetrievalStrategyInterface; |
|
17 |
|
18 /** |
|
19 * This service caches ACLs for an entire collection of objects. |
|
20 * |
|
21 * @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
22 */ |
|
23 class AclCollectionCache |
|
24 { |
|
25 private $aclProvider; |
|
26 private $objectIdentityRetrievalStrategy; |
|
27 private $securityIdentityRetrievalStrategy; |
|
28 |
|
29 /** |
|
30 * Constructor. |
|
31 * |
|
32 * @param AclProviderInterface $aclProvider |
|
33 * @param ObjectIdentityRetrievalStrategy $oidRetrievalStrategy |
|
34 * @param SecurityIdentityRetrievalStrategy $sidRetrievalStrategy |
|
35 * @return void |
|
36 */ |
|
37 public function __construct(AclProviderInterface $aclProvider, ObjectIdentityRetrievalStrategyInterface $oidRetrievalStrategy, SecurityIdentityRetrievalStrategyInterface $sidRetrievalStrategy) |
|
38 { |
|
39 $this->aclProvider = $aclProvider; |
|
40 $this->objectIdentityRetrievalStrategy = $oidRetrievalStrategy; |
|
41 $this->securityIdentityRetrievalStrategy = $sidRetrievalStrategy; |
|
42 } |
|
43 |
|
44 /** |
|
45 * Batch loads ACLs for an entire collection; thus, it reduces the number |
|
46 * of required queries considerably. |
|
47 * |
|
48 * @param mixed $collection anything that can be passed to foreach() |
|
49 * @param array $tokens an array of TokenInterface implementations |
|
50 * @return void |
|
51 */ |
|
52 public function cache($collection, array $tokens = array()) |
|
53 { |
|
54 $sids = array(); |
|
55 foreach ($tokens as $token) { |
|
56 $sids = array_merge($sids, $this->securityIdentityRetrievalStrategy->getSecurityIdentities($token)); |
|
57 } |
|
58 |
|
59 $oids = array(); |
|
60 foreach ($collection as $domainObject) { |
|
61 $oids[] = $this->objectIdentityRetrievalStrategy->getObjectIdentity($domainObject); |
|
62 } |
|
63 |
|
64 $this->aclProvider->findAcls($oids, $sids); |
|
65 } |
|
66 } |