Add voter for ExportGeneration stored object authorization

Introduces `ExportGenerationStoredObjectVoter` to handle permissions for stored objects linked to export generations. Implements entity association retrieval in `ExportGenerationRepository` by adhering to `AssociatedEntityToStoredObjectInterface`.
This commit is contained in:
2025-03-13 17:23:05 +01:00
parent bd61eedfbb
commit 70ca4acafb
2 changed files with 55 additions and 1 deletions

View File

@@ -0,0 +1,41 @@
<?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\Security\Authorization\StoredObject;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
use Chill\DocStoreBundle\Security\Authorization\StoredObjectVoterInterface;
use Chill\MainBundle\Repository\ExportGenerationRepository;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
final readonly class ExportGenerationStoredObjectVoter implements StoredObjectVoterInterface
{
public function __construct(private ExportGenerationRepository $repository) {}
public function supports(StoredObjectRoleEnum $attribute, StoredObject $subject): bool
{
return null !== $this->repository->findAssociatedEntityToStoredObject($subject);
}
public function voteOnAttribute(StoredObjectRoleEnum $attribute, StoredObject $subject, TokenInterface $token): bool
{
if (StoredObjectRoleEnum::EDIT === $attribute) {
return false;
}
if (null === $generation = $this->repository->findAssociatedEntityToStoredObject($subject)) {
throw new \UnexpectedValueException('generation not found');
}
return $token->getUser()->getUserIdentifier() === $generation->getCreatedBy()->getUserIdentifier();
}
}