public function deletePolicyAction(Request $request, $roleId, $policyId)
 {
     $role = $this->roleService->loadRole($roleId);
     $deleteForm = $this->createForm(new PolicyDeleteType(), ['policyId' => $policyId, 'roleId' => $roleId]);
     $deleteForm->handleRequest($request);
     if ($deleteForm->isValid()) {
         try {
             $roleDraft = $this->roleService->loadRoleDraftByRoleId($roleId);
         } catch (NotFoundException $e) {
             // The draft doesn't exist, let's create one
             $roleDraft = $this->roleService->createRoleDraft($role);
         }
         $foundPolicy = false;
         /** @var PolicyDraft $policy */
         foreach ($roleDraft->getPolicies() as $policy) {
             if ($policy->originalId == $policyId) {
                 $foundPolicy = true;
                 break;
             }
         }
         if (!$foundPolicy) {
             throw new BadRequestHttpException($this->translator->trans('role.error.policy_not_found', ['%policyId%' => $policyId, '%roleId%' => $roleId], 'role'));
         }
         $this->roleService->removePolicyByRoleDraft($roleDraft, $policy);
         $this->roleService->publishRoleDraft($roleDraft);
         $this->notify('role.policy.deleted', ['%roleIdentifier%' => $role->identifier, '%policyId%' => $policyId], 'role');
     } elseif ($deleteForm->isSubmitted()) {
         $this->notifyError('role.policy.error.delete', ['%roleIdentifier%' => $role->identifier, '%policyId%' => $policyId], 'role');
     }
     return $this->redirectToRouteAfterFormPost('admin_roleView', ['roleId' => $roleId]);
 }
 /**
  * Updates a role.
  *
  * @param int $roleId Role ID
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function updateRoleAction(Request $request, $roleId)
 {
     try {
         $roleDraft = $this->roleService->loadRoleDraft($roleId);
     } catch (NotFoundException $e) {
         // The draft doesn't exist, let's create one
         $role = $this->roleService->loadRole($roleId);
         $roleDraft = $this->roleService->createRoleDraft($role);
     }
     $roleData = (new RoleMapper())->mapToFormData($roleDraft);
     $form = $this->createForm(new RoleUpdateType(), $roleData);
     $actionUrl = $this->generateUrl('admin_roleUpdate', ['roleId' => $roleId]);
     // Synchronize form and data.
     $form->handleRequest($request);
     $hasErrors = false;
     if ($form->isValid()) {
         $this->actionDispatcher->dispatchFormAction($form, $roleData, $form->getClickedButton()->getName());
         if ($response = $this->actionDispatcher->getResponse()) {
             return $response;
         }
         return $this->redirectAfterFormPost($actionUrl);
     } elseif ($form->isSubmitted()) {
         $hasErrors = true;
     }
     $formView = $form->createView();
     // Show empty text input when name is not set, while showing "New role" in the page title
     $roleName = $roleDraft->identifier;
     if ($roleData->isNew()) {
         $roleName = 'role.name_new';
         $formView->vars['role_input_value'] = '';
     } else {
         $formView->vars['role_input_value'] = $roleName;
     }
     return $this->render('eZPlatformUIBundle:Role:update_role.html.twig', ['form' => $formView, 'action_url' => $actionUrl, 'role_draft' => $roleDraft, 'role_name' => $roleName, 'hasErrors' => $hasErrors]);
 }
Beispiel #3
0
 /**
  * Creates a new RoleDraft for an existing Role.
  *
  * @since 6.2
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException if the Role already has a Role Draft that will need to be removed first,
  *                                                                  or if the authenticated user is not allowed to create a role
  * @throws \eZ\Publish\Core\REST\Server\Exceptions\BadRequestException if a policy limitation in the $roleCreateStruct is not valid
  *
  * @return \eZ\Publish\Core\REST\Server\Values\CreatedRole
  */
 public function createRoleDraft($roleId, Request $request)
 {
     try {
         $roleDraft = $this->roleService->createRoleDraft($this->roleService->loadRole($roleId));
     } catch (InvalidArgumentException $e) {
         throw new ForbiddenException($e->getMessage());
     } catch (UnauthorizedException $e) {
         throw new ForbiddenException($e->getMessage());
     } catch (LimitationValidationException $e) {
         throw new BadRequestException($e->getMessage());
     } catch (Exceptions\Parser $e) {
         throw new BadRequestException($e->getMessage());
     }
     return new Values\CreatedRole(['role' => new Values\RestRole($roleDraft)]);
 }
 /**
  * Creates a new RoleDraft for existing Role.
  *
  * @since 6.0
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a role
  * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the Role already has a Role Draft that will need to be removed first
  * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a policy limitation in the $roleCreateStruct is not valid
  *
  * @param \eZ\Publish\API\Repository\Values\User\Role $role
  *
  * @return \eZ\Publish\API\Repository\Values\User\RoleDraft
  */
 public function createRoleDraft(Role $role)
 {
     $returnValue = $this->service->createRoleDraft($role);
     $this->signalDispatcher->emit(new CreateRoleDraftSignal(array('roleId' => $returnValue->id)));
     return $returnValue;
 }