Completed
Push — 6.0 ( eb641c...92cb73 )
by Ruud
148:12 queued 131:31
created

AclManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Service;
4
5
use Kunstmaan\NodeBundle\Entity\Node;
6
use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface;
7
use Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface;
8
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
9
10
/**
11
 * Class AclManager
12
 * @package Kunstmaan\AdminBundle\Security
13
 */
14
class AclManager
15
{
16
    /** @var MutableAclProviderInterface */
17
    private $aclProvider;
18
19
    /** @var ObjectIdentityRetrievalStrategyInterface */
20
    private $objectIdentityRetrievalStrategy;
21
22
    public function __construct(MutableAclProviderInterface $aclProvider, ObjectIdentityRetrievalStrategyInterface $objectIdentityRetrievalStrategy)
23
    {
24
        $this->aclProvider = $aclProvider;
25
        $this->objectIdentityRetrievalStrategy = $objectIdentityRetrievalStrategy;
26
    }
27
28
    /**
29
     * @param $originalNode
30
     * @param $nodeNewPage
31
     */
32
    public function updateNodeAcl(Node $originalNode, Node $nodeNewPage)
33
    {
34
        $originalIdentity = $this->objectIdentityRetrievalStrategy->getObjectIdentity($originalNode);
35
        $originalAcl = $this->aclProvider->findAcl($originalIdentity);
36
37
        $newIdentity = $this->objectIdentityRetrievalStrategy->getObjectIdentity($nodeNewPage);
38
        $newAcl = $this->aclProvider->createAcl($newIdentity);
39
40
        $aces = $originalAcl->getObjectAces();
41
        /* @var EntryInterface $ace */
42
        foreach ($aces as $ace) {
43
            $securityIdentity = $ace->getSecurityIdentity();
44
            if ($securityIdentity instanceof RoleSecurityIdentity) {
45
                $newAcl->insertObjectAce($securityIdentity, $ace->getMask());
46
            }
47
        }
48
        $this->aclProvider->updateAcl($newAcl);
49
50
    }
51
52
    /**
53
     * @param array     $nodes
54
     * @param string    $role
55
     * @param int       $mask
56
     */
57
    public function updateNodesAclToRole(array $nodes, $role, $mask)
58
    {
59
        foreach ($nodes as $node) {
60
            $objectIdentity = $this->objectIdentityRetrievalStrategy->getObjectIdentity($node);
61
62
            /** @var Acl $acl */
63
            $acl = $this->aclProvider->findAcl($objectIdentity);
64
            $securityIdentity = new RoleSecurityIdentity($role);
65
66
            /** @var Entry $ace */
67
            foreach ($acl->getObjectAces() as $index => $ace) {
68
                if (!$ace->getSecurityIdentity()->equals($securityIdentity)) {
69
                    continue;
70
                }
71
                $acl->updateObjectAce($index, $mask);
72
                break;
73
            }
74
            $this->aclProvider->updateAcl($acl);
75
        }
76
    }
77
}
78