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

src/Kunstmaan/AdminBundle/Service/AclManager.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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());
113
114
        $aclChangeset->setStatus(AclChangeset::STATUS_FINISHED);
115
        $this->em->persist($aclChangeset);
116
        $this->em->flush();
117
    }
118
}
119