src/Security/CommonData/ResearchedProductVoter.php line 14

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