src/Voters/ProductVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Voters;
  3. use App\Security\SecurityConfig;
  4. use App\Security\User;
  5. use LogicException;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. class ProductVoter extends Voter
  10. {
  11.     const PRODUCT_MANAGE_LIST 'product_manage_list';
  12.     const PRODUCT_SHOW_LIST 'product_show_list';
  13.     const PRODUCT_ADD_EDIT 'product_add_edit';
  14.     const CATEGORY_ADD_EDIT 'category_add_edit';
  15.     const PRODUCT_MANAGE_FIELDS 'product_manage_fields';
  16.     /**
  17.      * @var Security
  18.      */
  19.     private $security;
  20.     public function __construct(Security $security)
  21.     {
  22.         $this->security $security;
  23.     }
  24.     /**
  25.      * @param $attribute
  26.      * @param $subject
  27.      *
  28.      * @return bool
  29.      */
  30.     protected function supports($attribute$subject): bool
  31.     {
  32.         return in_array($attribute, [
  33.             self::PRODUCT_MANAGE_LIST,
  34.             self::PRODUCT_SHOW_LIST,
  35.             self::PRODUCT_ADD_EDIT,
  36.             self::CATEGORY_ADD_EDIT,
  37.             self::PRODUCT_MANAGE_FIELDS,
  38.         ], true);
  39.     }
  40.     /**
  41.      * @param $attribute
  42.      * @param $subject
  43.      * @param TokenInterface $token
  44.      *
  45.      * @return bool
  46.      *
  47.      * @throws LogicException
  48.      */
  49.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  50.     {
  51.         $user $token->getUser();
  52.         if (!$user instanceof User) {
  53.             return false;
  54.         }
  55.         switch ($attribute) {
  56.             case self::PRODUCT_MANAGE_LIST:
  57.                 return $this->canProductManageList();
  58.             case self::PRODUCT_SHOW_LIST:
  59.                 return $this->canProductShowList();
  60.             case self::PRODUCT_ADD_EDIT:
  61.                 return $this->canProductAddEdit();
  62.             case self::CATEGORY_ADD_EDIT:
  63.                 return $this->canCategoryAddEdit();
  64.             case self::PRODUCT_MANAGE_FIELDS:
  65.                 return $this->canProductManageFields();
  66.         }
  67.         throw new LogicException('This should never happen');
  68.     }
  69.     private function canProductManageList(): bool
  70.     {
  71.         return $this->canAccessModuleProduct();
  72.     }
  73.     private function canProductShowList(): bool
  74.     {
  75.         return $this->canAccessModuleProduct();
  76.     }
  77.     private function canProductAddEdit(): bool
  78.     {
  79.         return $this->canAccessModuleProduct();
  80.     }
  81.     private function canCategoryAddEdit(): bool
  82.     {
  83.         return $this->canAccessModuleProduct();
  84.     }
  85.     private function canProductManageFields(): bool
  86.     {
  87.         return $this->canAccessModuleProduct();
  88.     }
  89.     private function canAccessModuleProduct(): bool
  90.     {
  91.         return $this->security->isGranted(SecurityConfig::MODULE_PRODUCT);
  92.     }
  93. }