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