diff --git a/src/Bundle/ChillMainBundle/Controller/WorkflowSignatureCancelController.php b/src/Bundle/ChillMainBundle/Controller/WorkflowSignatureCancelController.php new file mode 100644 index 000000000..95cd1b779 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Controller/WorkflowSignatureCancelController.php @@ -0,0 +1,98 @@ +markSignatureAction( + $signature, + $request, + EntityWorkflowStepSignatureVoter::CANCEL, + function (EntityWorkflowStepSignature $signature) {$this->signatureStepStateChanger->markSignatureAsCanceled($signature); }, + '@ChillMain/WorkflowSignature/cancel.html.twig', + ); + } + + #[Route('/{_locale}/main/workflow/signature/{id}/reject', name: 'chill_main_workflow_signature_reject')] + public function rejectSignature(EntityWorkflowStepSignature $signature, Request $request): Response + { + return $this->markSignatureAction( + $signature, + $request, + EntityWorkflowStepSignatureVoter::REJECT, + function (EntityWorkflowStepSignature $signature) {$this->signatureStepStateChanger->markSignatureAsRejected($signature); }, + '@ChillMain/WorkflowSignature/reject.html.twig', + ); + } + + private function markSignatureAction( + EntityWorkflowStepSignature $signature, + Request $request, + string $permissionAttribute, + callable $markSignature, + string $template, + ): Response { + + if (!$this->security->isGranted($permissionAttribute, $signature)) { + throw new AccessDeniedHttpException('not allowed to cancel this signature'); + } + + $form = $this->formFactory->create(); + $form->add('confirm', SubmitType::class, ['label' => 'Confirm']); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $markSignature($signature); + $this->entityManager->flush(); + + return new RedirectResponse( + $this->chillUrlGenerator->returnPathOr('chill_main_workflow_show', ['id' => $signature->getStep()->getEntityWorkflow()->getId()]) + ); + } + + return + new Response( + $this->twig->render( + $template, + ['form' => $form->createView(), 'signature' => $signature] + ) + ); + } +} diff --git a/src/Bundle/ChillMainBundle/Entity/Workflow/EntityWorkflowStepSignature.php b/src/Bundle/ChillMainBundle/Entity/Workflow/EntityWorkflowStepSignature.php index 5b659d844..945e0d024 100644 --- a/src/Bundle/ChillMainBundle/Entity/Workflow/EntityWorkflowStepSignature.php +++ b/src/Bundle/ChillMainBundle/Entity/Workflow/EntityWorkflowStepSignature.php @@ -161,6 +161,16 @@ class EntityWorkflowStepSignature implements TrackCreationInterface, TrackUpdate return EntityWorkflowSignatureStateEnum::PENDING == $this->getState(); } + public function isCanceled(): bool + { + return EntityWorkflowSignatureStateEnum::CANCELED === $this->getState(); + } + + public function isRejected(): bool + { + return EntityWorkflowSignatureStateEnum::REJECTED === $this->getState(); + } + /** * Checks whether all signatures associated with a given workflow step are not pending. * diff --git a/src/Bundle/ChillMainBundle/Resources/views/Workflow/_signature.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Workflow/_signature.html.twig index b0e401645..42a3b68d8 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/Workflow/_signature.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/Workflow/_signature.html.twig @@ -3,7 +3,7 @@
{{ s.stateDate }}
- {% endif %} -{{ 'workflow.signature.cancel_are_you_sure'|trans({'%signer%': signature.signer|chill_entity_render_string}) }}
+ + {{ form_start(form) }} + + {{ form_end(form) }} +{% endblock %} diff --git a/src/Bundle/ChillMainBundle/Resources/views/WorkflowSignature/reject.html.twig b/src/Bundle/ChillMainBundle/Resources/views/WorkflowSignature/reject.html.twig new file mode 100644 index 000000000..f4047268e --- /dev/null +++ b/src/Bundle/ChillMainBundle/Resources/views/WorkflowSignature/reject.html.twig @@ -0,0 +1,20 @@ +{% extends '@ChillMain/layout.html.twig' %} + +{% block title %}{{ 'workflow.signature.reject_signature_of'|trans({ '%signer%': signature.signer|chill_entity_render_string }) }}{% endblock %} + +{% block content %} +{{ 'workflow.signature.reject_are_you_sure'|trans({'%signer%': signature.signer|chill_entity_render_string}) }}
+ + {{ form_start(form) }} + + {{ form_end(form) }} +{% endblock %} diff --git a/src/Bundle/ChillMainBundle/Routing/ChillUrlGenerator.php b/src/Bundle/ChillMainBundle/Routing/ChillUrlGenerator.php new file mode 100644 index 000000000..eaecee628 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Routing/ChillUrlGenerator.php @@ -0,0 +1,43 @@ +urlGenerator->generate($name, $parameters, $referenceType); + } + + public function generateWithReturnPath(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string + { + $uri = $this->requestStack->getCurrentRequest()->getRequestUri(); + + return $this->urlGenerator->generate($name, [$parameters, 'returnPath' => $uri], $referenceType); + } + + public function returnPathOr(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string + { + $request = $this->requestStack->getCurrentRequest(); + + if ($request->query->has('returnPath')) { + return $request->query->get('returnPath'); + } + + return $this->urlGenerator->generate($name, $parameters, $referenceType); + } +} diff --git a/src/Bundle/ChillMainBundle/Routing/ChillUrlGeneratorInterface.php b/src/Bundle/ChillMainBundle/Routing/ChillUrlGeneratorInterface.php new file mode 100644 index 000000000..5c00fa665 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Routing/ChillUrlGeneratorInterface.php @@ -0,0 +1,35 @@ +get($entityWorkflow, 'dummy'); $clock = new MockClock(); $user = new User(); - $changer = new SignatureStepStateChanger($registry, $clock, new NullLogger()); + + $messengerBus = new MessageBus([]); + $changer = new SignatureStepStateChanger($registry, $clock, new NullLogger(), $messengerBus); // move it to signature $dto = new WorkflowTransitionContextDTO($entityWorkflow); @@ -61,6 +64,8 @@ class SignatureStepStateChangerTest extends TestCase // we mark the first signature as signed $changer->markSignatureAsSigned($signatures[0], 1); + // the next step should be done by handling an async message + $changer->onPostMark($signatures[0]); self::assertEquals('signature', $entityWorkflow->getStep(), 'there should have any change in the entity workflow step'); self::assertEquals(EntityWorkflowSignatureStateEnum::SIGNED, $signatures[0]->getState()); @@ -70,6 +75,8 @@ class SignatureStepStateChangerTest extends TestCase // we mark the second signature as signed $changer->markSignatureAsSigned($signatures[1], 2); + // the next step should be done by handling an async message + $changer->onPostMark($signatures[1]); self::assertEquals(EntityWorkflowSignatureStateEnum::SIGNED, $signatures[1]->getState()); self::assertEquals('post-signature', $entityWorkflow->getStep(), 'the entity workflow step should be post-signature'); self::assertContains($user, $entityWorkflow->getCurrentStep()->getAllDestUser()); @@ -85,7 +92,7 @@ class SignatureStepStateChangerTest extends TestCase $workflow = $registry->get($entityWorkflow, 'dummy'); $clock = new MockClock(); $user = new User(); - $changer = new SignatureStepStateChanger($registry, $clock, new NullLogger()); + $changer = new SignatureStepStateChanger($registry, $clock, new NullLogger(), new MessageBus([])); // move it to signature $dto = new WorkflowTransitionContextDTO($entityWorkflow); @@ -102,6 +109,8 @@ class SignatureStepStateChangerTest extends TestCase // we mark the first signature as signed $changer->markSignatureAsSigned($signatures[0], 1); + // the next step should be done by handling an async message + $changer->onPostMark($signatures[0]); self::assertEquals('signature-without-metadata', $entityWorkflow->getStep(), 'there should have any change in the entity workflow step'); self::assertEquals(EntityWorkflowSignatureStateEnum::SIGNED, $signatures[0]->getState()); diff --git a/src/Bundle/ChillMainBundle/Workflow/Messenger/PostSignatureStateChangeHandler.php b/src/Bundle/ChillMainBundle/Workflow/Messenger/PostSignatureStateChangeHandler.php new file mode 100644 index 000000000..1a0d7bc5d --- /dev/null +++ b/src/Bundle/ChillMainBundle/Workflow/Messenger/PostSignatureStateChangeHandler.php @@ -0,0 +1,41 @@ +entityWorkflowStepSignatureRepository->find($message->signatureId); + + if (null === $signature) { + throw new UnrecoverableMessageHandlingException('signature not found'); + } + + $this->signatureStepStateChanger->onPostMark($signature); + + $this->entityManager->flush(); + $this->entityManager->clear(); + } +} diff --git a/src/Bundle/ChillMainBundle/Workflow/Messenger/PostSignatureStateChangeMessage.php b/src/Bundle/ChillMainBundle/Workflow/Messenger/PostSignatureStateChangeMessage.php new file mode 100644 index 000000000..44db0c500 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Workflow/Messenger/PostSignatureStateChangeMessage.php @@ -0,0 +1,22 @@ +setState(EntityWorkflowSignatureStateEnum::SIGNED) ->setZoneSignatureIndex($atIndex) - ->setStateDate($this->clock->now()) - ; - + ->setStateDate($this->clock->now()); $this->logger->info(self::LOG_PREFIX.'Mark signature entity as signed', ['signatureId' => $signature->getId(), 'index' => (string) $atIndex]); + $this->messageBus->dispatch(new PostSignatureStateChangeMessage((int) $signature->getId())); + } + public function markSignatureAsCanceled(EntityWorkflowStepSignature $signature): void + { + $signature + ->setState(EntityWorkflowSignatureStateEnum::CANCELED) + ->setStateDate($this->clock->now()); + $this->logger->info(self::LOG_PREFIX.'Mark signature entity as canceled', ['signatureId' => $signature->getId()]); + $this->messageBus->dispatch(new PostSignatureStateChangeMessage((int) $signature->getId())); + } + + public function markSignatureAsRejected(EntityWorkflowStepSignature $signature): void + { + $signature + ->setState(EntityWorkflowSignatureStateEnum::REJECTED) + ->setStateDate($this->clock->now()); + $this->logger->info(self::LOG_PREFIX.'Mark signature entity as rejected', ['signatureId' => $signature->getId()]); + $this->messageBus->dispatch(new PostSignatureStateChangeMessage((int) $signature->getId())); + } + + /** + * Executed after a signature has a new state. + * + * This should be executed only by a system user (without any user registered) + */ + public function onPostMark(EntityWorkflowStepSignature $signature): void + { if (!EntityWorkflowStepSignature::isAllSignatureNotPendingForStep($signature->getStep())) { $this->logger->info(self::LOG_PREFIX.'This is not the last signature, skipping transition to another place', ['signatureId' => $signature->getId()]); diff --git a/src/Bundle/ChillMainBundle/config/services/routing.yaml b/src/Bundle/ChillMainBundle/config/services/routing.yaml index 90ce6066f..7d5a80358 100644 --- a/src/Bundle/ChillMainBundle/config/services/routing.yaml +++ b/src/Bundle/ChillMainBundle/config/services/routing.yaml @@ -22,3 +22,8 @@ services: class: Chill\MainBundle\Routing\MenuTwig tags: - { name: twig.extension } + + Chill\MainBundle\Routing\ChillUrlGenerator: ~ + + Chill\MainBundle\Routing\ChillUrlGeneratorInterface: + alias: 'Chill\MainBundle\Routing\ChillUrlGenerator' diff --git a/src/Bundle/ChillMainBundle/translations/messages+intl-icu.fr.yaml b/src/Bundle/ChillMainBundle/translations/messages+intl-icu.fr.yaml index a0753f7a6..7f941b13a 100644 --- a/src/Bundle/ChillMainBundle/translations/messages+intl-icu.fr.yaml +++ b/src/Bundle/ChillMainBundle/translations/messages+intl-icu.fr.yaml @@ -45,8 +45,10 @@ workflow: few {# workflows} other {# workflows} } - signature_zone: - has_signed_statement: 'A signé le {datetime, date, short} à {datetime, time, short}' + signature: + signed_statement: 'Signature appliquée le {datetime, date, short} à {datetime, time, short}' + rejected_statement: 'Signature rejectée le {datetime, date, short} à {datetime, time, short}' + canceled_statement: 'Signature annulée le {datetime, date, short} à {datetime, time, short}' duration: diff --git a/src/Bundle/ChillMainBundle/translations/messages.fr.yml b/src/Bundle/ChillMainBundle/translations/messages.fr.yml index 495b9a3bd..8bade3ce6 100644 --- a/src/Bundle/ChillMainBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillMainBundle/translations/messages.fr.yml @@ -538,6 +538,8 @@ workflow: signature_zone: title: Signatures électroniques button_sign: Signer + button_cancel: Annuler + button_reject: Rejeter metadata: sign_by: 'Signature pour %name%' docType: Type de document @@ -550,6 +552,11 @@ workflow: user: Utilisateur already_signed_alert: La signature a déjà été appliquée + signature: + cancel_signature_of: Annulation de la signature de %signer% + cancel_are_you_sure: Êtes-vous sûr de vouloir annuler la signature de %signer% + reject_signature_of: Rejet de la signature de %signer% + reject_are_you_sure: Êtes-vous sûr de vouloir rejeter la signature de %signer% Subscribe final: Recevoir une notification à l'étape finale Subscribe all steps: Recevoir une notification à chaque étape diff --git a/src/Bundle/ChillPersonBundle/Tests/Controller/WorkflowSignatureCancelControllerStepTest.php b/src/Bundle/ChillPersonBundle/Tests/Controller/WorkflowSignatureCancelControllerStepTest.php new file mode 100644 index 000000000..727f525d3 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Controller/WorkflowSignatureCancelControllerStepTest.php @@ -0,0 +1,86 @@ +formFactory = self::getContainer()->get('form.factory'); + $this->signatureStepStateChanger = self::getContainer()->get(SignatureStepStateChanger::class); + $this->chillUrlGenerator = self::getContainer()->get(ChillUrlGeneratorInterface::class); + + $requestContext = self::getContainer()->get(RequestContext::class); + $requestContext->setParameter('_locale', 'fr'); + + $this->requestStack = self::getContainer()->get(RequestStack::class); + + } + + public function testCancelSignatureGet(): void + { + $entityWorkflow = new EntityWorkflow(); + $dto = new WorkflowTransitionContextDTO($entityWorkflow); + $dto->futureUserSignature = new User(); + $entityWorkflow->setStep('signature', $dto, 'to_signature', new \DateTimeImmutable(), new User()); + $signature = $entityWorkflow->getCurrentStep()->getSignatures()->first(); + + $security = $this->createMock(Security::class); + $security->expects($this->once())->method('isGranted') + ->with(EntityWorkflowStepSignatureVoter::CANCEL, $signature)->willReturn(true); + + $entityManager = $this->createMock(EntityManager::class); + + $twig = $this->createMock(Environment::class); + $twig->expects($this->once())->method('render')->withAnyParameters() + ->willReturn('template'); + + $controller = new WorkflowSignatureCancelController($entityManager, $security, $this->formFactory, $twig, $this->signatureStepStateChanger, $this->chillUrlGenerator); + + $request = new Request(); + $request->setMethod('GET'); + + $this->requestStack->push($request); + + $response = $controller->cancelSignature($signature, $request); + + self::assertEquals(200, $response->getStatusCode()); + } +}