|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the RoleDomainMapper class. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace eZ\Publish\Core\Repository\Helper; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\Core\Repository\Values\User\Policy; |
|
12
|
|
|
use eZ\Publish\Core\Repository\Values\User\PolicyDraft; |
|
13
|
|
|
use eZ\Publish\Core\Repository\Values\User\Role; |
|
14
|
|
|
use eZ\Publish\API\Repository\Values\User\Role as APIRole; |
|
15
|
|
|
use eZ\Publish\Core\Repository\Values\User\RoleDraft; |
|
16
|
|
|
use eZ\Publish\API\Repository\Values\User\RoleCreateStruct as APIRoleCreateStruct; |
|
17
|
|
|
use eZ\Publish\API\Repository\Values\User\RoleCopyStruct as APIRoleCopyStruct; |
|
18
|
|
|
use eZ\Publish\Core\Repository\Values\User\UserRoleAssignment; |
|
19
|
|
|
use eZ\Publish\Core\Repository\Values\User\UserGroupRoleAssignment; |
|
20
|
|
|
use eZ\Publish\API\Repository\Values\User\User; |
|
21
|
|
|
use eZ\Publish\API\Repository\Values\User\UserGroup; |
|
22
|
|
|
use eZ\Publish\SPI\Persistence\User\Policy as SPIPolicy; |
|
23
|
|
|
use eZ\Publish\SPI\Persistence\User\RoleAssignment as SPIRoleAssignment; |
|
24
|
|
|
use eZ\Publish\SPI\Persistence\User\Role as SPIRole; |
|
25
|
|
|
use eZ\Publish\SPI\Persistence\User\RoleCreateStruct as SPIRoleCreateStruct; |
|
26
|
|
|
use eZ\Publish\SPI\Persistence\User\RoleCopyStruct as SPIRoleCopyStruct; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Internal service to map Role objects between API and SPI values. |
|
30
|
|
|
* |
|
31
|
|
|
* @internal Meant for internal use by Repository. |
|
32
|
|
|
*/ |
|
33
|
|
|
class RoleDomainMapper |
|
34
|
|
|
{ |
|
35
|
|
|
/** @var \eZ\Publish\Core\Repository\Helper\LimitationService */ |
|
36
|
|
|
protected $limitationService; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param \eZ\Publish\Core\Repository\Helper\LimitationService $limitationService |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(LimitationService $limitationService) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->limitationService = $limitationService; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Maps provided SPI Role value object to API Role value object. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \eZ\Publish\SPI\Persistence\User\Role $role |
|
50
|
|
|
* |
|
51
|
|
|
* @return \eZ\Publish\API\Repository\Values\User\Role |
|
52
|
|
|
*/ |
|
53
|
|
|
public function buildDomainRoleObject(SPIRole $role) |
|
54
|
|
|
{ |
|
55
|
|
|
$rolePolicies = []; |
|
56
|
|
|
foreach ($role->policies as $spiPolicy) { |
|
57
|
|
|
$rolePolicies[] = $this->buildDomainPolicyObject($spiPolicy); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return new Role( |
|
61
|
|
|
[ |
|
62
|
|
|
'id' => $role->id, |
|
63
|
|
|
'identifier' => $role->identifier, |
|
64
|
|
|
'status' => $role->status, |
|
65
|
|
|
'policies' => $rolePolicies, |
|
66
|
|
|
] |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Builds a RoleDraft domain object from value object returned by persistence |
|
72
|
|
|
* Decorates Role. |
|
73
|
|
|
* |
|
74
|
|
|
* @param \eZ\Publish\SPI\Persistence\User\Role $spiRole |
|
75
|
|
|
* |
|
76
|
|
|
* @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
|
77
|
|
|
*/ |
|
78
|
|
|
public function buildDomainRoleDraftObject(SPIRole $spiRole) |
|
79
|
|
|
{ |
|
80
|
|
|
return new RoleDraft( |
|
81
|
|
|
[ |
|
82
|
|
|
'innerRole' => $this->buildDomainRoleObject($spiRole), |
|
83
|
|
|
] |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Maps provided SPI Policy value object to API Policy value object. |
|
89
|
|
|
* |
|
90
|
|
|
* @param \eZ\Publish\SPI\Persistence\User\Policy $spiPolicy |
|
91
|
|
|
* |
|
92
|
|
|
* @return \eZ\Publish\API\Repository\Values\User\Policy|\eZ\Publish\API\Repository\Values\User\PolicyDraft |
|
93
|
|
|
*/ |
|
94
|
|
|
public function buildDomainPolicyObject(SPIPolicy $spiPolicy) |
|
95
|
|
|
{ |
|
96
|
|
|
$policyLimitations = []; |
|
97
|
|
|
if ($spiPolicy->module !== '*' && $spiPolicy->function !== '*' && $spiPolicy->limitations !== '*') { |
|
98
|
|
|
foreach ($spiPolicy->limitations as $identifier => $values) { |
|
|
|
|
|
|
99
|
|
|
$policyLimitations[] = $this->limitationService->getLimitationType($identifier)->buildValue($values); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$policy = new Policy( |
|
104
|
|
|
[ |
|
105
|
|
|
'id' => $spiPolicy->id, |
|
106
|
|
|
'roleId' => $spiPolicy->roleId, |
|
107
|
|
|
'module' => $spiPolicy->module, |
|
108
|
|
|
'function' => $spiPolicy->function, |
|
109
|
|
|
'limitations' => $policyLimitations, |
|
110
|
|
|
] |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
// Original ID is set on SPI policy, which means that it's a draft. |
|
114
|
|
|
if ($spiPolicy->originalId) { |
|
|
|
|
|
|
115
|
|
|
$policy = new PolicyDraft(['innerPolicy' => $policy, 'originalId' => $spiPolicy->originalId]); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $policy; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Builds the API UserRoleAssignment object from provided SPI RoleAssignment object. |
|
123
|
|
|
* |
|
124
|
|
|
* @param \eZ\Publish\SPI\Persistence\User\RoleAssignment $spiRoleAssignment |
|
125
|
|
|
* @param \eZ\Publish\API\Repository\Values\User\User $user |
|
126
|
|
|
* @param \eZ\Publish\API\Repository\Values\User\Role $role |
|
127
|
|
|
* |
|
128
|
|
|
* @return \eZ\Publish\API\Repository\Values\User\UserRoleAssignment |
|
129
|
|
|
*/ |
|
130
|
|
View Code Duplication |
public function buildDomainUserRoleAssignmentObject(SPIRoleAssignment $spiRoleAssignment, User $user, APIRole $role) |
|
131
|
|
|
{ |
|
132
|
|
|
$limitation = null; |
|
133
|
|
|
if (!empty($spiRoleAssignment->limitationIdentifier)) { |
|
134
|
|
|
$limitation = $this |
|
135
|
|
|
->limitationService |
|
136
|
|
|
->getLimitationType($spiRoleAssignment->limitationIdentifier) |
|
137
|
|
|
->buildValue($spiRoleAssignment->values); |
|
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return new UserRoleAssignment( |
|
141
|
|
|
[ |
|
142
|
|
|
'id' => $spiRoleAssignment->id, |
|
143
|
|
|
'limitation' => $limitation, |
|
144
|
|
|
'role' => $role, |
|
145
|
|
|
'user' => $user, |
|
146
|
|
|
] |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Builds the API UserGroupRoleAssignment object from provided SPI RoleAssignment object. |
|
152
|
|
|
* |
|
153
|
|
|
* @param \eZ\Publish\SPI\Persistence\User\RoleAssignment $spiRoleAssignment |
|
154
|
|
|
* @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
|
155
|
|
|
* @param \eZ\Publish\API\Repository\Values\User\Role $role |
|
156
|
|
|
* |
|
157
|
|
|
* @return \eZ\Publish\API\Repository\Values\User\UserGroupRoleAssignment |
|
158
|
|
|
*/ |
|
159
|
|
View Code Duplication |
public function buildDomainUserGroupRoleAssignmentObject(SPIRoleAssignment $spiRoleAssignment, UserGroup $userGroup, APIRole $role) |
|
160
|
|
|
{ |
|
161
|
|
|
$limitation = null; |
|
162
|
|
|
if (!empty($spiRoleAssignment->limitationIdentifier)) { |
|
163
|
|
|
$limitation = $this |
|
164
|
|
|
->limitationService |
|
165
|
|
|
->getLimitationType($spiRoleAssignment->limitationIdentifier) |
|
166
|
|
|
->buildValue($spiRoleAssignment->values); |
|
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return new UserGroupRoleAssignment( |
|
170
|
|
|
[ |
|
171
|
|
|
'id' => $spiRoleAssignment->id, |
|
172
|
|
|
'limitation' => $limitation, |
|
173
|
|
|
'role' => $role, |
|
174
|
|
|
'userGroup' => $userGroup, |
|
175
|
|
|
] |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Creates SPI Role create struct from provided API role create struct. |
|
181
|
|
|
* |
|
182
|
|
|
* @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $roleCreateStruct |
|
183
|
|
|
* |
|
184
|
|
|
* @return \eZ\Publish\SPI\Persistence\User\RoleCreateStruct |
|
185
|
|
|
*/ |
|
186
|
|
View Code Duplication |
public function buildPersistenceRoleCreateStruct(APIRoleCreateStruct $roleCreateStruct) |
|
187
|
|
|
{ |
|
188
|
|
|
$policiesToCreate = []; |
|
189
|
|
|
foreach ($roleCreateStruct->getPolicies() as $policyCreateStruct) { |
|
190
|
|
|
$policiesToCreate[] = $this->buildPersistencePolicyObject( |
|
191
|
|
|
$policyCreateStruct->module, |
|
192
|
|
|
$policyCreateStruct->function, |
|
193
|
|
|
$policyCreateStruct->getLimitations() |
|
194
|
|
|
); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return new SPIRoleCreateStruct( |
|
198
|
|
|
[ |
|
199
|
|
|
'identifier' => $roleCreateStruct->identifier, |
|
200
|
|
|
'policies' => $policiesToCreate, |
|
201
|
|
|
] |
|
202
|
|
|
); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Creates SPI Role copy struct from provided API role copy struct. |
|
207
|
|
|
* |
|
208
|
|
|
* @param \eZ\Publish\API\Repository\Values\User\RoleCopyStruct $roleCopyStruct |
|
209
|
|
|
* @param mixed $clonedRoleId |
|
|
|
|
|
|
210
|
|
|
* |
|
211
|
|
|
* @return \eZ\Publish\SPI\Persistence\User\RoleCopyStruct |
|
212
|
|
|
*/ |
|
213
|
|
View Code Duplication |
public function buildPersistenceRoleCopyStruct(APIRoleCopyStruct $roleCopyStruct, $clonedId) |
|
214
|
|
|
{ |
|
215
|
|
|
$policiesToCopy = []; |
|
216
|
|
|
foreach ($roleCopyStruct->getPolicies() as $policyCopyStruct) { |
|
217
|
|
|
$policiesToCopy[] = $this->buildPersistencePolicyObject( |
|
218
|
|
|
$policyCopyStruct->module, |
|
219
|
|
|
$policyCopyStruct->function, |
|
220
|
|
|
$policyCopyStruct->getLimitations() |
|
221
|
|
|
); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
return new SPIRoleCopyStruct( |
|
225
|
|
|
[ |
|
226
|
|
|
'clonedId' => $clonedId, |
|
227
|
|
|
'newIdentifier' => $roleCopyStruct->newIdentifier, |
|
228
|
|
|
'policies' => $policiesToCopy, |
|
229
|
|
|
] |
|
230
|
|
|
); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Creates SPI Policy value object from provided module, function and limitations. |
|
235
|
|
|
* |
|
236
|
|
|
* @param string $module |
|
237
|
|
|
* @param string $function |
|
238
|
|
|
* @param \eZ\Publish\API\Repository\Values\User\Limitation[] $limitations |
|
239
|
|
|
* |
|
240
|
|
|
* @return \eZ\Publish\SPI\Persistence\User\Policy |
|
241
|
|
|
*/ |
|
242
|
|
|
public function buildPersistencePolicyObject($module, $function, array $limitations) |
|
243
|
|
|
{ |
|
244
|
|
|
$limitationsToCreate = '*'; |
|
245
|
|
|
if ($module !== '*' && $function !== '*' && !empty($limitations)) { |
|
246
|
|
|
$limitationsToCreate = []; |
|
247
|
|
|
foreach ($limitations as $limitation) { |
|
248
|
|
|
$limitationsToCreate[$limitation->getIdentifier()] = $limitation->limitationValues; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
return new SPIPolicy( |
|
253
|
|
|
[ |
|
254
|
|
|
'module' => $module, |
|
255
|
|
|
'function' => $function, |
|
256
|
|
|
'limitations' => $limitationsToCreate, |
|
257
|
|
|
] |
|
258
|
|
|
); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.