src/Security/Voter/PurchaseVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Purchase;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. class PurchaseVoter extends Voter
  8. {
  9.     protected function supports($attribute$subject): bool
  10.     {
  11.         $supportsAttribute $attribute == 'delete';
  12.         $supportsSubject $subject instanceof Purchase;
  13.         return $supportsAttribute && $supportsSubject;
  14.     }
  15.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  16.     {
  17.         $authenticatedUser $token->getUser();
  18.         if (!$authenticatedUser instanceof User) return false;
  19.         /** @var Purchase $purchase */
  20.         $purchase $subject;
  21.         return $purchase->getTransactions()->isEmpty();
  22.     }
  23. }