-
-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathNodeController.php
374 lines (323 loc) · 14.3 KB
/
NodeController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
declare(strict_types=1);
namespace Neos\Neos\Controller\Frontend;
use Neos\ContentRepository\Core\ContentRepository;
use Neos\ContentRepository\Core\Projection\ContentGraph\ContentGraphWithRuntimeCaches\ContentSubgraphWithRuntimeCaches;
use Neos\ContentRepository\Core\Projection\ContentGraph\ContentGraphWithRuntimeCaches\InMemoryCache;
use Neos\ContentRepository\Core\Projection\ContentGraph\ContentSubgraphInterface;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindClosestNodeFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindSubtreeFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\Projection\ContentGraph\Nodes;
use Neos\ContentRepository\Core\Projection\ContentGraph\Subtree;
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ActionController;
use Neos\Flow\Mvc\Exception\NoMatchingRouteException;
use Neos\Flow\Property\PropertyMapper;
use Neos\Flow\Security\Authorization\PrivilegeManagerInterface;
use Neos\Flow\Security\Context as SecurityContext;
use Neos\Flow\Session\SessionInterface;
use Neos\Flow\Utility\Now;
use Neos\Neos\Domain\Model\RenderingMode;
use Neos\Neos\Domain\Service\NodeTypeNameFactory;
use Neos\Neos\Domain\Service\RenderingModeService;
use Neos\Neos\FrontendRouting\Exception\InvalidShortcutException;
use Neos\Neos\FrontendRouting\Exception\NodeNotFoundException;
use Neos\Neos\FrontendRouting\NodeAddress;
use Neos\Neos\FrontendRouting\NodeAddressFactory;
use Neos\Neos\FrontendRouting\NodeShortcutResolver;
use Neos\Neos\FrontendRouting\NodeUriBuilder;
use Neos\Neos\FrontendRouting\SiteDetection\SiteDetectionResult;
use Neos\Neos\Utility\NodeTypeWithFallbackProvider;
use Neos\Neos\View\FusionView;
class NodeController extends ActionController
{
use NodeTypeWithFallbackProvider;
#[Flow\Inject]
protected ContentRepositoryRegistry $contentRepositoryRegistry;
/**
* @Flow\Inject
* @var PrivilegeManagerInterface
*/
protected $privilegeManager;
/**
* @Flow\Inject(lazy=false)
* @var Now
*/
protected $now;
/**
* @Flow\Inject
* @var SecurityContext
*/
protected $securityContext;
/**
* @Flow\Inject
* @var SessionInterface
*/
protected $session;
/**
* @Flow\Inject
* @var NodeShortcutResolver
*/
protected $nodeShortcutResolver;
/**
* @Flow\Inject
* @var PropertyMapper
*/
protected $propertyMapper;
/**
* @var string
*/
protected $defaultViewObjectName = FusionView::class;
/**
* @var FusionView
*/
protected $view;
#[Flow\Inject]
protected RenderingModeService $renderingModeService;
#[Flow\InjectConfiguration(path: "frontend.shortcutRedirectHttpStatusCode", package: "Neos.Neos")]
protected int $shortcutRedirectHttpStatusCode;
/**
* @param string $node
* @throws NodeNotFoundException
* @throws \Neos\Flow\Mvc\Exception\StopActionException
* @throws \Neos\Flow\Mvc\Routing\Exception\MissingActionNameException
* @throws \Neos\Flow\Session\Exception\SessionNotStartedException
* @throws \Neos\Neos\Exception
* @Flow\SkipCsrfProtection We need to skip CSRF protection here because this action could be called
* with unsafe requests from widgets or plugins that are rendered on the node
* - For those the CSRF token is validated on the sub-request, so it is safe to be skipped here
*/
public function previewAction(string $node): void
{
// @todo add $renderingModeName as parameter and append it for successive links again as get parameter to node uris
$renderingMode = $this->renderingModeService->findByCurrentUser();
$visibilityConstraints = VisibilityConstraints::frontend();
if ($this->privilegeManager->isPrivilegeTargetGranted('Neos.Neos:Backend.GeneralAccess')) {
$visibilityConstraints = VisibilityConstraints::withoutRestrictions();
}
$siteDetectionResult = SiteDetectionResult::fromRequest($this->request->getHttpRequest());
$contentRepository = $this->contentRepositoryRegistry->get($siteDetectionResult->contentRepositoryId);
$nodeAddress = NodeAddressFactory::create($contentRepository)->createFromUriString($node);
$subgraph = $contentRepository->getContentGraph()->getSubgraph(
$nodeAddress->contentStreamId,
$nodeAddress->dimensionSpacePoint,
$visibilityConstraints
);
$site = $subgraph->findClosestNode($nodeAddress->nodeAggregateId, FindClosestNodeFilter::create(nodeTypes: NodeTypeNameFactory::NAME_SITE));
if ($site === null) {
throw new NodeNotFoundException("TODO: SITE NOT FOUND; should not happen (for address " . $nodeAddress);
}
$this->fillCacheWithContentNodes($nodeAddress->nodeAggregateId, $subgraph);
$nodeInstance = $subgraph->findNodeById($nodeAddress->nodeAggregateId);
if (is_null($nodeInstance)) {
throw new NodeNotFoundException(
'The requested node does not exist or isn\'t accessible to the current user',
1430218623
);
}
if (
$this->getNodeType($nodeInstance)->isOfType(NodeTypeNameFactory::NAME_SHORTCUT)
&& !$renderingMode->isEdit
&& $nodeAddress->workspaceName->isLive() // shortcuts are only resolvable for the live workspace
) {
$this->handleShortcutNode($nodeAddress, $contentRepository);
}
$this->view->setOption('renderingModeName', $renderingMode->name);
$this->view->assignMultiple([
'value' => $nodeInstance,
'site' => $site,
]);
if ($renderingMode->isEdit) {
$this->overrideViewVariablesFromInternalArguments();
$this->response->setHttpHeader('Cache-Control', 'no-cache');
if (!$this->view->canRenderWithNodeAndPath()) {
$this->view->setFusionPath('rawContent');
}
}
}
/**
* Initializes the view with the necessary parameters encoded in the given NodeAddress
*
* @param string $node Legacy name for backwards compatibility of route components
* @throws NodeNotFoundException
* @throws \Neos\Flow\Mvc\Exception\StopActionException
* @throws \Neos\Flow\Mvc\Routing\Exception\MissingActionNameException
* @throws \Neos\Flow\Session\Exception\SessionNotStartedException
* @throws \Neos\Neos\Exception
* @Flow\SkipCsrfProtection We need to skip CSRF protection here because this action could be called
* with unsafe requests from widgets or plugins that are rendered on the node
* - For those the CSRF token is validated on the sub-request, so it is safe to be skipped here
*/
public function showAction(string $node): void
{
$siteDetectionResult = SiteDetectionResult::fromRequest($this->request->getHttpRequest());
$contentRepository = $this->contentRepositoryRegistry->get($siteDetectionResult->contentRepositoryId);
$nodeAddress = NodeAddressFactory::create($contentRepository)->createFromUriString($node);
if (!$nodeAddress->isInLiveWorkspace()) {
throw new NodeNotFoundException('The requested node isn\'t accessible to the current user', 1430218623);
}
$subgraph = $contentRepository->getContentGraph()->getSubgraph(
$nodeAddress->contentStreamId,
$nodeAddress->dimensionSpacePoint,
VisibilityConstraints::frontend()
);
$nodeInstance = $subgraph->findNodeById($nodeAddress->nodeAggregateId);
if ($nodeInstance === null) {
throw new NodeNotFoundException(sprintf('The cached node address for this uri could not be resolved. Possibly you have to flush the "Flow_Mvc_Routing_Route" cache. %s', $nodeAddress), 1707300738);
}
$site = $subgraph->findClosestNode($nodeAddress->nodeAggregateId, FindClosestNodeFilter::create(nodeTypes: NodeTypeNameFactory::NAME_SITE));
if ($site === null) {
throw new NodeNotFoundException(sprintf('The site node of %s could not be resolved.', $nodeAddress), 1707300861);
}
$this->fillCacheWithContentNodes($nodeAddress->nodeAggregateId, $subgraph);
if ($this->getNodeType($nodeInstance)->isOfType(NodeTypeNameFactory::NAME_SHORTCUT)) {
$this->handleShortcutNode($nodeAddress, $contentRepository);
}
$this->view->setOption('renderingModeName', RenderingMode::FRONTEND);
$this->view->assignMultiple([
'value' => $nodeInstance,
'site' => $site,
]);
}
/**
* Checks if the optionally given node context path, affected node context path and Fusion path are set
* and overrides the rendering to use those. Will also add a "X-Neos-AffectedNodePath" header in case the
* actually affected node is different from the one routing resolved.
* This is used in out of band rendering for the backend.
*
* @return void
* @throws NodeNotFoundException
*/
protected function overrideViewVariablesFromInternalArguments()
{
if (($nodeContextPath = $this->request->getInternalArgument('__nodeContextPath')) !== null) {
assert(is_string($nodeContextPath));
$node = $this->propertyMapper->convert($nodeContextPath, Node::class);
if (!$node instanceof Node) {
throw new NodeNotFoundException(sprintf(
'The node with context path "%s" could not be resolved',
$nodeContextPath
), 1437051934);
}
$this->view->assign('value', $node);
}
if (($affectedNodeContextPath = $this->request->getInternalArgument('__affectedNodeContextPath')) !== null) {
assert(is_string($affectedNodeContextPath));
$this->response->setHttpHeader('X-Neos-AffectedNodePath', $affectedNodeContextPath);
}
if (($fusionPath = $this->request->getInternalArgument('__fusionPath')) !== null) {
assert(is_string($fusionPath));
$this->view->setFusionPath($fusionPath);
}
}
/**
* Handles redirects to shortcut targets of nodes in the live workspace.
*
* @param NodeAddress $nodeAddress
* @throws NodeNotFoundException
* @throws \Neos\Flow\Mvc\Exception\StopActionException
*/
protected function handleShortcutNode(NodeAddress $nodeAddress, ContentRepository $contentRepository): void
{
try {
$resolvedTarget = $this->nodeShortcutResolver->resolveShortcutTarget($nodeAddress, $contentRepository);
} catch (InvalidShortcutException $e) {
throw new NodeNotFoundException(sprintf(
'The shortcut node target of node "%s" could not be resolved: %s',
$nodeAddress,
$e->getMessage()
), 1430218730, $e);
}
if ($resolvedTarget instanceof NodeAddress) {
if ($resolvedTarget === $nodeAddress) {
return;
}
try {
$resolvedUri = NodeUriBuilder::fromRequest($this->request)->uriFor($nodeAddress);
} catch (NoMatchingRouteException $e) {
throw new NodeNotFoundException(sprintf(
'The shortcut node target of node "%s" could not be resolved: %s',
$nodeAddress,
$e->getMessage()
), 1599670695, $e);
}
} else {
$resolvedUri = $resolvedTarget;
}
$this->redirectToUri($resolvedUri, statusCode: $this->shortcutRedirectHttpStatusCode);
}
private function fillCacheWithContentNodes(
NodeAggregateId $nodeAggregateId,
ContentSubgraphInterface $subgraph,
): void {
if (!$subgraph instanceof ContentSubgraphWithRuntimeCaches) {
// wrong subgraph implementation
return;
}
$inMemoryCache = $subgraph->inMemoryCache;
$subtree = $subgraph->findSubtree(
$nodeAggregateId,
FindSubtreeFilter::create(nodeTypes: '!' . NodeTypeNameFactory::NAME_DOCUMENT, maximumLevels: 20)
);
if ($subtree === null) {
return;
}
$currentDocumentNode = $subtree->node;
foreach ($subtree->children as $childSubtree) {
self::fillCacheInternal(
$childSubtree,
$currentDocumentNode,
$inMemoryCache
);
}
}
private static function fillCacheInternal(
Subtree $subtree,
Node $parentNode,
InMemoryCache $inMemoryCache
): void {
$node = $subtree->node;
$parentNodeIdentifierByChildNodeIdentifierCache
= $inMemoryCache->getParentNodeIdByChildNodeIdCache();
$namedChildNodeByNodeIdentifierCache = $inMemoryCache->getNamedChildNodeByNodeIdCache();
$allChildNodesByNodeIdentifierCache = $inMemoryCache->getAllChildNodesByNodeIdCache();
if ($node->nodeName !== null) {
$namedChildNodeByNodeIdentifierCache->add(
$parentNode->nodeAggregateId,
$node->nodeName,
$node
);
} else {
// @todo use node aggregate identifier instead?
}
$parentNodeIdentifierByChildNodeIdentifierCache->add(
$node->nodeAggregateId,
$parentNode->nodeAggregateId
);
$allChildNodes = [];
foreach ($subtree->children as $childSubtree) {
self::fillCacheInternal($childSubtree, $node, $inMemoryCache);
$childNode = $childSubtree->node;
$allChildNodes[] = $childNode;
}
// TODO Explain why this is safe (Content can not contain other documents)
$allChildNodesByNodeIdentifierCache->add(
$node->nodeAggregateId,
null,
Nodes::fromArray($allChildNodes)
);
}
}