Add SHARE permission to SavedExportVoter with tests

Introduced the SHARE attribute and updated SavedExportVoter to handle it. Added new functionality to check if a SavedExport is shared with a specific user and included corresponding unit tests for both the voter and entity behaviors.
This commit is contained in:
2025-04-14 10:59:47 +02:00
parent 3d9b9ea672
commit 420dd4f868
3 changed files with 167 additions and 0 deletions

View File

@@ -12,8 +12,11 @@ declare(strict_types=1);
namespace Chill\MainBundle\Security\Authorization;
use Chill\MainBundle\Entity\SavedExport;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Export\ExportManager;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class SavedExportVoter extends Voter
{
@@ -23,12 +26,17 @@ class SavedExportVoter extends Voter
final public const GENERATE = 'CHLL_MAIN_EXPORT_SAVED_GENERATE';
final public const SHARE = 'CHLL_MAIN_EXPORT_SAVED_SHARE';
private const ALL = [
self::DELETE,
self::EDIT,
self::GENERATE,
self::SHARE,
];
public function __construct(private ExportManager $exportManager, private Security $security) {}
protected function supports($attribute, $subject): bool
{
return $subject instanceof SavedExport && \in_array($attribute, self::ALL, true);
@@ -49,4 +57,10 @@ class SavedExportVoter extends Voter
default => throw new \UnexpectedValueException('attribute not supported: '.$attribute),
};
}
private function canUserGenerate(User $user, SavedExport $savedExport): bool
{
return ($savedExport->getUser() === $user || $savedExport->isSharedWithUser($user))
&& $this->security->isGranted(ChillExportVoter::EXPORT, $this->exportManager->getExport($savedExport->getExportAlias()));
}
}