Complex classes like RoleService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RoleService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class RoleService implements RoleServiceInterface |
||
44 | { |
||
45 | /** @var \eZ\Publish\API\Repository\Repository */ |
||
46 | protected $repository; |
||
47 | |||
48 | /** @var \eZ\Publish\SPI\Persistence\User\Handler */ |
||
49 | protected $userHandler; |
||
50 | |||
51 | /** @var \eZ\Publish\Core\Repository\Permission\LimitationService */ |
||
52 | protected $limitationService; |
||
53 | |||
54 | /** @var \eZ\Publish\Core\Repository\Helper\RoleDomainMapper */ |
||
55 | protected $roleDomainMapper; |
||
56 | |||
57 | /** @var array */ |
||
58 | protected $settings; |
||
59 | |||
60 | /** @var \eZ\Publish\API\Repository\PermissionResolver */ |
||
61 | private $permissionResolver; |
||
62 | |||
63 | /** |
||
64 | * Setups service with reference to repository object that created it & corresponding handler. |
||
65 | * |
||
66 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
67 | * @param \eZ\Publish\SPI\Persistence\User\Handler $userHandler |
||
68 | * @param \eZ\Publish\Core\Repository\Permission\LimitationService $limitationService |
||
69 | * @param \eZ\Publish\Core\Repository\Helper\RoleDomainMapper $roleDomainMapper |
||
70 | * @param array $settings |
||
71 | */ |
||
72 | public function __construct( |
||
73 | RepositoryInterface $repository, |
||
74 | Handler $userHandler, |
||
75 | Permission\LimitationService $limitationService, |
||
76 | Helper\RoleDomainMapper $roleDomainMapper, |
||
77 | array $settings = [] |
||
78 | ) { |
||
79 | $this->repository = $repository; |
||
80 | $this->userHandler = $userHandler; |
||
81 | $this->limitationService = $limitationService; |
||
82 | $this->roleDomainMapper = $roleDomainMapper; |
||
83 | $this->settings = $settings; |
||
84 | $this->permissionResolver = $repository->getPermissionResolver(); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Creates a new RoleDraft. |
||
89 | * |
||
90 | * @since 6.0 |
||
91 | * |
||
92 | * @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $roleCreateStruct |
||
93 | * |
||
94 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
95 | * |
||
96 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
97 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the name of the role already exists or if limitation of the same type |
||
98 | * is repeated in the policy create struct or if limitation is not allowed on module/function |
||
99 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a RoleDraft |
||
100 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a policy limitation in the $roleCreateStruct is not valid |
||
101 | */ |
||
102 | public function createRole(APIRoleCreateStruct $roleCreateStruct): APIRoleDraft |
||
103 | { |
||
104 | if (!is_string($roleCreateStruct->identifier) || empty($roleCreateStruct->identifier)) { |
||
105 | throw new InvalidArgumentValue('identifier', $roleCreateStruct->identifier, 'RoleCreateStruct'); |
||
106 | } |
||
107 | |||
108 | if (!$this->permissionResolver->canUser('role', 'create', $roleCreateStruct)) { |
||
109 | throw new UnauthorizedException('role', 'create'); |
||
110 | } |
||
111 | |||
112 | try { |
||
113 | $existingRole = $this->loadRoleByIdentifier($roleCreateStruct->identifier); |
||
114 | |||
115 | throw new InvalidArgumentException( |
||
116 | '$roleCreateStruct', |
||
117 | "A Role '{$existingRole->id}' with identifier '{$roleCreateStruct->identifier}' " . |
||
118 | 'already exists' |
||
119 | ); |
||
120 | } catch (APINotFoundException $e) { |
||
121 | // Do nothing |
||
122 | } |
||
123 | |||
124 | $limitationValidationErrors = $this->validateRoleCreateStruct($roleCreateStruct); |
||
125 | if (!empty($limitationValidationErrors)) { |
||
126 | throw new LimitationValidationException($limitationValidationErrors); |
||
127 | } |
||
128 | |||
129 | $spiRoleCreateStruct = $this->roleDomainMapper->buildPersistenceRoleCreateStruct($roleCreateStruct); |
||
130 | |||
131 | $this->repository->beginTransaction(); |
||
132 | try { |
||
133 | $spiRole = $this->userHandler->createRole($spiRoleCreateStruct); |
||
134 | $this->repository->commit(); |
||
135 | } catch (Exception $e) { |
||
136 | $this->repository->rollback(); |
||
137 | throw $e; |
||
138 | } |
||
139 | |||
140 | return $this->roleDomainMapper->buildDomainRoleDraftObject($spiRole); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Creates a new RoleDraft for an existing Role. |
||
145 | * |
||
146 | * @since 6.0 |
||
147 | * |
||
148 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
149 | * |
||
150 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
151 | * |
||
152 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
153 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the Role already has a RoleDraft that will need to be removed first |
||
154 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a RoleDraft |
||
155 | */ |
||
156 | public function createRoleDraft(APIRole $role): APIRoleDraft |
||
183 | |||
184 | /** |
||
185 | * Loads a RoleDraft for the given id. |
||
186 | * |
||
187 | * @since 6.0 |
||
188 | * |
||
189 | * @param int $id |
||
190 | * |
||
191 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
192 | * |
||
193 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
194 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
195 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a RoleDraft with the given id was not found |
||
196 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a RoleDraft |
||
197 | */ |
||
198 | public function loadRoleDraft(int $id): APIRoleDraft |
||
199 | { |
||
200 | $spiRole = $this->userHandler->loadRole($id, Role::STATUS_DRAFT); |
||
201 | |||
202 | $role = $this->roleDomainMapper->buildDomainRoleDraftObject($spiRole); |
||
203 | |||
204 | if (!$this->permissionResolver->canUser('role', 'read', $role)) { |
||
205 | throw new UnauthorizedException('role', 'read'); |
||
206 | } |
||
207 | |||
208 | return $role; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Loads a RoleDraft by the ID of the role it was created from. |
||
213 | * |
||
214 | * @param int $roleId ID of the role the draft was created from. |
||
215 | * |
||
216 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
217 | * |
||
218 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
219 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
220 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a RoleDraft with the given id was not found |
||
221 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this role |
||
222 | */ |
||
223 | public function loadRoleDraftByRoleId(int $roleId): APIRoleDraft |
||
224 | { |
||
225 | $spiRole = $this->userHandler->loadRoleDraftByRoleId($roleId); |
||
226 | |||
227 | $role = $this->roleDomainMapper->buildDomainRoleDraftObject($spiRole); |
||
228 | |||
229 | if (!$this->permissionResolver->canUser('role', 'read', $role)) { |
||
230 | throw new UnauthorizedException('role', 'read'); |
||
231 | } |
||
232 | |||
233 | return $role; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Updates the properties of a RoleDraft. |
||
238 | * |
||
239 | * @since 6.0 |
||
240 | * |
||
241 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
242 | * @param \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct $roleUpdateStruct |
||
243 | * |
||
244 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
245 | * |
||
246 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
247 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the identifier of the RoleDraft already exists |
||
248 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
249 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update a RoleDraft |
||
250 | */ |
||
251 | public function updateRoleDraft(APIRoleDraft $roleDraft, RoleUpdateStruct $roleUpdateStruct): APIRoleDraft |
||
252 | { |
||
253 | if ($roleUpdateStruct->identifier !== null && !is_string($roleUpdateStruct->identifier)) { |
||
254 | throw new InvalidArgumentValue('identifier', $roleUpdateStruct->identifier, 'RoleUpdateStruct'); |
||
255 | } |
||
256 | |||
257 | $loadedRoleDraft = $this->loadRoleDraft($roleDraft->id); |
||
258 | |||
259 | if (!$this->permissionResolver->canUser('role', 'update', $loadedRoleDraft)) { |
||
260 | throw new UnauthorizedException('role', 'update'); |
||
261 | } |
||
262 | |||
263 | if ($roleUpdateStruct->identifier !== null) { |
||
264 | try { |
||
265 | /* Throw exception if: |
||
266 | * - A published role with the same identifier exists, AND |
||
267 | * - The ID of the published role does not match the original ID of the draft |
||
268 | */ |
||
269 | $existingSPIRole = $this->userHandler->loadRoleByIdentifier($roleUpdateStruct->identifier); |
||
270 | $SPIRoleDraft = $this->userHandler->loadRole($loadedRoleDraft->id, Role::STATUS_DRAFT); |
||
271 | if ($existingSPIRole->id != $SPIRoleDraft->originalId) { |
||
272 | throw new InvalidArgumentException( |
||
273 | '$roleUpdateStruct', |
||
274 | "A Role '{$existingSPIRole->id}' with identifier '{$roleUpdateStruct->identifier}' " . |
||
275 | 'already exists' |
||
276 | ); |
||
277 | } |
||
278 | } catch (APINotFoundException $e) { |
||
279 | // Do nothing |
||
280 | } |
||
281 | } |
||
282 | |||
283 | $this->repository->beginTransaction(); |
||
284 | try { |
||
285 | $this->userHandler->updateRole( |
||
286 | new SPIRoleUpdateStruct( |
||
287 | [ |
||
288 | 'id' => $loadedRoleDraft->id, |
||
289 | 'identifier' => $roleUpdateStruct->identifier ?: $loadedRoleDraft->identifier, |
||
290 | ] |
||
291 | ) |
||
292 | ); |
||
293 | $this->repository->commit(); |
||
294 | } catch (Exception $e) { |
||
295 | $this->repository->rollback(); |
||
296 | throw $e; |
||
297 | } |
||
298 | |||
299 | return $this->loadRoleDraft($loadedRoleDraft->id); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Adds a new policy to the RoleDraft. |
||
304 | * |
||
305 | * @since 6.0 |
||
306 | * |
||
307 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
308 | * @param \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStruct |
||
309 | * |
||
310 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
311 | * |
||
312 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
313 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if limitation of the same type is repeated in policy create |
||
314 | * struct or if limitation is not allowed on module/function |
||
315 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
316 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a limitation in the $policyCreateStruct is not valid |
||
317 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to add a policy |
||
318 | */ |
||
319 | public function addPolicyByRoleDraft(APIRoleDraft $roleDraft, APIPolicyCreateStruct $policyCreateStruct): APIRoleDraft |
||
320 | { |
||
321 | if (!is_string($policyCreateStruct->module) || empty($policyCreateStruct->module)) { |
||
322 | throw new InvalidArgumentValue('module', $policyCreateStruct->module, 'PolicyCreateStruct'); |
||
323 | } |
||
324 | |||
325 | if (!is_string($policyCreateStruct->function) || empty($policyCreateStruct->function)) { |
||
326 | throw new InvalidArgumentValue('function', $policyCreateStruct->function, 'PolicyCreateStruct'); |
||
327 | } |
||
328 | |||
329 | if ($policyCreateStruct->module === '*' && $policyCreateStruct->function !== '*') { |
||
330 | throw new InvalidArgumentValue('module', $policyCreateStruct->module, 'PolicyCreateStruct'); |
||
331 | } |
||
332 | |||
333 | if (!$this->permissionResolver->canUser('role', 'update', $roleDraft)) { |
||
334 | throw new UnauthorizedException('role', 'update'); |
||
335 | } |
||
336 | |||
337 | $loadedRoleDraft = $this->loadRoleDraft($roleDraft->id); |
||
338 | |||
339 | $limitations = $policyCreateStruct->getLimitations(); |
||
340 | $limitationValidationErrors = $this->validatePolicy( |
||
341 | $policyCreateStruct->module, |
||
342 | $policyCreateStruct->function, |
||
343 | $limitations |
||
344 | ); |
||
345 | if (!empty($limitationValidationErrors)) { |
||
346 | throw new LimitationValidationException($limitationValidationErrors); |
||
347 | } |
||
348 | |||
349 | $spiPolicy = $this->roleDomainMapper->buildPersistencePolicyObject( |
||
350 | $policyCreateStruct->module, |
||
351 | $policyCreateStruct->function, |
||
352 | $limitations |
||
353 | ); |
||
354 | |||
355 | $this->repository->beginTransaction(); |
||
356 | try { |
||
357 | $this->userHandler->addPolicyByRoleDraft($loadedRoleDraft->id, $spiPolicy); |
||
358 | $this->repository->commit(); |
||
359 | } catch (Exception $e) { |
||
360 | $this->repository->rollback(); |
||
361 | throw $e; |
||
362 | } |
||
363 | |||
364 | return $this->loadRoleDraft($loadedRoleDraft->id); |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Removes a policy from a RoleDraft. |
||
369 | * |
||
370 | * @since 6.0 |
||
371 | * |
||
372 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
373 | * @param \eZ\Publish\API\Repository\Values\User\PolicyDraft $policyDraft the policy to remove from the RoleDraft |
||
374 | * |
||
375 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
376 | * |
||
377 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
378 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if policy does not belong to the given RoleDraft |
||
379 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
380 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a policy |
||
381 | */ |
||
382 | public function removePolicyByRoleDraft(APIRoleDraft $roleDraft, PolicyDraft $policyDraft): APIRoleDraft |
||
383 | { |
||
384 | if (!$this->permissionResolver->canUser('role', 'update', $roleDraft)) { |
||
385 | throw new UnauthorizedException('role', 'update'); |
||
386 | } |
||
387 | |||
388 | if ($policyDraft->roleId != $roleDraft->id) { |
||
389 | throw new InvalidArgumentException('$policy', 'The Policy does not belong to the given Role'); |
||
390 | } |
||
391 | |||
392 | $this->internalDeletePolicy($policyDraft); |
||
393 | |||
394 | return $this->loadRoleDraft($roleDraft->id); |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Updates the limitations of a policy. The module and function cannot be changed and |
||
399 | * the limitations are replaced by the ones in $roleUpdateStruct. |
||
400 | * |
||
401 | * @since 6.0 |
||
402 | * |
||
403 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
404 | * @param \eZ\Publish\API\Repository\Values\User\PolicyDraft $policy |
||
405 | * @param \eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct $policyUpdateStruct |
||
406 | * |
||
407 | * @return \eZ\Publish\API\Repository\Values\User\PolicyDraft |
||
408 | * |
||
409 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
410 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if limitation of the same type is repeated in policy update |
||
411 | * struct or if limitation is not allowed on module/function |
||
412 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a limitation in the $policyUpdateStruct is not valid |
||
413 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update a policy |
||
414 | */ |
||
415 | public function updatePolicyByRoleDraft( |
||
416 | APIRoleDraft $roleDraft, |
||
417 | PolicyDraft $policy, |
||
418 | APIPolicyUpdateStruct $policyUpdateStruct |
||
419 | ): PolicyDraft { |
||
420 | if (!is_string($policy->module)) { |
||
421 | throw new InvalidArgumentValue('module', $policy->module, 'Policy'); |
||
422 | } |
||
423 | |||
424 | if (!is_string($policy->function)) { |
||
425 | throw new InvalidArgumentValue('function', $policy->function, 'Policy'); |
||
426 | } |
||
427 | |||
428 | if (!$this->permissionResolver->canUser('role', 'update', $roleDraft)) { |
||
429 | throw new UnauthorizedException('role', 'update'); |
||
430 | } |
||
431 | |||
432 | if ($policy->roleId !== $roleDraft->id) { |
||
433 | throw new InvalidArgumentException('$policy', 'does not belong to the provided role draft'); |
||
434 | } |
||
435 | |||
436 | $limitations = $policyUpdateStruct->getLimitations(); |
||
437 | $limitationValidationErrors = $this->validatePolicy( |
||
438 | $policy->module, |
||
439 | $policy->function, |
||
440 | $limitations |
||
441 | ); |
||
442 | if (!empty($limitationValidationErrors)) { |
||
443 | throw new LimitationValidationException($limitationValidationErrors); |
||
444 | } |
||
445 | |||
446 | $spiPolicy = $this->roleDomainMapper->buildPersistencePolicyObject( |
||
447 | $policy->module, |
||
448 | $policy->function, |
||
449 | $limitations |
||
450 | ); |
||
451 | $spiPolicy->id = $policy->id; |
||
452 | $spiPolicy->roleId = $policy->roleId; |
||
453 | $spiPolicy->originalId = $policy->originalId; |
||
454 | |||
455 | $this->repository->beginTransaction(); |
||
456 | try { |
||
457 | $this->userHandler->updatePolicy($spiPolicy); |
||
458 | $this->repository->commit(); |
||
459 | } catch (Exception $e) { |
||
460 | $this->repository->rollback(); |
||
461 | throw $e; |
||
462 | } |
||
463 | |||
464 | return $this->roleDomainMapper->buildDomainPolicyObject($spiPolicy); |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Deletes the given RoleDraft. |
||
469 | * |
||
470 | * @since 6.0 |
||
471 | * |
||
472 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
473 | * |
||
474 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
475 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
476 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
477 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to delete this RoleDraft |
||
478 | */ |
||
479 | public function deleteRoleDraft(APIRoleDraft $roleDraft): void |
||
480 | { |
||
481 | $loadedRoleDraft = $this->loadRoleDraft($roleDraft->id); |
||
482 | |||
483 | $this->repository->beginTransaction(); |
||
484 | try { |
||
485 | $this->userHandler->deleteRole($loadedRoleDraft->id, Role::STATUS_DRAFT); |
||
486 | $this->repository->commit(); |
||
487 | } catch (Exception $e) { |
||
488 | $this->repository->rollback(); |
||
489 | throw $e; |
||
490 | } |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * Publishes a given RoleDraft. |
||
495 | * |
||
496 | * @since 6.0 |
||
497 | * |
||
498 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
499 | * |
||
500 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the role draft cannot be loaded |
||
501 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
502 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to publish this RoleDraft |
||
503 | */ |
||
504 | public function publishRoleDraft(APIRoleDraft $roleDraft): void |
||
505 | { |
||
506 | if (!$this->permissionResolver->canUser('role', 'update', $roleDraft)) { |
||
507 | throw new UnauthorizedException('role', 'update'); |
||
508 | } |
||
509 | |||
510 | try { |
||
511 | $loadedRoleDraft = $this->loadRoleDraft($roleDraft->id); |
||
512 | } catch (APINotFoundException $e) { |
||
513 | throw new BadStateException( |
||
514 | '$roleDraft', |
||
515 | 'The Role does not have a draft.', |
||
516 | $e |
||
517 | ); |
||
518 | } |
||
519 | |||
520 | $this->repository->beginTransaction(); |
||
521 | try { |
||
522 | $this->userHandler->publishRoleDraft($loadedRoleDraft->id); |
||
523 | $this->repository->commit(); |
||
524 | } catch (Exception $e) { |
||
525 | $this->repository->rollback(); |
||
526 | throw $e; |
||
527 | } |
||
528 | } |
||
529 | |||
530 | /** |
||
531 | * Loads a role for the given id. |
||
532 | * |
||
533 | * @param int $id |
||
534 | * |
||
535 | * @return \eZ\Publish\Core\Repository\Values\User\Role |
||
536 | * |
||
537 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
538 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
539 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a role with the given id was not found |
||
540 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this role |
||
541 | */ |
||
542 | public function loadRole(int $id): APIRole |
||
543 | { |
||
544 | $spiRole = $this->userHandler->loadRole($id); |
||
545 | |||
546 | $role = $this->roleDomainMapper->buildDomainRoleObject($spiRole); |
||
547 | |||
548 | if (!$this->permissionResolver->canUser('role', 'read', $role)) { |
||
549 | throw new UnauthorizedException('role', 'read'); |
||
550 | } |
||
551 | |||
552 | return $role; |
||
553 | } |
||
554 | |||
555 | /** |
||
556 | * Loads a role for the given identifier. |
||
557 | * |
||
558 | * @param string $identifier |
||
559 | * |
||
560 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
561 | * |
||
562 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
563 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
564 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a role with the given name was not found |
||
565 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this role |
||
566 | */ |
||
567 | public function loadRoleByIdentifier(string $identifier): APIRole |
||
568 | { |
||
569 | $spiRole = $this->userHandler->loadRoleByIdentifier($identifier); |
||
570 | |||
571 | $role = $this->roleDomainMapper->buildDomainRoleObject($spiRole); |
||
572 | |||
573 | if (!$this->permissionResolver->canUser('role', 'read', $role)) { |
||
574 | throw new UnauthorizedException('role', 'read'); |
||
575 | } |
||
576 | |||
577 | return $role; |
||
578 | } |
||
579 | |||
580 | /** |
||
581 | * Loads all roles, excluding the ones the current user is not allowed to read. |
||
582 | * |
||
583 | * @return \eZ\Publish\API\Repository\Values\User\Role[] |
||
584 | */ |
||
585 | public function loadRoles(): iterable |
||
586 | { |
||
587 | $roles = array_map( |
||
588 | function ($spiRole) { |
||
589 | return $this->roleDomainMapper->buildDomainRoleObject($spiRole); |
||
590 | }, |
||
591 | $this->userHandler->loadRoles() |
||
592 | ); |
||
593 | |||
594 | return array_values( |
||
595 | array_filter( |
||
596 | $roles, |
||
597 | function ($role) { |
||
598 | return $this->permissionResolver->canUser('role', 'read', $role); |
||
599 | } |
||
600 | ) |
||
601 | ); |
||
602 | } |
||
603 | |||
604 | /** |
||
605 | * Deletes the given role. |
||
606 | * |
||
607 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
608 | * |
||
609 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
610 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
611 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
612 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to delete this role |
||
613 | */ |
||
614 | public function deleteRole(APIRole $role): void |
||
631 | |||
632 | /** |
||
633 | * Assigns a role to the given user group. |
||
634 | * |
||
635 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
636 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
637 | * @param \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $roleLimitation an optional role limitation (which is either a subtree limitation or section limitation) |
||
638 | * |
||
639 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
640 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If assignment already exists |
||
641 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
642 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to assign a role |
||
643 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if $roleLimitation is not valid |
||
644 | */ |
||
645 | public function assignRoleToUserGroup(APIRole $role, UserGroup $userGroup, RoleLimitation $roleLimitation = null): void |
||
681 | |||
682 | /** |
||
683 | * Assigns a role to the given user. |
||
684 | * |
||
685 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
686 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
687 | * @param \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $roleLimitation an optional role limitation (which is either a subtree limitation or section limitation) |
||
688 | * |
||
689 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
690 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If assignment already exists |
||
691 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
692 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if $roleLimitation is not valid |
||
693 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to assign a role |
||
694 | */ |
||
695 | public function assignRoleToUser(APIRole $role, User $user, RoleLimitation $roleLimitation = null): void |
||
731 | |||
732 | /** |
||
733 | * Removes the given role assignment. |
||
734 | * |
||
735 | * @param \eZ\Publish\API\Repository\Values\User\RoleAssignment $roleAssignment |
||
736 | * |
||
737 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
738 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
739 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
740 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a role assignment |
||
741 | */ |
||
742 | public function removeRoleAssignment(RoleAssignment $roleAssignment): void |
||
759 | |||
760 | /** |
||
761 | * Loads a role assignment for the given id. |
||
762 | * |
||
763 | * @param int $roleAssignmentId |
||
764 | * |
||
765 | * @return \eZ\Publish\API\Repository\Values\User\RoleAssignment |
||
766 | * |
||
767 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
768 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
769 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the role assignment was not found |
||
770 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this role |
||
771 | */ |
||
772 | public function loadRoleAssignment(int $roleAssignmentId): RoleAssignment |
||
808 | |||
809 | /** |
||
810 | * Returns the assigned user and user groups to this role. |
||
811 | * |
||
812 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
813 | * |
||
814 | * @return \eZ\Publish\API\Repository\Values\User\RoleAssignment[] |
||
815 | * |
||
816 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
817 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
818 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read a role |
||
819 | */ |
||
820 | public function getRoleAssignments(APIRole $role): iterable |
||
856 | |||
857 | /** |
||
858 | * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser() |
||
859 | * |
||
860 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
861 | * @param bool $inherited |
||
862 | * |
||
863 | * @return iterable |
||
864 | * |
||
865 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
866 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
867 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
868 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
869 | */ |
||
870 | public function getRoleAssignmentsForUser(User $user, bool $inherited = false): iterable |
||
899 | |||
900 | /** |
||
901 | * Returns the roles assigned to the given user group, excluding the ones the current user is not allowed to read. |
||
902 | * |
||
903 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
904 | * |
||
905 | * @return \eZ\Publish\API\Repository\Values\User\UserGroupRoleAssignment[] |
||
906 | * |
||
907 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
908 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
909 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
910 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
911 | */ |
||
912 | public function getRoleAssignmentsForUserGroup(UserGroup $userGroup): iterable |
||
930 | |||
931 | /** |
||
932 | * Instantiates a role create class. |
||
933 | * |
||
934 | * @param string $name |
||
935 | * |
||
936 | * @return \eZ\Publish\API\Repository\Values\User\RoleCreateStruct |
||
937 | */ |
||
938 | public function newRoleCreateStruct(string $name): APIRoleCreateStruct |
||
947 | |||
948 | /** |
||
949 | * Instantiates a policy create class. |
||
950 | * |
||
951 | * @param string $module |
||
952 | * @param string $function |
||
953 | * |
||
954 | * @return \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct |
||
955 | */ |
||
956 | public function newPolicyCreateStruct(string $module, string $function): APIPolicyCreateStruct |
||
966 | |||
967 | /** |
||
968 | * Instantiates a policy update class. |
||
969 | * |
||
970 | * @return \eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct |
||
971 | */ |
||
972 | public function newPolicyUpdateStruct(): APIPolicyUpdateStruct |
||
980 | |||
981 | /** |
||
982 | * Instantiates a policy update class. |
||
983 | * |
||
984 | * @return \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct |
||
985 | */ |
||
986 | public function newRoleUpdateStruct(): RoleUpdateStruct |
||
990 | |||
991 | /** |
||
992 | * Returns the LimitationType registered with the given identifier. |
||
993 | * |
||
994 | * Returns the correct implementation of API Limitation value object |
||
995 | * based on provided identifier |
||
996 | * |
||
997 | * @param string $identifier |
||
998 | * |
||
999 | * @return \eZ\Publish\SPI\Limitation\Type |
||
1000 | * |
||
1001 | * @throws \RuntimeException if there is no LimitationType with $identifier |
||
1002 | */ |
||
1003 | public function getLimitationType(string $identifier): Type |
||
1007 | |||
1008 | /** |
||
1009 | * Returns the LimitationType's assigned to a given module/function. |
||
1010 | * |
||
1011 | * Typically used for: |
||
1012 | * - Internal validation limitation value use on Policies |
||
1013 | * - Role admin gui for editing policy limitations incl list limitation options via valueSchema() |
||
1014 | * |
||
1015 | * @param string $module Legacy name of "controller", it's a unique identifier like "content" |
||
1016 | * @param string $function Legacy name of a controller "action", it's a unique within the controller like "read" |
||
1017 | * |
||
1018 | * @return \eZ\Publish\SPI\Limitation\Type[] |
||
1019 | * |
||
1020 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If module/function to limitation type mapping |
||
1021 | * refers to a non existing identifier. |
||
1022 | */ |
||
1023 | public function getLimitationTypesByModuleFunction(string $module, string $function): iterable |
||
1044 | |||
1045 | /** |
||
1046 | * Validates Policies and Limitations in Role create struct. |
||
1047 | * |
||
1048 | * @uses ::validatePolicy() |
||
1049 | * |
||
1050 | * @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $roleCreateStruct |
||
1051 | * |
||
1052 | * @return \eZ\Publish\Core\FieldType\ValidationError[][][] |
||
1053 | * |
||
1054 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
1055 | */ |
||
1056 | protected function validateRoleCreateStruct(APIRoleCreateStruct $roleCreateStruct): iterable |
||
1073 | |||
1074 | /** |
||
1075 | * Validates Policy context: Limitations on a module and function. |
||
1076 | * |
||
1077 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the same limitation is repeated or if |
||
1078 | * limitation is not allowed on module/function |
||
1079 | * |
||
1080 | * @param string $module |
||
1081 | * @param string $function |
||
1082 | * @param \eZ\Publish\API\Repository\Values\User\Limitation[] $limitations |
||
1083 | * |
||
1084 | * @return \eZ\Publish\Core\FieldType\ValidationError[][] |
||
1085 | */ |
||
1086 | protected function validatePolicy(string $module, string $function, array $limitations): iterable |
||
1111 | |||
1112 | /** |
||
1113 | * Validate that assignments not already exists and filter validations against existing. |
||
1114 | * |
||
1115 | * @param int $contentId |
||
1116 | * @param \eZ\Publish\SPI\Persistence\User\Role $spiRole |
||
1117 | * @param array|null $limitation |
||
1118 | * |
||
1119 | * @return array[]|null Filtered version of $limitation |
||
1120 | * |
||
1121 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If assignment already exists |
||
1122 | */ |
||
1123 | protected function checkAssignmentAndFilterLimitationValues( |
||
1168 | |||
1169 | /** |
||
1170 | * Deletes a policy. |
||
1171 | * |
||
1172 | * Used by {@link removePolicy()} and {@link deletePolicy()} |
||
1173 | * |
||
1174 | * @param \eZ\Publish\API\Repository\Values\User\Policy $policy |
||
1175 | * |
||
1176 | * @throws \Exception |
||
1177 | */ |
||
1178 | protected function internalDeletePolicy(APIPolicy $policy): void |
||
1189 | } |
||
1190 |