mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-06 06:44:59 +00:00
Adjust logic for removing the hold on a workflow only by user who owns the hold and when a transition is applied on the workflow
This commit is contained in:
@@ -4,6 +4,7 @@ namespace Chill\MainBundle\Controller;
|
||||
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStepHold;
|
||||
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
|
||||
use Chill\MainBundle\Repository\Workflow\EntityWorkflowStepHoldRepository;
|
||||
use Chill\MainBundle\Security\ChillSecurity;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
@@ -20,32 +21,23 @@ class WorkflowOnHoldController extends AbstractController
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly Security $security,
|
||||
private readonly Registry $registry,
|
||||
private readonly EntityWorkflowStepHoldRepository $entityWorkflowStepHoldRepository
|
||||
private readonly EntityWorkflowStepHoldRepository $entityWorkflowStepHoldRepository,
|
||||
private readonly EntityWorkflowRepository $entityWorkflowRepository
|
||||
) {}
|
||||
|
||||
#[Route(path: '/{_locale}/main/workflow/{id}/hold', name: 'chill_main_workflow_on_hold')]
|
||||
public function putOnHold(EntityWorkflow $entityWorkflow, Request $request): Response
|
||||
{
|
||||
$entityWorkflow = $this->entityWorkflowRepository->find($entityWorkflow);
|
||||
|
||||
$currentStep = $entityWorkflow->getCurrentStep();
|
||||
$currentUser = $this->security->getUser();
|
||||
|
||||
$workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
|
||||
|
||||
$enabledTransitions = $workflow->getEnabledTransitions($entityWorkflow);
|
||||
if (\count($enabledTransitions) === 0) {
|
||||
throw $this->createAccessDeniedException('No transitions are available for the current workflow state.');
|
||||
}
|
||||
|
||||
$isTransitionAllowed = false;
|
||||
foreach ($enabledTransitions as $transition) {
|
||||
if ($workflow->can($entityWorkflow, $transition->getName())) {
|
||||
$isTransitionAllowed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$isTransitionAllowed) {
|
||||
throw $this->createAccessDeniedException('You are not allowed to apply any transitions to this workflow, therefore you cannot put it on hold.');
|
||||
if (!count($enabledTransitions) > 0) {
|
||||
throw $this->createAccessDeniedException('You are not allowed to apply any transitions to this workflow, therefore you cannot toggle the hold status.');
|
||||
}
|
||||
|
||||
$stepHold = new EntityWorkflowStepHold($currentStep, $currentUser);
|
||||
@@ -61,10 +53,16 @@ class WorkflowOnHoldController extends AbstractController
|
||||
{
|
||||
$hold = $this->entityWorkflowStepHoldRepository->findById($holdId);
|
||||
$entityWorkflow = $hold->getStep()->getEntityWorkflow();
|
||||
$currentUser = $this->security->getUser();
|
||||
|
||||
if ($hold->getByUser() !== $currentUser) {
|
||||
throw $this->createAccessDeniedException('You are not allowed to remove the hold status.');
|
||||
}
|
||||
|
||||
$this->entityManager->remove($hold);
|
||||
$this->entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('chill_main_workflow_show', ['id' => $entityWorkflow->getId()]);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user