Add WorkflowDocumentService and use in StoredObject voters

A WorkflowDocumentService was created that can be injected\
in context-specific StoredObject voters that need to check whether\
the document in question is attached to a workflow.
This commit is contained in:
Julie Lenaerts 2024-06-20 17:32:09 +02:00
parent 7c03a25f1a
commit 4607c36b57
2 changed files with 56 additions and 10 deletions

View File

@ -2,13 +2,15 @@
namespace ChillDocStoreBundle\Security\Authorization; namespace ChillDocStoreBundle\Security\Authorization;
use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument;
use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Repository\AccompanyingCourseDocumentRepository; use Chill\DocStoreBundle\Repository\AccompanyingCourseDocumentRepository;
use Chill\DocStoreBundle\Security\Authorization\AccompanyingCourseDocumentVoter; use Chill\DocStoreBundle\Security\Authorization\AccompanyingCourseDocumentVoter;
use Chill\DocStoreBundle\Security\Authorization\StoredObjectVoterInterface; use Chill\DocStoreBundle\Security\Authorization\StoredObjectVoterInterface;
use Chill\DocStoreBundle\Service\WorkflowDocumentService;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Security;
class AccompanyingCourseDocumentStoredObjectVoter implements StoredObjectVoterInterface class AccompanyingCourseDocumentStoredObjectVoter implements StoredObjectVoterInterface
{ {
@ -16,15 +18,15 @@ class AccompanyingCourseDocumentStoredObjectVoter implements StoredObjectVoterIn
public function __construct( public function __construct(
private readonly AccompanyingCourseDocumentRepository $repository, private readonly AccompanyingCourseDocumentRepository $repository,
private readonly Security $security, private readonly AccompanyingCourseDocumentVoter $accompanyingCourseDocumentVoter,
private readonly AccompanyingCourseDocumentVoter $accompanyingCourseDocumentVoter private readonly WorkflowDocumentService $workflowDocumentService
){ ){
} }
public function supports(string $attribute, StoredObject $subject): bool public function supports(string $attribute, StoredObject $subject): bool
{ {
// check if the stored object is linked to an AccompanyingCourseDocument // check if the stored object is linked to an AccompanyingCourseDocument
return !empty($this->repository->findLinkedCourseDocument($subject->getId())); return $this->repository->findLinkedCourseDocument($subject) instanceof AccompanyingCourseDocument;
} }
public function voteOnAttribute(string $attribute, StoredObject $subject, TokenInterface $token): bool public function voteOnAttribute(string $attribute, StoredObject $subject, TokenInterface $token): bool
@ -34,17 +36,26 @@ class AccompanyingCourseDocumentStoredObjectVoter implements StoredObjectVoterIn
} }
// Retrieve the related accompanying course document // Retrieve the related accompanying course document
$accompanyingCourseDocument = $this->repository->findLinkedCourseDocument($subject->getId()); $accompanyingCourseDocument = $this->repository->findLinkedCourseDocument($subject);
// Determine the attribute to pass to AccompanyingCourseDocumentVoter // Determine the attribute to pass to AccompanyingCourseDocumentVoter
$voterAttribute = ($attribute === self::SEE_AND_EDIT) ? AccompanyingCourseDocumentVoter::UPDATE : AccompanyingCourseDocumentVoter::SEE_DETAILS; $voterAttribute = match($attribute) {
self::SEE_AND_EDIT => AccompanyingCourseDocumentVoter::UPDATE,
default => AccompanyingCourseDocumentVoter::SEE_DETAILS,
};
// Check access using AccompanyingCourseDocumentVoter // Check access using AccompanyingCourseDocumentVoter
if ($this->accompanyingCourseDocumentVoter->voteOnAttribute($voterAttribute, $accompanyingCourseDocument, $token)) { if (false === $this->accompanyingCourseDocumentVoter->voteOnAttribute($voterAttribute, $accompanyingCourseDocument, $token)) {
// TODO implement logic to check for associated workflow
return true;
} else {
return false; return false;
} }
// Check if entity is related to a workflow, if so, check if user can apply transition
$relatedWorkflow = $this->workflowDocumentService->getRelatedWorkflow($accompanyingCourseDocument);
if ($relatedWorkflow instanceof EntityWorkflow){
return $this->workflowDocumentService->canApplyTransition($relatedWorkflow);
}
return true;
} }
} }

View File

@ -0,0 +1,35 @@
<?php
namespace Chill\DocStoreBundle\Service;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Symfony\Component\Security\Core\Security;
class WorkflowDocumentService
{
public function __construct(private readonly Security $security, private readonly EntityWorkflowRepository $repository)
{
}
public function getRelatedWorkflow($entity): ?EntityWorkflow
{
return $this->repository->findByRelatedEntity(get_class($entity), $entity->getId());
}
public function canApplyTransition(EntityWorkflow $entityWorkflow): bool
{
if ($entityWorkflow->isFinal()) {
return false;
}
$currentUser = $this->security->getUser();
if ($entityWorkflow->getCurrentStep()->getAllDestUser()->contains($currentUser)) {
return true;
}
return false;
}
}