Fixed: [workflow] validation of users in cc and dest

The validation is processed at the form step, so before the transition is processed
This commit is contained in:
Julien Fastré 2023-04-13 12:24:44 +02:00
parent 63759a940f
commit a21637331f
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
7 changed files with 24 additions and 75 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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
{

View File

@ -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();
}
}
}
)
]);
}
}

View File

@ -1,27 +0,0 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Validator\Constraints\Entity;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class WorkflowStepUsers extends Constraint
{
public string $message = 'The user in cc cannot be a dest user in the same workflow step';
public function getTargets()
{
return [self::CLASS_CONSTRAINT];
}
}

View File

@ -1,43 +0,0 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Validator\Constraints\Entity;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* @Annotation
*/
class WorkflowStepUsersValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if (!$value instanceof EntityWorkflowStep) {
throw new UnexpectedTypeException($value, EntityWorkflowStep::class);
}
if (!$constraint instanceof WorkflowStepUsers) {
throw new UnexpectedTypeException($constraint, WorkflowStepUsers::class);
}
foreach($value->getDestUser() as $u) {
if ($value->getCcUser()->contains($u)) {
$this->context
->buildViolation($constraint->message)
->atPath('ccUsers')
->addViolation();
}
}
}
}

View File

@ -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