src/Security/Copywriter/OrderVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\Copywriter;
  3. use App\Entity\Copywriter\Order;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. /**
  7.  * Class CopywriterVoter
  8.  *
  9.  * @package App\Security\Copywriter
  10.  */
  11. class OrderVoter extends Voter
  12. {
  13.     /**
  14.      * constant edit
  15.      */
  16.     public const EDIT   'edit';
  17.     /**
  18.      * constant view
  19.      */
  20.     public const VIEW   'view';
  21.     /**
  22.      * constant delete
  23.      */
  24.     public const DELETE 'delete';
  25.     /**
  26.      * base voter actions
  27.      */
  28.     private const ATTRIBUTES = [
  29.         self::EDIT,
  30.         self::VIEW,
  31.         self::DELETE
  32.     ];
  33.     /**
  34.      * @param string $attribute
  35.      * @param mixed  $subject
  36.      *
  37.      * @return bool
  38.      */
  39.     protected function supports($attribute$subject): bool
  40.     {
  41.         return
  42.             $subject instanceof Order &&
  43.             in_array($attributeself::ATTRIBUTES);
  44.     }
  45.     /**
  46.      * @param string         $attribute
  47.      * @param Order          $order
  48.      * @param TokenInterface $token
  49.      *
  50.      * @return bool
  51.      * @throws \LogicException
  52.      */
  53.     protected function voteOnAttribute($attribute$orderTokenInterface $token): bool
  54.     {
  55.         switch ($attribute) {
  56.             case self::EDIT:
  57.             case self::DELETE:
  58.             case self::VIEW:
  59.                 return $order->getUser() == $token->getUser();
  60.                 break;
  61.         }
  62.         throw new \LogicException('Invalid attribute: ' $attribute);
  63.     }
  64. }