src/Security/Voter/ProductVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Product;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class ProductVoter extends Voter
  8. {
  9.     protected function supports($attribute$subject)
  10.     {
  11.         // replace with your own logic
  12.         // https://symfony.com/doc/current/security/voters.html
  13.         return in_array($attribute, ['PRODUCT_EDIT'])
  14.             && $subject instanceof Product;
  15.     }
  16.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  17.     {
  18.         $user $token->getUser();
  19.         // if the user is anonymous, do not grant access
  20.         if (!$user instanceof UserInterface) {
  21.             return false;
  22.         }
  23.         // ... (check conditions and return true to grant permission) ...
  24.         switch ($attribute) {
  25.             /** @var $subject Product */
  26.             case 'PRODUCT_EDIT':
  27.                 if ($user === $subject->getOwner()) {
  28.                     return true;
  29.                 }
  30.                 break;
  31.         }
  32.         return false;
  33.     }
  34. }