|
@@ 372-418 (lines=47) @@
|
| 369 |
|
* |
| 370 |
|
* @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
| 371 |
|
*/ |
| 372 |
|
public function addPolicyByRoleDraft(APIRoleDraft $roleDraft, APIPolicyCreateStruct $policyCreateStruct) |
| 373 |
|
{ |
| 374 |
|
if (!is_string($policyCreateStruct->module) || empty($policyCreateStruct->module)) { |
| 375 |
|
throw new InvalidArgumentValue('module', $policyCreateStruct->module, 'PolicyCreateStruct'); |
| 376 |
|
} |
| 377 |
|
|
| 378 |
|
if (!is_string($policyCreateStruct->function) || empty($policyCreateStruct->function)) { |
| 379 |
|
throw new InvalidArgumentValue('function', $policyCreateStruct->function, 'PolicyCreateStruct'); |
| 380 |
|
} |
| 381 |
|
|
| 382 |
|
if ($policyCreateStruct->module === '*' && $policyCreateStruct->function !== '*') { |
| 383 |
|
throw new InvalidArgumentValue('module', $policyCreateStruct->module, 'PolicyCreateStruct'); |
| 384 |
|
} |
| 385 |
|
|
| 386 |
|
if ($this->repository->hasAccess('role', 'update') !== true) { |
| 387 |
|
throw new UnauthorizedException('role', 'update'); |
| 388 |
|
} |
| 389 |
|
|
| 390 |
|
$loadedRoleDraft = $this->loadRoleDraft($roleDraft->id); |
| 391 |
|
|
| 392 |
|
$limitations = $policyCreateStruct->getLimitations(); |
| 393 |
|
$limitationValidationErrors = $this->validatePolicy( |
| 394 |
|
$policyCreateStruct->module, |
| 395 |
|
$policyCreateStruct->function, |
| 396 |
|
$limitations |
| 397 |
|
); |
| 398 |
|
if (!empty($limitationValidationErrors)) { |
| 399 |
|
throw new LimitationValidationException($limitationValidationErrors); |
| 400 |
|
} |
| 401 |
|
|
| 402 |
|
$spiPolicy = $this->roleDomainMapper->buildPersistencePolicyObject( |
| 403 |
|
$policyCreateStruct->module, |
| 404 |
|
$policyCreateStruct->function, |
| 405 |
|
$limitations |
| 406 |
|
); |
| 407 |
|
|
| 408 |
|
$this->repository->beginTransaction(); |
| 409 |
|
try { |
| 410 |
|
$this->userHandler->addPolicyByRoleDraft($loadedRoleDraft->id, $spiPolicy); |
| 411 |
|
$this->repository->commit(); |
| 412 |
|
} catch (Exception $e) { |
| 413 |
|
$this->repository->rollback(); |
| 414 |
|
throw $e; |
| 415 |
|
} |
| 416 |
|
|
| 417 |
|
return $this->loadRoleDraft($loadedRoleDraft->id); |
| 418 |
|
} |
| 419 |
|
|
| 420 |
|
/** |
| 421 |
|
* Removes a policy from a RoleDraft. |
|
@@ 656-702 (lines=47) @@
|
| 653 |
|
* |
| 654 |
|
* @return \eZ\Publish\API\Repository\Values\User\Role |
| 655 |
|
*/ |
| 656 |
|
public function addPolicy(APIRole $role, APIPolicyCreateStruct $policyCreateStruct) |
| 657 |
|
{ |
| 658 |
|
if (!is_string($policyCreateStruct->module) || empty($policyCreateStruct->module)) { |
| 659 |
|
throw new InvalidArgumentValue('module', $policyCreateStruct->module, 'PolicyCreateStruct'); |
| 660 |
|
} |
| 661 |
|
|
| 662 |
|
if (!is_string($policyCreateStruct->function) || empty($policyCreateStruct->function)) { |
| 663 |
|
throw new InvalidArgumentValue('function', $policyCreateStruct->function, 'PolicyCreateStruct'); |
| 664 |
|
} |
| 665 |
|
|
| 666 |
|
if ($policyCreateStruct->module === '*' && $policyCreateStruct->function !== '*') { |
| 667 |
|
throw new InvalidArgumentValue('module', $policyCreateStruct->module, 'PolicyCreateStruct'); |
| 668 |
|
} |
| 669 |
|
|
| 670 |
|
if ($this->repository->hasAccess('role', 'update') !== true) { |
| 671 |
|
throw new UnauthorizedException('role', 'update'); |
| 672 |
|
} |
| 673 |
|
|
| 674 |
|
$loadedRole = $this->loadRole($role->id); |
| 675 |
|
|
| 676 |
|
$limitations = $policyCreateStruct->getLimitations(); |
| 677 |
|
$limitationValidationErrors = $this->validatePolicy( |
| 678 |
|
$policyCreateStruct->module, |
| 679 |
|
$policyCreateStruct->function, |
| 680 |
|
$limitations |
| 681 |
|
); |
| 682 |
|
if (!empty($limitationValidationErrors)) { |
| 683 |
|
throw new LimitationValidationException($limitationValidationErrors); |
| 684 |
|
} |
| 685 |
|
|
| 686 |
|
$spiPolicy = $this->roleDomainMapper->buildPersistencePolicyObject( |
| 687 |
|
$policyCreateStruct->module, |
| 688 |
|
$policyCreateStruct->function, |
| 689 |
|
$limitations |
| 690 |
|
); |
| 691 |
|
|
| 692 |
|
$this->repository->beginTransaction(); |
| 693 |
|
try { |
| 694 |
|
$this->userHandler->addPolicy($loadedRole->id, $spiPolicy); |
| 695 |
|
$this->repository->commit(); |
| 696 |
|
} catch (Exception $e) { |
| 697 |
|
$this->repository->rollback(); |
| 698 |
|
throw $e; |
| 699 |
|
} |
| 700 |
|
|
| 701 |
|
return $this->loadRole($loadedRole->id); |
| 702 |
|
} |
| 703 |
|
|
| 704 |
|
/** |
| 705 |
|
* Removes a policy from the role. |