src/Voters/ModuleVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Voters;
  3. use App\Model\Module\Module;
  4. use App\Security\SecurityConfig;
  5. use App\Security\User;
  6. use LogicException;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class ModuleVoter extends Voter
  10. {
  11.     /**
  12.      * @param $attribute
  13.      * @param $subject
  14.      *
  15.      * @return bool
  16.      */
  17.     protected function supports($attribute$subject): bool
  18.     {
  19.         return in_array($attribute, [
  20.             Module::MODULE_PRODUCT,
  21.             Module::MODULE_QUOTE_LINE,
  22.             Module::MODULE_PRODUCT_CUSTOMIZE,
  23.         ], true);
  24.     }
  25.     /**
  26.      * @param $attribute
  27.      * @param $subject
  28.      * @param TokenInterface $token
  29.      *
  30.      * @return bool
  31.      *
  32.      * @throws LogicException
  33.      */
  34.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  35.     {
  36.         $user $token->getUser();
  37.         if (!$user instanceof User) {
  38.             return false;
  39.         }
  40.         switch ($attribute) {
  41.             case SecurityConfig::MODULE_QUOTE_LINE:
  42.             case SecurityConfig::MODULE_PRODUCT:
  43.             case SecurityConfig::MODULE_PRODUCT_CUSTOMIZE:
  44.                 return in_array($attribute$user->getRoles(), true);
  45.         }
  46.         throw new LogicException('This should never happen');
  47.     }
  48. }