mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Merge branch 'feature/704-un-related-entity-ne-peut-pas-faire-lobjet-de-deux-workflow-simultanés' into 'signature-app-master'
Prevent creation of a new workflow if one already exists See merge request Chill-Projet/chill-bundles!737
This commit is contained in:
commit
75005b4ed6
@ -102,11 +102,17 @@ export default {
|
||||
},
|
||||
getPopContent(step) {
|
||||
if (step.transitionPrevious != null) {
|
||||
return `<ul class="small_in_title">
|
||||
<li><span class="item-key">${i18n.messages.fr.by} : </span><b>${step.transitionPreviousBy.text}</b></li>
|
||||
<li><span class="item-key">${i18n.messages.fr.at} : </span><b>${this.formatDate(step.transitionPreviousAt.datetime)}</b></li>
|
||||
</ul>`
|
||||
;
|
||||
if (step.transitionPreviousBy !== null) {
|
||||
return `<ul class="small_in_title">
|
||||
<li><span class="item-key">${i18n.messages.fr.by} : </span><b>${step.transitionPreviousBy.text}</b></li>
|
||||
<li><span class="item-key">${i18n.messages.fr.at} : </span><b>${this.formatDate(step.transitionPreviousAt.datetime)}</b></li>
|
||||
</ul>`
|
||||
;
|
||||
} else {
|
||||
return `<ul class="small_in_title">
|
||||
<li><span class="item-key">${i18n.messages.fr.at} : </span><b>${this.formatDate(step.transitionPreviousAt.datetime)}</b></li>
|
||||
</ul>`
|
||||
}
|
||||
}
|
||||
},
|
||||
formatDate(datetime) {
|
||||
|
@ -6,7 +6,7 @@
|
||||
{{ form_errors(transition_form) }}
|
||||
|
||||
{% set step = entity_workflow.currentStepChained %}
|
||||
{% set labels = workflow_metadata(entity_workflow, 'label', step.currentStep) %}
|
||||
{% set labels = workflow_metadata(entity_workflow, 'label', step.currentStep, entity_workflow.workflowName) %}
|
||||
{% set label = labels is null ? step.currentStep : labels|localize_translatable_string %}
|
||||
|
||||
<div class="card">
|
||||
|
@ -1,8 +1,5 @@
|
||||
{% set acl = "0" %}
|
||||
{% if is_granted('CHILL_MAIN_WORKFLOW_CREATE', blank_workflow) %}
|
||||
{% set acl = "1" %}
|
||||
{% endif %}
|
||||
|
||||
{% set acl = "1" %}
|
||||
{# note: the list of available workflows already check that it is possible to create a given workflow #}
|
||||
{# vue component #}
|
||||
<div data-list-workflows="1"
|
||||
data-workflows="{{ entity_workflows_json|json_encode|e('html_attr') }}"
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="flex-table">
|
||||
{% for step in entity_workflow.stepsChained %}
|
||||
{% set place_labels = workflow_metadata(entity_workflow, 'label', step.currentStep) %}
|
||||
{% set place_labels = workflow_metadata(entity_workflow, 'label', step.currentStep, entity_workflow.workflowName) %}
|
||||
{% set place_label = place_labels is null ? step.currentStep : place_labels|localize_translatable_string %}
|
||||
|
||||
<div class="item-bloc {{ 'bloc' ~ step.id }} {% if loop.first %}initial{% endif %}">
|
||||
@ -35,9 +35,9 @@
|
||||
</div>
|
||||
{% if step.next is not null %}
|
||||
{% set transition = chill_workflow_transition_by_string(step.entityWorkflow, step.transitionAfter) %}
|
||||
{% set transition_labels = workflow_metadata(step.entityWorkflow, 'label', transition) %}
|
||||
{% set transition_labels = workflow_metadata(step.entityWorkflow, 'label', transition, step.entityWorkflow.workflowName) %}
|
||||
{% set transition_label = transition_labels is null ? step.transitionAfter : transition_labels|localize_translatable_string %}
|
||||
{% set forward = workflow_metadata(step.entityWorkflow, 'isForward', transition) %}
|
||||
{% set forward = workflow_metadata(step.entityWorkflow, 'isForward', transition, step.entityWorkflow.workflowName) %}
|
||||
<div class="item-row separator">
|
||||
<div class="item-col" style="width: inherit;">
|
||||
<div>
|
||||
|
@ -44,7 +44,7 @@
|
||||
{% endif %}
|
||||
{% if step.previous is not null %}
|
||||
{% set transition = chill_workflow_transition_by_string(step.entityWorkflow, step.previous.transitionAfter) %}
|
||||
{% set labels = workflow_metadata(step.entityWorkflow, 'label', transition) %}
|
||||
{% set labels = workflow_metadata(step.entityWorkflow, 'label', transition, step.entityWorkflow.workflowName) %}
|
||||
{% set label = labels is null ? step.previous.transitionAfter : labels|localize_translatable_string %}
|
||||
{{ label }}
|
||||
{% endif %}
|
||||
@ -53,7 +53,7 @@
|
||||
{% macro breadcrumb(_ctx) %}
|
||||
<div class="breadcrumb">
|
||||
{% for step in _ctx.entity_workflow.stepsChained %}
|
||||
{% set labels = workflow_metadata(_ctx.entity_workflow, 'label', step.currentStep) %}
|
||||
{% set labels = workflow_metadata(_ctx.entity_workflow, 'label', step.currentStep, _ctx.entity_workflow.workflowName) %}
|
||||
{% set label = labels is null ? step.currentStep : labels|localize_translatable_string %}
|
||||
{% set popTitle = _self.popoverTitle(step) %}
|
||||
{% set popContent = _self.popoverContent(step) %}
|
||||
|
@ -13,6 +13,7 @@ namespace Chill\MainBundle\Security\Authorization;
|
||||
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||
use Chill\MainBundle\Workflow\EntityWorkflowManager;
|
||||
use Chill\MainBundle\Workflow\Helper\DuplicateEntityWorkflowFinder;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
@ -27,7 +28,11 @@ class EntityWorkflowVoter extends Voter
|
||||
|
||||
final public const SHOW_ENTITY_LINK = 'CHILL_MAIN_WORKFLOW_LINK_SHOW';
|
||||
|
||||
public function __construct(private readonly EntityWorkflowManager $manager, private readonly Security $security) {}
|
||||
public function __construct(
|
||||
private readonly EntityWorkflowManager $manager,
|
||||
private readonly Security $security,
|
||||
private readonly DuplicateEntityWorkflowFinder $duplicateEntityWorkflowFinder,
|
||||
) {}
|
||||
|
||||
protected function supports($attribute, $subject)
|
||||
{
|
||||
@ -41,6 +46,15 @@ class EntityWorkflowVoter extends Voter
|
||||
{
|
||||
switch ($attribute) {
|
||||
case self::CREATE:
|
||||
if (false === $this->voteOnAttribute(self::SEE, $subject, $token)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->duplicateEntityWorkflowFinder->hasDuplicateOpenedOrFinalPositiveEntityWorkflow($subject)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
case self::SEE:
|
||||
$handler = $this->manager->getHandler($subject);
|
||||
|
||||
|
@ -0,0 +1,105 @@
|
||||
<?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\Tests\Workflow\Helper;
|
||||
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||
use Chill\MainBundle\Workflow\EntityWorkflowMarkingStore;
|
||||
use Chill\MainBundle\Workflow\Helper\OpenedEntityWorkflowHelper;
|
||||
use Chill\MainBundle\Workflow\WorkflowTransitionContextDTO;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Workflow\DefinitionBuilder;
|
||||
use Symfony\Component\Workflow\Metadata\InMemoryMetadataStore;
|
||||
use Symfony\Component\Workflow\Registry;
|
||||
use Symfony\Component\Workflow\SupportStrategy\WorkflowSupportStrategyInterface;
|
||||
use Symfony\Component\Workflow\Workflow;
|
||||
use Symfony\Component\Workflow\WorkflowInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class OpenedEntityWorkflowHelperTest extends TestCase
|
||||
{
|
||||
private function buildRegistry(): Registry
|
||||
{
|
||||
$builder = new DefinitionBuilder();
|
||||
$builder
|
||||
->setInitialPlaces('initial')
|
||||
->addPlaces(['initial', 'final_positive', 'final_negative', 'final_no_data'])
|
||||
->setMetadataStore(
|
||||
new InMemoryMetadataStore(
|
||||
placesMetadata: [
|
||||
'final_positive' => ['isFinalPositive' => true, 'isFinal' => true],
|
||||
'final_negative' => ['isFinalPositive' => false, 'isFinal' => true],
|
||||
'final_no_data' => ['isFinal' => true],
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$workflow = new Workflow($builder->build(), new EntityWorkflowMarkingStore(), name: 'dummy');
|
||||
$registry = new Registry();
|
||||
$registry->addWorkflow(
|
||||
$workflow,
|
||||
new class () implements WorkflowSupportStrategyInterface {
|
||||
public function supports(WorkflowInterface $workflow, object $subject): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return $registry;
|
||||
}
|
||||
|
||||
public function testIsFinalPositive(): void
|
||||
{
|
||||
$entityWorkflow = new EntityWorkflow();
|
||||
$entityWorkflow->setWorkflowName('dummy');
|
||||
$dto = new WorkflowTransitionContextDTO($entityWorkflow);
|
||||
$entityWorkflow->setStep('final_positive', $dto, 'fake', new \DateTimeImmutable());
|
||||
$entityWorkflow->getCurrentStep()->setIsFinal(true);
|
||||
|
||||
$helper = new OpenedEntityWorkflowHelper($this->buildRegistry());
|
||||
|
||||
self::assertTrue($helper->isFinalPositive($entityWorkflow));
|
||||
self::assertFalse($helper->isFinalNegative($entityWorkflow));
|
||||
}
|
||||
|
||||
public function testIsFinalNegative(): void
|
||||
{
|
||||
$entityWorkflow = new EntityWorkflow();
|
||||
$entityWorkflow->setWorkflowName('dummy');
|
||||
$dto = new WorkflowTransitionContextDTO($entityWorkflow);
|
||||
$entityWorkflow->setStep('final_negative', $dto, 'fake', new \DateTimeImmutable());
|
||||
$entityWorkflow->getCurrentStep()->setIsFinal(true);
|
||||
|
||||
$helper = new OpenedEntityWorkflowHelper($this->buildRegistry());
|
||||
|
||||
self::assertFalse($helper->isFinalPositive($entityWorkflow));
|
||||
self::assertTrue($helper->isFinalNegative($entityWorkflow));
|
||||
}
|
||||
|
||||
public function testIsFinalNoInformation(): void
|
||||
{
|
||||
$entityWorkflow = new EntityWorkflow();
|
||||
$entityWorkflow->setWorkflowName('dummy');
|
||||
$dto = new WorkflowTransitionContextDTO($entityWorkflow);
|
||||
$entityWorkflow->setStep('final_no_data', $dto, 'fake', new \DateTimeImmutable());
|
||||
$entityWorkflow->getCurrentStep()->setIsFinal(true);
|
||||
|
||||
$helper = new OpenedEntityWorkflowHelper($this->buildRegistry());
|
||||
|
||||
self::assertFalse($helper->isFinalPositive($entityWorkflow));
|
||||
self::assertFalse($helper->isFinalNegative($entityWorkflow));
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
<?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\Helper;
|
||||
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
|
||||
|
||||
/**
|
||||
* Class DuplicateEntityWorkflowFinder.
|
||||
*
|
||||
* Finds duplicate entity workflows based on their status.
|
||||
*/
|
||||
class DuplicateEntityWorkflowFinder
|
||||
{
|
||||
public function __construct(private readonly EntityWorkflowRepository $entityWorkflowRepository, private readonly OpenedEntityWorkflowHelper $openedEntityWorkflowHelper) {}
|
||||
|
||||
/**
|
||||
* Return true if the related entity has opened workflow with same name, or workflow with a positive final step.
|
||||
*
|
||||
* This will return false if the other workflow is final **and** negative (because it has been canceled, for instance).
|
||||
*
|
||||
* If there isn't any other workflow related to the same entity, this will return false.
|
||||
*/
|
||||
public function hasDuplicateOpenedOrFinalPositiveEntityWorkflow(EntityWorkflow $entityWorkflow): bool
|
||||
{
|
||||
$otherWorkflows = $this->entityWorkflowRepository->findByRelatedEntity($entityWorkflow->getRelatedEntityClass(), $entityWorkflow->getRelatedEntityId());
|
||||
|
||||
foreach ($otherWorkflows as $otherWorkflow) {
|
||||
if ($entityWorkflow === $otherWorkflow) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($otherWorkflow->getWorkflowName() !== $entityWorkflow->getWorkflowName()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$this->openedEntityWorkflowHelper->isFinal($otherWorkflow)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->openedEntityWorkflowHelper->isFinalPositive($otherWorkflow)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@ -19,20 +19,33 @@ use Symfony\Component\Workflow\WorkflowInterface;
|
||||
|
||||
class MetadataExtractor
|
||||
{
|
||||
public function __construct(private readonly Registry $registry, private readonly TranslatableStringHelperInterface $translatableStringHelper) {}
|
||||
public function __construct(
|
||||
private readonly Registry $registry,
|
||||
private readonly TranslatableStringHelperInterface $translatableStringHelper,
|
||||
private readonly DuplicateEntityWorkflowFinder $duplicateEntityWorkflowFinder,
|
||||
) {}
|
||||
|
||||
public function availableWorkflowFor(string $relatedEntityClass, ?int $relatedEntityId = 0): array
|
||||
{
|
||||
$blankEntityWorkflow = new EntityWorkflow();
|
||||
$blankEntityWorkflow
|
||||
->setRelatedEntityId($relatedEntityId)
|
||||
->setRelatedEntityClass($relatedEntityClass);
|
||||
->setRelatedEntityClass($relatedEntityClass)
|
||||
;
|
||||
|
||||
// build the list of available workflows, and extract their names from metadata
|
||||
$workflows = $this->registry->all($blankEntityWorkflow);
|
||||
$workflowsList = [];
|
||||
|
||||
foreach ($workflows as $workflow) {
|
||||
// shortcut: we must not be able to create a new workflow if there are already created workflows,
|
||||
// so, we find if there are workflows opened or final positive for the same entity
|
||||
$blankEntityWorkflow->setWorkflowName($workflow->getName());
|
||||
if ($this->duplicateEntityWorkflowFinder->hasDuplicateOpenedOrFinalPositiveEntityWorkflow($blankEntityWorkflow)) {
|
||||
// if yes, we skip suggesting workflow
|
||||
continue;
|
||||
}
|
||||
|
||||
$metadata = $workflow->getMetadataStore()->getWorkflowMetadata();
|
||||
$text = \array_key_exists('label', $metadata) ?
|
||||
$this->translatableStringHelper->localize($metadata['label']) : $workflow->getName();
|
||||
|
@ -0,0 +1,83 @@
|
||||
<?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\Helper;
|
||||
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||
use Symfony\Component\Workflow\Registry;
|
||||
|
||||
/**
|
||||
* Helps to determine the status of the EntityWorkflow.
|
||||
*
|
||||
* A "negative final" step means that the workflow has been canceled, without being formerly finalized
|
||||
*/
|
||||
class OpenedEntityWorkflowHelper
|
||||
{
|
||||
public function __construct(private readonly Registry $registry) {}
|
||||
|
||||
/**
|
||||
* Return true if the entity workflow has reached a final step.
|
||||
*
|
||||
* @return bool true if the entityworkflow has reached a final step
|
||||
*/
|
||||
public function isFinal(EntityWorkflow $entityWorkflow): bool
|
||||
{
|
||||
return $entityWorkflow->isFinal();
|
||||
}
|
||||
|
||||
/**
|
||||
* return true if the entity workflow has reached a final and positive step.
|
||||
*
|
||||
* If multiple steps, they are all taken into account: return true if at least one step has the metadata
|
||||
* `isFinalPositive: true`
|
||||
*/
|
||||
public function isFinalPositive(EntityWorkflow $entityWorkflow): bool
|
||||
{
|
||||
if (!$this->isFinal($entityWorkflow)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->findFinalMark($entityWorkflow, true) ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* return true if the entity workflow has reached a final and negative step.
|
||||
*
|
||||
* If multiple steps, they are all taken into account: return true if at least one step has the metadata
|
||||
* `isFinalPositive: false`.
|
||||
*/
|
||||
public function isFinalNegative(EntityWorkflow $entityWorkflow): bool
|
||||
{
|
||||
if (!$this->isFinal($entityWorkflow)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->findFinalMark($entityWorkflow, false) ?? false;
|
||||
}
|
||||
|
||||
private function findFinalMark(EntityWorkflow $entityWorkflow, bool $expectedMark): ?bool
|
||||
{
|
||||
$workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
|
||||
$marking = $workflow->getMarkingStore()->getMarking($entityWorkflow);
|
||||
|
||||
foreach ($marking->getPlaces() as $place => $key) {
|
||||
$metadata = $workflow->getMetadataStore()->getPlaceMetadata($place);
|
||||
|
||||
if (array_key_exists('isFinalPositive', $metadata)) {
|
||||
if ($expectedMark === $metadata['isFinalPositive']) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -59,7 +59,6 @@ class WorkflowTwigExtensionRuntime implements RuntimeExtensionInterface
|
||||
|
||||
return $environment->render('@ChillMain/Workflow/_extension_list_workflow_for.html.twig', [
|
||||
'entity_workflows_json' => $this->normalizer->normalize($entityWorkflows, 'json', ['groups' => 'read']),
|
||||
'blank_workflow' => $blankEntityWorkflow,
|
||||
'workflows_available' => $workflowsAvailable,
|
||||
'relatedEntityClass' => $relatedEntityClass,
|
||||
'relatedEntityId' => $relatedEntityId,
|
||||
|
Loading…
x
Reference in New Issue
Block a user