-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathNodePolicyService.php
189 lines (165 loc) · 6.23 KB
/
NodePolicyService.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
<?php
namespace Neos\Neos\Ui\Service;
/*
* This file is part of the Neos.Neos.Ui 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\Node;
use Neos\ContentRepository\Core\NodeType\NodeType;
use Neos\ContentRepository\Security\Authorization\Privilege\Node\CreateNodePrivilege;
use Neos\ContentRepository\Security\Authorization\Privilege\Node\CreateNodePrivilegeSubject;
use Neos\ContentRepository\Security\Authorization\Privilege\Node\EditNodePrivilege;
use Neos\ContentRepository\Security\Authorization\Privilege\Node\EditNodePropertyPrivilege;
use Neos\ContentRepository\Security\Authorization\Privilege\Node\NodePrivilegeSubject;
use Neos\ContentRepository\Security\Authorization\Privilege\Node\PropertyAwareNodePrivilegeSubject;
use Neos\ContentRepository\Security\Authorization\Privilege\Node\RemoveNodePrivilege;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Neos\Flow\Security\Authorization\Privilege\PrivilegeInterface;
use Neos\Flow\Security\Authorization\PrivilegeManagerInterface;
use Neos\Flow\Security\Policy\PolicyService;
use Neos\Neos\Security\Authorization\Privilege\NodeTreePrivilege;
/**
* @Flow\Scope("singleton")
*/
class NodePolicyService
{
/**
* @Flow\Inject
* @var PrivilegeManagerInterface
*/
protected $privilegeManager;
/**
* @Flow\Inject
* @var ContentRepositoryRegistry
*/
protected $contentRepositoryRegistry;
/**
* @Flow\Inject
* @var ObjectManagerInterface
*/
protected $objectManager;
/**
* @param ObjectManagerInterface $objectManager
* @return array<string,bool> the key is a Privilege class name;
* the value is "true" if privileges are configured for this class name.
* @Flow\CompileStatic
*/
public static function getUsedPrivilegeClassNames(ObjectManagerInterface $objectManager): array
{
/** @var PolicyService $policyService */
$policyService = $objectManager->get(PolicyService::class);
$usedPrivilegeClassNames = [];
foreach ($policyService->getPrivilegeTargets() as $privilegeTarget) {
$usedPrivilegeClassNames[$privilegeTarget->getPrivilegeClassName()] = true;
foreach (class_parents($privilegeTarget->getPrivilegeClassName()) ?: [] as $parentPrivilege) {
if (is_a($parentPrivilege, PrivilegeInterface::class)) {
/** @var string $parentPrivilege */
$usedPrivilegeClassNames[$parentPrivilege] = true;
}
}
}
return $usedPrivilegeClassNames;
}
/**
* @return array<string,mixed>
*/
public function getNodePolicyInformation(Node $node): array
{
return [
'disallowedNodeTypes' => $this->getDisallowedNodeTypes($node),
'canRemove' => $this->canRemoveNode($node),
'canEdit' => $this->canEditNode($node),
'disallowedProperties' => $this->getDisallowedProperties($node)
];
}
/**
* @param Node $node
* @return bool
*/
public function isNodeTreePrivilegeGranted(Node $node): bool
{
if (!isset(self::getUsedPrivilegeClassNames($this->objectManager)[NodeTreePrivilege::class])) {
return true;
}
return $this->privilegeManager->isGranted(
NodeTreePrivilege::class,
new NodePrivilegeSubject($node)
);
}
/**
* @param Node $node
* @return array<int,NodeType>
*/
public function getDisallowedNodeTypes(Node $node): array
{
$disallowedNodeTypes = [];
if (!isset(self::getUsedPrivilegeClassNames($this->objectManager)[CreateNodePrivilege::class])) {
return $disallowedNodeTypes;
}
$filter = function ($nodeType) use ($node) {
return !$this->privilegeManager->isGranted(
CreateNodePrivilege::class,
new CreateNodePrivilegeSubject($node, $nodeType)
);
};
$contentRepository = $this->contentRepositoryRegistry->get($node->subgraphIdentity->contentRepositoryId);
$disallowedNodeTypeObjects = array_filter($contentRepository->getNodeTypeManager()->getNodeTypes(), $filter);
$mapper = function ($nodeType) {
return $nodeType->getName();
};
return array_map($mapper, $disallowedNodeTypeObjects);
}
/**
* @param Node $node
* @return bool
*/
public function canRemoveNode(Node $node): bool
{
$canRemove = true;
if (isset(self::getUsedPrivilegeClassNames($this->objectManager)[RemoveNodePrivilege::class])) {
$canRemove = $this->privilegeManager->isGranted(
RemoveNodePrivilege::class,
new NodePrivilegeSubject($node)
);
}
return $canRemove;
}
/**
* @param Node $node
* @return bool
*/
public function canEditNode(Node $node): bool
{
$canEdit = true;
if (isset(self::getUsedPrivilegeClassNames($this->objectManager)[EditNodePrivilege::class])) {
$canEdit = $this->privilegeManager->isGranted(EditNodePrivilege::class, new NodePrivilegeSubject($node));
}
return $canEdit;
}
/**
* @param Node $node
* @return array<int,string>
*/
public function getDisallowedProperties(Node $node): array
{
$disallowedProperties = [];
if (!isset(self::getUsedPrivilegeClassNames($this->objectManager)[EditNodePropertyPrivilege::class])) {
return $disallowedProperties;
}
$filter = function ($propertyName) use ($node) {
return !$this->privilegeManager->isGranted(
EditNodePropertyPrivilege::class,
new PropertyAwareNodePrivilegeSubject($node, null, $propertyName)
);
};
$disallowedProperties = array_filter(array_keys($node->nodeType->getProperties()), $filter);
return $disallowedProperties;
}
}