-
-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathUsageController.php
216 lines (184 loc) · 7.69 KB
/
UsageController.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
<?php
declare(strict_types=1);
namespace Neos\Media\Browser\Controller;
/*
* This file is part of the Neos.Media.Browser 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.
*/
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindClosestNodeFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ActionController;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Service\AssetService;
use Neos\Neos\AssetUsage\Dto\AssetUsageReference;
use Neos\Neos\Domain\Repository\SiteRepository;
use Neos\Neos\Domain\Service\NodeTypeNameFactory;
use Neos\Neos\Domain\Service\WorkspaceService;
use Neos\Neos\FrontendRouting\SiteDetection\SiteDetectionResult;
use Neos\Neos\Security\Authorization\ContentRepositoryAuthorizationService;
use Neos\Neos\Service\UserService;
/**
* Controller for asset usage handling
*
* @Flow\Scope("singleton")
*/
class UsageController extends ActionController
{
/**
* @Flow\Inject
* @var AssetService
*/
protected $assetService;
/**
* @Flow\Inject
* @var UserService
*/
protected $userService;
/**
* @Flow\Inject
* @var SiteRepository
*/
protected $siteRepository;
/**
* @Flow\Inject
* @var ContentRepositoryRegistry
*/
protected $contentRepositoryRegistry;
/**
* @Flow\Inject
* @var WorkspaceService
*/
protected $workspaceService;
/**
* @Flow\Inject
* @var SecurityContext
*/
protected $securityContext;
/**
* @Flow\Inject
* @var ContentRepositoryAuthorizationService
*/
protected $contentRepositoryAuthorizationService;
/**
* @Flow\Inject
* @var PrivilegeManagerInterface
*/
protected $privilegeManager;
/**
* Get Related Nodes for an asset
*
* @param AssetInterface $asset
* @return void
*/
public function relatedNodesAction(AssetInterface $asset)
{
$currentContentRepositoryId = SiteDetectionResult::fromRequest($this->request->getHttpRequest())->contentRepositoryId;
$currentContentRepository = $this->contentRepositoryRegistry->get($currentContentRepositoryId);
$currentUser = $this->userService->getBackendUser();
assert($currentUser !== null);
$userWorkspace = $this->workspaceService->getPersonalWorkspaceForUser($currentContentRepositoryId, $currentUser->getId());
$usageReferences = $this->assetService->getUsageReferences($asset);
$relatedNodes = [];
$inaccessibleRelations = [];
$existingSites = $this->siteRepository->findAll();
foreach ($usageReferences as $usage) {
$inaccessibleRelation = [
'type' => get_class($usage),
'usage' => $usage
];
if (!$usage instanceof AssetUsageReference) {
$inaccessibleRelations[] = $inaccessibleRelation;
continue;
}
$contentRepository = $this->contentRepositoryRegistry->get($usage->getContentRepositoryId());
$nodeAggregate = $contentRepository->getContentGraph($usage->getWorkspaceName())->findNodeAggregateById(
$usage->getNodeAggregateId()
);
$nodeType = $nodeAggregate ? $contentRepository->getNodeTypeManager()->getNodeType($nodeAggregate->nodeTypeName) : null;
$workspacePermissions = $this->contentRepositoryAuthorizationService->getWorkspacePermissions($currentContentRepositoryId, $usage->getWorkspaceName(), $this->securityContext->getRoles(), $this->userService->getBackendUser()?->getId());
$workspace = $contentRepository->findWorkspaceByName($usage->getWorkspaceName());
$inaccessibleRelation['label'] = $this->getLabelForInaccessibleWorkspace($workspace);
$inaccessibleRelation['nodeIdentifier'] = $usage->getNodeAggregateId()->value;
$inaccessibleRelation['workspaceName'] = $usage->getWorkspaceName()->value;
$inaccessibleRelation['workspace'] = $workspace;
$inaccessibleRelation['nodeType'] = $nodeType;
$inaccessibleRelation['accessible'] = $workspacePermissions->read;
if (!$workspacePermissions->read) {
$inaccessibleRelations[] = $inaccessibleRelation;
continue;
}
$subgraph = $contentRepository->getContentGraph($usage->getWorkspaceName())->getSubgraph(
$usage->getOriginDimensionSpacePoint()->toDimensionSpacePoint(),
VisibilityConstraints::withoutRestrictions()
);
$node = $subgraph->findNodeById($usage->getNodeAggregateId());
// this should actually never happen.
if (!$node) {
$inaccessibleRelations[] = $inaccessibleRelation;
continue;
}
$documentNode = $subgraph->findClosestNode($node->aggregateId, FindClosestNodeFilter::create(nodeTypes: NodeTypeNameFactory::NAME_DOCUMENT));
// this should actually never happen, too.
if (!$documentNode) {
$inaccessibleRelations[] = $inaccessibleRelation;
continue;
}
$siteNode = $subgraph->findClosestNode($node->aggregateId, FindClosestNodeFilter::create(nodeTypes: NodeTypeNameFactory::NAME_SITE));
// this should actually never happen, too. :D
if (!$siteNode || !$siteNode->name) {
$inaccessibleRelations[] = $inaccessibleRelation;
continue;
}
$site = null;
foreach ($existingSites as $existingSite) {
if ($siteNode->name->equals($existingSite->getNodeName()->toNodeName())) {
$site = $existingSite;
}
}
// guessed it? this should actually never as well ^^
if (!$site) {
$inaccessibleRelations[] = $inaccessibleRelation;
continue;
}
$relatedNodes[$site->getNodeName()->value]['site'] = $site;
$relatedNodes[$site->getNodeName()->value]['nodes'][] = [
'node' => $node,
'workspace' => $workspace,
'documentNode' => $documentNode
];
}
$this->view->assignMultiple([
'totalUsageCount' => count($usageReferences),
'nodeUsageClass' => AssetUsageReference::class,
'asset' => $asset,
'inaccessibleRelations' => $inaccessibleRelations,
'relatedNodes' => $relatedNodes,
'contentDimensions' => $currentContentRepository->getContentDimensionSource()->getContentDimensionsOrderedByPriority(),
'userWorkspace' => $userWorkspace,
]);
}
private function getLabelForInaccessibleWorkspace(Workspace $workspace): string
{
/*
TODO: Needs to get re-implemented for Neos 9.
See: https://github.com/neos/neos-development-collection/pull/5182
$currentAccount = $this->securityContext->getAccount();
if ($currentAccount != null && $this->privilegeManager->isPrivilegeTargetGranted('Neos.Media.Browser:WorkspaceName')) {
if ($workspace->isPrivateWorkspace()) {
$owner = $workspace->getOwner();
return '(' . $owner->getLabel() . ')';
} else {
return '(' . $workspace->getTitle() . ')';
}
}
*/
return '';
}
}