1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\NodeBundle\Helper; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Kunstmaan\AdminBundle\Entity\BaseUser; |
7
|
|
|
use Kunstmaan\AdminBundle\Helper\CloneHelper; |
8
|
|
|
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap; |
9
|
|
|
use Kunstmaan\NodeBundle\Entity\DuplicateSubPageInterface; |
10
|
|
|
use Kunstmaan\NodeBundle\Entity\HasNodeInterface; |
11
|
|
|
use Kunstmaan\NodeBundle\Entity\Node; |
12
|
|
|
use Kunstmaan\NodeBundle\Entity\PageInterface; |
13
|
|
|
use Kunstmaan\NodeBundle\Event\Events; |
14
|
|
|
use Kunstmaan\NodeBundle\Event\NodeDuplicateEvent; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
16
|
|
|
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity; |
17
|
|
|
use Symfony\Component\Security\Acl\Model\AclProviderInterface; |
18
|
|
|
use Symfony\Component\Security\Acl\Model\EntryInterface; |
19
|
|
|
use Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface; |
20
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
21
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
22
|
|
|
|
23
|
|
|
class PageCloningHelper |
24
|
|
|
{ |
25
|
|
|
/** @var EntityManagerInterface */ |
26
|
|
|
private $em; |
27
|
|
|
|
28
|
|
|
/** @var CloneHelper */ |
29
|
|
|
private $cloneHelper; |
30
|
|
|
|
31
|
|
|
/** @var AclProviderInterface */ |
32
|
|
|
private $aclProvider; |
33
|
|
|
|
34
|
|
|
/** @var ObjectIdentityRetrievalStrategyInterface */ |
35
|
|
|
private $identityRetrievalStrategy; |
36
|
|
|
|
37
|
|
|
/** @var AuthorizationCheckerInterface */ |
38
|
|
|
private $authorizationCheckerInterface; |
39
|
|
|
|
40
|
|
|
/** @var EventDispatcherInterface */ |
41
|
|
|
private $eventDispatcherInterface; |
42
|
|
|
|
43
|
|
|
public function __construct(EntityManagerInterface $em, CloneHelper $cloneHelper, AclProviderInterface $aclProvider, ObjectIdentityRetrievalStrategyInterface $identityRetrivalStrategy, AuthorizationCheckerInterface $authorizationChecker, EventDispatcherInterface $eventDispatcher) |
44
|
|
|
{ |
45
|
|
|
$this->em = $em; |
46
|
|
|
$this->cloneHelper = $cloneHelper; |
47
|
|
|
$this->aclProvider = $aclProvider; |
48
|
|
|
$this->identityRetrievalStrategy = $identityRetrivalStrategy; |
49
|
|
|
$this->authorizationCheckerInterface = $authorizationChecker; |
50
|
|
|
$this->eventDispatcherInterface = $eventDispatcher; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @throws AccessDeniedException |
55
|
|
|
*/ |
56
|
|
|
public function duplicateWithChildren($id, string $locale, BaseUser $user, string $title = null): Node |
57
|
|
|
{ |
58
|
|
|
/* @var Node $parentNode */ |
59
|
|
|
$originalNode = $this->em->getRepository('KunstmaanNodeBundle:Node') |
60
|
|
|
->find($id); |
61
|
|
|
|
62
|
|
|
$this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $originalNode); |
63
|
|
|
|
64
|
|
|
$this->eventDispatcherInterface->dispatch( |
65
|
|
|
Events::PRE_DUPLICATE_WITH_CHILDREN, |
66
|
|
|
new NodeDuplicateEvent($originalNode) |
|
|
|
|
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$newPage = $this->clonePage($originalNode, $locale, $title); |
|
|
|
|
70
|
|
|
$nodeNewPage = $this->createNodeStructureForNewPage($originalNode, $newPage, $user, $locale); |
|
|
|
|
71
|
|
|
$this->cloneChildren($originalNode, $newPage, $user, $locale); |
|
|
|
|
72
|
|
|
|
73
|
|
|
$this->eventDispatcherInterface->dispatch( |
74
|
|
|
Events::POST_DUPLICATE_WITH_CHILDREN, |
75
|
|
|
new NodeDuplicateEvent($originalNode) |
|
|
|
|
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
return $nodeNewPage; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function denyAccessUnlessGranted($attributes, $subject = null, $message = 'Access Denied.') |
82
|
|
|
{ |
83
|
|
|
if (!$this->authorizationCheckerInterface->isGranted($attributes, $subject)) { |
84
|
|
|
$exception = new AccessDeniedException(); |
85
|
|
|
$exception->setAttributes($attributes); |
86
|
|
|
$exception->setSubject($subject); |
87
|
|
|
|
88
|
|
|
throw $exception; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function clonePage(Node $originalNode, $locale, $title = null) |
93
|
|
|
{ |
94
|
|
|
$originalNodeTranslations = $originalNode->getNodeTranslation($locale, true); |
95
|
|
|
$originalRef = $originalNodeTranslations->getPublicNodeVersion()->getRef($this->em); |
96
|
|
|
|
97
|
|
|
$newPage = $this->cloneHelper |
98
|
|
|
->deepCloneAndSave($originalRef); |
99
|
|
|
|
100
|
|
|
if ($title !== null) { |
101
|
|
|
$newPage->setTitle($title); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
//set the parent |
105
|
|
|
$parentNodeTranslation = $originalNode->getParent()->getNodeTranslation($locale, true); |
106
|
|
|
$parent = $parentNodeTranslation->getPublicNodeVersion()->getRef($this->em); |
107
|
|
|
$newPage->setParent($parent); |
108
|
|
|
$this->em->persist($newPage); |
109
|
|
|
$this->em->flush(); |
110
|
|
|
|
111
|
|
|
return $newPage; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function createNodeStructureForNewPage(Node $originalNode, HasNodeInterface $newPage, BaseUser $user, string $locale): Node |
115
|
|
|
{ |
116
|
|
|
/* @var Node $nodeNewPage */ |
117
|
|
|
$nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')->createNodeFor( |
|
|
|
|
118
|
|
|
$newPage, |
119
|
|
|
$locale, |
120
|
|
|
$user |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
if ($newPage->isStructureNode()) { |
124
|
|
|
$nodeTranslation = $nodeNewPage->getNodeTranslation($locale, true); |
125
|
|
|
$nodeTranslation->setSlug(''); |
126
|
|
|
$this->em->persist($nodeTranslation); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
$this->em->flush(); |
129
|
|
|
|
130
|
|
|
$this->updateAcl($originalNode, $nodeNewPage); |
131
|
|
|
|
132
|
|
|
return $nodeNewPage; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
View Code Duplication |
private function updateAcl($originalNode, $nodeNewPage): void |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$originalIdentity = $this->identityRetrievalStrategy->getObjectIdentity($originalNode); |
138
|
|
|
$originalAcl = $this->aclProvider->findAcl($originalIdentity); |
139
|
|
|
|
140
|
|
|
$newIdentity = $this->identityRetrievalStrategy->getObjectIdentity($nodeNewPage); |
141
|
|
|
$newAcl = $this->aclProvider->createAcl($newIdentity); |
|
|
|
|
142
|
|
|
|
143
|
|
|
$aces = $originalAcl->getObjectAces(); |
144
|
|
|
/* @var EntryInterface $ace */ |
145
|
|
|
foreach ($aces as $ace) { |
146
|
|
|
$securityIdentity = $ace->getSecurityIdentity(); |
147
|
|
|
if ($securityIdentity instanceof RoleSecurityIdentity) { |
148
|
|
|
$newAcl->insertObjectAce($securityIdentity, $ace->getMask()); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
$this->aclProvider->updateAcl($newAcl); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
private function cloneChildren(Node $originalNode, PageInterface $newPage, BaseUser $user, string $locale): void |
155
|
|
|
{ |
156
|
|
|
$nodeChildren = $originalNode->getChildren(); |
157
|
|
|
/** @var Node $originalNodeChild */ |
158
|
|
|
foreach ($nodeChildren as $originalNodeChild) { |
159
|
|
|
$originalNodeTranslations = $originalNodeChild->getNodeTranslation($locale, true); |
160
|
|
|
$originalRef = $originalNodeTranslations->getPublicNodeVersion()->getRef($this->em); |
161
|
|
|
|
162
|
|
|
if (!$originalRef instanceof DuplicateSubPageInterface || !$originalRef->skipClone()) { |
163
|
|
|
$newChildPage = $this->clonePage($originalNodeChild, $locale); |
164
|
|
|
$newChildPage->setParent($newPage); |
165
|
|
|
$this->createNodeStructureForNewPage($originalNodeChild, $newChildPage, $user, $locale); |
166
|
|
|
$this->cloneChildren($originalNodeChild, $newChildPage, $user, $locale); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
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: