/** * Adds a new policy to the RoleDraft. * * @since 6.0 * * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to add a policy * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if limitation of the same type is repeated in policy create * struct or if limitation is not allowed on module/function * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a limitation in the $policyCreateStruct is not valid * * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft * @param \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStruct * * @return \eZ\Publish\API\Repository\Values\User\RoleDraft */ public function addPolicyByRoleDraft(APIRoleDraft $roleDraft, APIPolicyCreateStruct $policyCreateStruct) { if (!is_string($policyCreateStruct->module) || empty($policyCreateStruct->module)) { throw new InvalidArgumentValue('module', $policyCreateStruct->module, 'PolicyCreateStruct'); } if (!is_string($policyCreateStruct->function) || empty($policyCreateStruct->function)) { throw new InvalidArgumentValue('function', $policyCreateStruct->function, 'PolicyCreateStruct'); } if ($policyCreateStruct->module === '*' && $policyCreateStruct->function !== '*') { throw new InvalidArgumentValue('module', $policyCreateStruct->module, 'PolicyCreateStruct'); } if ($this->repository->hasAccess('role', 'update') !== true) { throw new UnauthorizedException('role', 'update'); } $loadedRoleDraft = $this->loadRoleDraft($roleDraft->id); $limitations = $policyCreateStruct->getLimitations(); $limitationValidationErrors = $this->validatePolicy($policyCreateStruct->module, $policyCreateStruct->function, $limitations); if (!empty($limitationValidationErrors)) { throw new LimitationValidationException($limitationValidationErrors); } $spiPolicy = $this->roleDomainMapper->buildPersistencePolicyObject($policyCreateStruct->module, $policyCreateStruct->function, $limitations); $this->repository->beginTransaction(); try { $this->userHandler->addPolicyByRoleDraft($loadedRoleDraft->id, $spiPolicy); $this->repository->commit(); } catch (Exception $e) { $this->repository->rollback(); throw $e; } return $this->loadRoleDraft($loadedRoleDraft->id); }