src/Security/Authorization/Voter/PriceGroupVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Authorization\Voter;
  3. use App\Entity\CompanySalesPriceGroup;
  4. use App\Entity\PriceGroup;
  5. use App\Entity\User;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class PriceGroupVoter extends Voter
  9. {
  10.     const CREATE  'create';
  11.     const READ    'read';
  12.     const UPDATE  'update';
  13.     const DELETE  'delete';
  14.     /**
  15.      * {@inheritDoc}
  16.      */
  17.     protected function supports($attribute$subject)
  18.     {
  19.         $attributes = [
  20.             self::CREATE,
  21.             self::READ,
  22.             self::UPDATE,
  23.             self::DELETE,
  24.         ];
  25.         if (!in_array($attribute$attributes)) {
  26.             return false;
  27.         }
  28.         if (!$subject instanceof PriceGroup) {
  29.             return false;
  30.         }
  31.         return true;
  32.     }
  33.     /**
  34.      * {@inheritDoc}
  35.      */
  36.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  37.     {
  38.         $user $token->getUser();
  39.         if (!$user instanceof User) {
  40.             return false;
  41.         }
  42.         if (!$subject instanceof CompanySalesPriceGroup) {
  43.             return false;
  44.         }
  45.         switch ($attribute) {
  46.             case self::CREATE:
  47.             case self::UPDATE:
  48.             case self::DELETE:
  49.                 if ($user->isSuperAdmin()) {
  50.                     return true;
  51.                 }
  52.                 break;
  53.             case self::READ:
  54.                 return true;
  55.                 break;
  56.         }
  57.         return false;
  58.     }
  59. }