From a21637331fe61f70be7907496c8e622d247e2a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 13 Apr 2023 12:24:44 +0200 Subject: [PATCH] Fixed: [workflow] validation of users in cc and dest The validation is processed at the form step, so before the transition is processed --- .../Controller/WorkflowController.php | 6 ++- .../Entity/Workflow/EntityWorkflow.php | 4 +- .../Entity/Workflow/EntityWorkflowStep.php | 2 - .../ChillMainBundle/Form/WorkflowStepType.php | 14 ++++++ .../Constraints/Entity/WorkflowStepUsers.php | 27 ------------ .../Entity/WorkflowStepUsersValidator.php | 43 ------------------- .../translations/validators.fr.yml | 3 +- 7 files changed, 24 insertions(+), 75 deletions(-) delete mode 100644 src/Bundle/ChillMainBundle/Validator/Constraints/Entity/WorkflowStepUsers.php delete mode 100644 src/Bundle/ChillMainBundle/Validator/Constraints/Entity/WorkflowStepUsersValidator.php diff --git a/src/Bundle/ChillMainBundle/Controller/WorkflowController.php b/src/Bundle/ChillMainBundle/Controller/WorkflowController.php index 789e8dc34..ee09cc1a6 100644 --- a/src/Bundle/ChillMainBundle/Controller/WorkflowController.php +++ b/src/Bundle/ChillMainBundle/Controller/WorkflowController.php @@ -333,7 +333,11 @@ class WorkflowController extends AbstractController $transitionForm = $this->createForm( WorkflowStepType::class, $entityWorkflow->getCurrentStep(), - ['transition' => true, 'entity_workflow' => $entityWorkflow, 'suggested_users' => $usersInvolved] + [ + 'transition' => true, + 'entity_workflow' => $entityWorkflow, + 'suggested_users' => $usersInvolved + ] ); $transitionForm->handleRequest($request); diff --git a/src/Bundle/ChillMainBundle/Entity/Workflow/EntityWorkflow.php b/src/Bundle/ChillMainBundle/Entity/Workflow/EntityWorkflow.php index 73f4c2b04..d0f558cd5 100644 --- a/src/Bundle/ChillMainBundle/Entity/Workflow/EntityWorkflow.php +++ b/src/Bundle/ChillMainBundle/Entity/Workflow/EntityWorkflow.php @@ -16,6 +16,7 @@ use Chill\MainBundle\Doctrine\Model\TrackCreationTrait; use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface; use Chill\MainBundle\Doctrine\Model\TrackUpdateTrait; use Chill\MainBundle\Entity\User; +use Chill\MainBundle\Validator\Constraints\Entity\WorkflowStepUsersOnTransition; use Chill\MainBundle\Workflow\Validator\EntityWorkflowCreation; use DateTimeInterface; use Doctrine\Common\Collections\ArrayCollection; @@ -24,6 +25,7 @@ use Doctrine\ORM\Mapping as ORM; use Iterator; use RuntimeException; use Symfony\Component\Serializer\Annotation as Serializer; +use Symfony\Component\Validator\Constraints as Assert; use function count; use function is_array; @@ -97,7 +99,7 @@ class EntityWorkflow implements TrackCreationInterface, TrackUpdateInterface /** * @ORM\OneToMany(targetEntity=EntityWorkflowStep::class, mappedBy="entityWorkflow", orphanRemoval=true, cascade={"persist"}) * @ORM\OrderBy({"transitionAt": "ASC", "id": "ASC"}) - * + * @Assert\Valid(traverse=true) * @var Collection|EntityWorkflowStep[] */ private Collection $steps; diff --git a/src/Bundle/ChillMainBundle/Entity/Workflow/EntityWorkflowStep.php b/src/Bundle/ChillMainBundle/Entity/Workflow/EntityWorkflowStep.php index 39729aa96..e43d524a4 100644 --- a/src/Bundle/ChillMainBundle/Entity/Workflow/EntityWorkflowStep.php +++ b/src/Bundle/ChillMainBundle/Entity/Workflow/EntityWorkflowStep.php @@ -12,7 +12,6 @@ declare(strict_types=1); namespace Chill\MainBundle\Entity\Workflow; use Chill\MainBundle\Entity\User; -use Chill\MainBundle\Validator\Constraints\Entity\WorkflowStepUsers; use DateTimeImmutable; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; @@ -25,7 +24,6 @@ use function in_array; /** * @ORM\Entity * @ORM\Table("chill_main_workflow_entity_step") - * @WorkflowStepUsers() */ class EntityWorkflowStep { diff --git a/src/Bundle/ChillMainBundle/Form/WorkflowStepType.php b/src/Bundle/ChillMainBundle/Form/WorkflowStepType.php index 3ef37c03a..16dc0a4a5 100644 --- a/src/Bundle/ChillMainBundle/Form/WorkflowStepType.php +++ b/src/Bundle/ChillMainBundle/Form/WorkflowStepType.php @@ -243,6 +243,20 @@ class WorkflowStepType extends AbstractType } } ), + new Callback( + function ($step, ExecutionContextInterface $context, $payload) { + $form = $context->getObject(); + + foreach($form->get('future_dest_users')->getData() as $u) { + if (in_array($u, $form->get('future_cc_users')->getData(), true)) { + $context + ->buildViolation('workflow.The user in cc cannot be a dest user in the same workflow step') + ->atPath('ccUsers') + ->addViolation(); + } + } + } + ) ]); } } diff --git a/src/Bundle/ChillMainBundle/Validator/Constraints/Entity/WorkflowStepUsers.php b/src/Bundle/ChillMainBundle/Validator/Constraints/Entity/WorkflowStepUsers.php deleted file mode 100644 index 842df37cf..000000000 --- a/src/Bundle/ChillMainBundle/Validator/Constraints/Entity/WorkflowStepUsers.php +++ /dev/null @@ -1,27 +0,0 @@ -getDestUser() as $u) { - if ($value->getCcUser()->contains($u)) { - $this->context - ->buildViolation($constraint->message) - ->atPath('ccUsers') - ->addViolation(); - } - } - } -} diff --git a/src/Bundle/ChillMainBundle/translations/validators.fr.yml b/src/Bundle/ChillMainBundle/translations/validators.fr.yml index 31d3bdc73..4ab9bde34 100644 --- a/src/Bundle/ChillMainBundle/translations/validators.fr.yml +++ b/src/Bundle/ChillMainBundle/translations/validators.fr.yml @@ -33,6 +33,7 @@ notification: workflow: You must add at least one dest user or email: Indiquez au moins un destinataire ou une adresse email + The user in cc cannot be a dest user in the same workflow step: L'utilisateur en copie ne peut pas être présent dans les utilisateurs qui valideront la prochaine étape rolling_date: - When fixed date is selected, you must provide a date: Indiquez la date fixe choisie \ No newline at end of file + When fixed date is selected, you must provide a date: Indiquez la date fixe choisie