mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-25 00:53:48 +00:00
Implement voting logic: separation of concerns
A separate AccompanyingCourseDocumentStoredObjectVoter was\ created to handle the specific access to a Stored object\ related to an Accompanying Course Document.
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace ChillDocStoreBundle\Security\Authorization;
|
||||
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\DocStoreBundle\Repository\AccompanyingCourseDocumentRepository;
|
||||
use Chill\DocStoreBundle\Security\Authorization\AccompanyingCourseDocumentVoter;
|
||||
use Chill\DocStoreBundle\Security\Authorization\StoredObjectVoterInterface;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
|
||||
class AccompanyingCourseDocumentStoredObjectVoter implements StoredObjectVoterInterface
|
||||
{
|
||||
final public const SEE_AND_EDIT = 'CHILL_ACCOMPANYING_COURSE_DOCUMENT_STORED_OBJECT_SEE_EDIT';
|
||||
|
||||
public function __construct(
|
||||
private readonly AccompanyingCourseDocumentRepository $repository,
|
||||
private readonly Security $security,
|
||||
private readonly AccompanyingCourseDocumentVoter $accompanyingCourseDocumentVoter
|
||||
){
|
||||
}
|
||||
|
||||
public function supports(string $attribute, StoredObject $subject): bool
|
||||
{
|
||||
// check if the stored object is linked to an AccompanyingCourseDocument
|
||||
return !empty($this->repository->findLinkedCourseDocument($subject->getId()));
|
||||
}
|
||||
|
||||
public function voteOnAttribute(string $attribute, StoredObject $subject, TokenInterface $token): bool
|
||||
{
|
||||
if (!$token->getUser() instanceof User) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Retrieve the related accompanying course document
|
||||
$accompanyingCourseDocument = $this->repository->findLinkedCourseDocument($subject->getId());
|
||||
|
||||
// Determine the attribute to pass to AccompanyingCourseDocumentVoter
|
||||
$voterAttribute = ($attribute === self::SEE_AND_EDIT) ? AccompanyingCourseDocumentVoter::UPDATE : AccompanyingCourseDocumentVoter::SEE_DETAILS;
|
||||
|
||||
// Check access using AccompanyingCourseDocumentVoter
|
||||
if ($this->accompanyingCourseDocumentVoter->voteOnAttribute($voterAttribute, $accompanyingCourseDocument, $token)) {
|
||||
// TODO implement logic to check for associated workflow
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user