public function processUpdatePolicy(FormActionEvent $event) { // Don't update anything if we just want to cancel the draft. if ($event->getClickedButton() === 'removeDraft') { return; } /** @var \EzSystems\RepositoryForms\Data\Role\PolicyCreateData|\EzSystems\RepositoryForms\Data\Role\PolicyUpdateData $data */ $data = $event->getData(); if ($data->isNew() && $data->moduleFunction) { list($module, $function) = explode('|', $data->moduleFunction); $data->module = $module; $data->function = $function; $initialRoleDraft = $data->roleDraft; $updatedRoleDraft = $this->roleService->addPolicyByRoleDraft($initialRoleDraft, $data); $initialPoliciesById = $this->getPoliciesById($initialRoleDraft); $updatedPoliciesById = $this->getPoliciesById($updatedRoleDraft); foreach ($updatedPoliciesById as $policyId => $policyDraft) { if (!isset($initialPoliciesById[$policyId])) { $data->setPolicyDraft($policyDraft); break; } } } else { // Only save limitations on update. // It is not possible by design to update policy module/function. foreach ($data->limitationsData as $limitation) { // Add posted limitations as valid ones, recognized by RoleService. if (!empty($limitation->limitationValues)) { $data->addLimitation($limitation); } } $this->roleService->updatePolicyByRoleDraft($data->roleDraft, $data->policyDraft, $data); } }
/** * Adds a policy to a role draft. * * @since 6.2 * @deprecated since 6.3, use {@see addPolicy} * * @param $roleId * * @return \eZ\Publish\Core\REST\Server\Values\CreatedPolicy */ public function addPolicyByRoleDraft($roleId, Request $request) { $createStruct = $this->inputDispatcher->parse(new Message(array('Content-Type' => $request->headers->get('Content-Type')), $request->getContent())); try { $role = $this->roleService->addPolicyByRoleDraft($this->roleService->loadRoleDraft($roleId), $createStruct); } catch (LimitationValidationException $e) { throw new BadRequestException($e->getMessage()); } return new Values\CreatedPolicy(array('policy' => $this->getLastAddedPolicy($role))); }
/** * Adds a new policy to the role draft. * * @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(RoleDraft $roleDraft, PolicyCreateStruct $policyCreateStruct) { $returnValue = $this->service->addPolicyByRoleDraft($roleDraft, $policyCreateStruct); $this->signalDispatcher->emit(new AddPolicyByRoleDraftSignal(array('roleId' => $roleDraft->id, 'policyId' => $returnValue->id))); return $returnValue; }