chill-bundles/src/Bundle/ChillMainBundle/Workflow/EventSubscriber/EntityWorkflowGuardTransition.php
Julien Fastré 70671dadac
Refactor workflow guard logic and add internal methods
Removed guard logic from EntityWorkflowTransitionEventSubscriber and created a new EntityWorkflowGuardTransition class for separation of concerns. Marked several setter methods in EntityWorkflowStepSignature as internal to guide proper usage. Added comprehensive tests to ensure the new guard logic functions correctly.
2024-09-11 21:13:58 +02:00

106 lines
3.3 KiB
PHP

<?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\EventSubscriber;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Templating\Entity\UserRender;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\TransitionBlocker;
class EntityWorkflowGuardTransition implements EventSubscriberInterface
{
public function __construct(
private readonly UserRender $userRender,
private readonly Security $security,
) {}
public static function getSubscribedEvents(): array
{
return [
'workflow.guard' => [
['guardEntityWorkflow', 0],
],
];
}
public function guardEntityWorkflow(GuardEvent $event)
{
if (!$event->getSubject() instanceof EntityWorkflow) {
return;
}
/** @var EntityWorkflow $entityWorkflow */
$entityWorkflow = $event->getSubject();
if ($entityWorkflow->isFinal()) {
$event->addTransitionBlocker(
new TransitionBlocker(
'workflow.The workflow is finalized',
'd6306280-7535-11ec-a40d-1f7bee26e2c0'
)
);
return;
}
$user = $this->security->getUser();
$metadata = $event->getWorkflow()->getMetadataStore()->getTransitionMetadata($event->getTransition());
$systemTransitions = explode('+', $metadata['transitionGuard'] ?? 'only-dest');
if (null === $user) {
if (in_array('system', $systemTransitions, true)) {
// it is safe to apply this transition
return;
}
$event->addTransitionBlocker(
new TransitionBlocker(
'workflow.Transition is not allowed for system',
'd9e39a18-704c-11ef-b235-8fe0619caee7'
)
);
return;
}
// for users
if (!in_array('only-dest', $systemTransitions, true)) {
$event->addTransitionBlocker(
new TransitionBlocker(
'workflow.Only system can apply this transition',
'5b6b95e0-704d-11ef-a5a9-4b6fc11a8eeb'
)
);
}
if (!$entityWorkflow->getCurrentStep()->getAllDestUser()->contains($user)) {
if ($event->getMarking()->has('initial')) {
return;
}
$event->addTransitionBlocker(new TransitionBlocker(
'workflow.You are not allowed to apply a transition on this workflow. Only those users are allowed: %users%',
'f3eeb57c-7532-11ec-9495-e7942a2ac7bc',
[
'%users%' => implode(
', ',
$entityWorkflow->getCurrentStep()->getAllDestUser()->map(fn (User $u) => $this->userRender->renderString($u, []))->toArray()
),
]
));
}
}
}