Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

AclManager::applyAclChangesets()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 12
cp 0
rs 9.7333
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 12
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Service;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Kunstmaan\AdminBundle\Entity\AclChangeset;
7
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin;
8
use Kunstmaan\NodeBundle\Entity\Node;
9
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
10
use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface;
11
use Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface;
12
13
/**
14
 * Class AclManager
15
 */
16
class AclManager
17
{
18
    /** @var MutableAclProviderInterface */
19
    private $aclProvider;
20
21
    /** @var ObjectIdentityRetrievalStrategyInterface */
22
    private $objectIdentityRetrievalStrategy;
23
24
    /** @var EntityManagerInterface */
25
    private $em;
26
27
    /** @var PermissionAdmin */
28
    private $permissionAdmin;
29
30
    public function __construct(MutableAclProviderInterface $aclProvider, ObjectIdentityRetrievalStrategyInterface $objectIdentityRetrievalStrategy, EntityManagerInterface $em, PermissionAdmin $permissionAdmin)
31
    {
32
        $this->aclProvider = $aclProvider;
33
        $this->objectIdentityRetrievalStrategy = $objectIdentityRetrievalStrategy;
34
        $this->em = $em;
35
        $this->permissionAdmin = $permissionAdmin;
36
    }
37
38
    /**
39
     * @param $originalNode
40
     * @param $nodeNewPage
41
     */
42 View Code Duplication
    public function updateNodeAcl(Node $originalNode, Node $nodeNewPage)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        $originalIdentity = $this->objectIdentityRetrievalStrategy->getObjectIdentity($originalNode);
45
        $originalAcl = $this->aclProvider->findAcl($originalIdentity);
46
47
        $newIdentity = $this->objectIdentityRetrievalStrategy->getObjectIdentity($nodeNewPage);
48
        $newAcl = $this->aclProvider->createAcl($newIdentity);
49
50
        $aces = $originalAcl->getObjectAces();
51
        /* @var EntryInterface $ace */
52
        foreach ($aces as $ace) {
53
            $securityIdentity = $ace->getSecurityIdentity();
54
            if ($securityIdentity instanceof RoleSecurityIdentity) {
55
                $newAcl->insertObjectAce($securityIdentity, $ace->getMask());
56
            }
57
        }
58
        $this->aclProvider->updateAcl($newAcl);
59
    }
60
61
    /**
62
     * @param string $role
63
     * @param int    $mask
64
     */
65
    public function updateNodesAclToRole(array $nodes, $role, $mask)
66
    {
67
        foreach ($nodes as $node) {
68
            $objectIdentity = $this->objectIdentityRetrievalStrategy->getObjectIdentity($node);
69
70
            /** @var Acl $acl */
71
            $acl = $this->aclProvider->findAcl($objectIdentity);
72
            $securityIdentity = new RoleSecurityIdentity($role);
73
74
            /** @var Entry $ace */
75
            foreach ($acl->getObjectAces() as $index => $ace) {
76
                if (!$ace->getSecurityIdentity()->equals($securityIdentity)) {
77
                    continue;
78
                }
79
                $acl->updateObjectAce($index, $mask);
80
81
                break;
82
            }
83
            $this->aclProvider->updateAcl($acl);
84
        }
85
    }
86
87
    public function applyAclChangesets()
88
    {
89
        /* @var AclChangesetRepository $aclRepo */
90
        $aclRepo = $this->em->getRepository(AclChangeset::class);
91
        do {
92
            /* @var AclChangeset $changeset */
93
            $changeset = $aclRepo->findNewChangeset();
94
            if (\is_null($changeset)) {
95
                break;
96
            }
97
98
            $this->applyAclChangeSet($changeset);
99
100
            $hasPending = $aclRepo->hasPendingChangesets();
101
        } while ($hasPending);
102
    }
103
104
    public function applyAclChangeSet(AclChangeset $aclChangeset)
105
    {
106
        $aclChangeset->setPid(getmypid());
107
        $aclChangeset->setStatus(AclChangeset::STATUS_RUNNING);
108
        $this->em->persist($aclChangeset);
109
        $this->em->flush();
110
111
        $entity = $this->em->getRepository($aclChangeset->getRefEntityName())->find($aclChangeset->getRefId());
112
        $this->permissionAdmin->applyAclChangeset($entity, $aclChangeset->getChangeset());
0 ignored issues
show
Documentation introduced by
$entity is of type object|null, but the function expects a object<Kunstmaan\AdminBu...\Entity\AbstractEntity>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
113
114
        $aclChangeset->setStatus(AclChangeset::STATUS_FINISHED);
115
        $this->em->persist($aclChangeset);
116
        $this->em->flush();
117
    }
118
}
119