Use the new VoterHelper in voters

This commit is contained in:
Jan Böhmer 2023-08-28 22:00:25 +02:00
parent fc6643bd6f
commit 6be55d1837
16 changed files with 146 additions and 162 deletions

View file

@ -41,23 +41,25 @@ declare(strict_types=1);
namespace App\Security\Voter;
use App\Services\UserSystem\VoterHelper;
use Symfony\Bundle\SecurityBundle\Security;
use App\Entity\Parts\Part;
use App\Entity\PriceInformations\Orderdetail;
use App\Entity\UserSystem\User;
use App\Services\UserSystem\PermissionManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class OrderdetailVoter extends ExtendedVoter
final class OrderdetailVoter extends Voter
{
public function __construct(PermissionManager $resolver, EntityManagerInterface $entityManager, protected Security $security)
public function __construct(private readonly Security $security, private readonly VoterHelper $helper)
{
parent::__construct($resolver, $entityManager);
}
protected const ALLOWED_PERMS = ['read', 'edit', 'create', 'delete', 'show_history', 'revert_element'];
protected function voteOnUser(string $attribute, $subject, User $user): bool
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
if (! is_a($subject, Orderdetail::class, true)) {
throw new \RuntimeException('This voter can only handle Orderdetail objects!');
@ -73,7 +75,7 @@ class OrderdetailVoter extends ExtendedVoter
//If we have no part associated use the generic part permission
if (is_string($subject) || !$subject->getPart() instanceof Part) {
return $this->resolver->inherit($user, 'parts', $operation) ?? false;
return $this->helper->isGranted($token, 'parts', $operation);
}
//Otherwise vote on the part