src/Security/Translator/OrderVoter.php line 14

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