Add TransitionHasSignerIfSignature validator

Introduced a new validator `TransitionHasSignerIfSignature` to enforce that a signature transition must have a designated signer. Included related tests, updated DTO annotations, and added translations for new validation messages.
This commit is contained in:
2024-11-07 19:55:09 +01:00
parent b6c141a785
commit e5148f603b
5 changed files with 273 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
<?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\Workflow\Validator;
use Symfony\Component\Validator\Constraint;
#[\Attribute]
class TransitionHasSignerIfSignature extends Constraint
{
public $message = 'workflow.You must add a destinee for signing';
public $messageShouldBeEmpty = 'workflow.You must not add a destinee for signing';
public $code = '81d9e284-9d31-11ef-a078-6767f100370b';
public function getTargets(): string
{
return self::CLASS_CONSTRAINT;
}
}

View File

@@ -0,0 +1,68 @@
<?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\Workflow\Validator;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Workflow\WorkflowTransitionContextDTO;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Workflow\Registry;
final class TransitionHasSignerIfSignatureValidator extends ConstraintValidator
{
public function __construct(
private readonly Registry $registry,
) {}
public function validate($value, Constraint $constraint): void
{
if (!$constraint instanceof TransitionHasSignerIfSignature) {
throw new UnexpectedTypeException($constraint, TransitionHasSignerIfSignature::class);
}
if (!$value instanceof WorkflowTransitionContextDTO) {
throw new UnexpectedTypeException($value, WorkflowTransitionContextDTO::class);
}
if (null === $value->transition) {
return;
}
$workflow = $this->registry->get($value->entityWorkflow, $value->entityWorkflow->getWorkflowName());
foreach ($value->transition->getTos() as $to) {
$metadata = $workflow->getMetadataStore()->getPlaceMetadata($to);
if ([] !== ($metadata['isSignature'] ?? [])) {
if (null === $value->futureUserSignature && [] === $value->futurePersonSignatures) {
$this->context->buildViolation($constraint->message)
->setCode($constraint->code)
->addViolation();
return;
}
return;
}
}
// at this step, there is no signature in the To, we must check there is not users or persons
if ($value->futureUserSignature instanceof User || [] !== $value->futurePersonSignatures) {
$this->context->buildViolation($constraint->messageShouldBeEmpty)
->setCode($constraint->code)
->atPath($value->futureUserSignature instanceof User ? 'futureUserSignature' : 'futurePersonSignatures')
->addViolation();
}
}
}

View File

@@ -16,6 +16,7 @@ use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Workflow\Validator\TransitionHasDestineeIfIsSentExternal;
use Chill\MainBundle\Workflow\Validator\TransitionHasDestUserIfRequired;
use Chill\MainBundle\Workflow\Validator\TransitionHasSignerIfSignature;
use Chill\PersonBundle\Entity\Person;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\ThirdPartyBundle\Validator\ThirdPartyHasEmail;
@@ -28,6 +29,7 @@ use Symfony\Component\Workflow\Transition;
*/
#[TransitionHasDestineeIfIsSentExternal]
#[TransitionHasDestUserIfRequired]
#[TransitionHasSignerIfSignature]
class WorkflowTransitionContextDTO
{
/**