src/Security/VehicleVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Vehicles;
  4. use App\Entity\Users;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. class VehicleVoter extends Voter
  10. {
  11.     const ACCESS= [ 
  12.         'vehicle_view_exit_permission'     => 'vehicle_view_exit_permission',
  13.         'vehicle_edit_tech_info'           => 'vehicle_edit_tech_info',
  14.         'other_missing_parts'              => 'other_missing_parts',
  15.         'vehicle_sale_tax_fields'          => 'vehicle_sale_tax_fields'
  16.         ];
  17.     /**
  18.      * @var AccessDecisionManager|null
  19.      */
  20.     protected $decisionManager;
  21.     /**
  22.      * @var EntityManager|null
  23.      */
  24.     protected $entityManager;
  25.     /**
  26.      * DelegateVoter constructor.
  27.      * @param AccessDecisionManager|null $decisionManager
  28.      * @param EntityManager|null $entityManager
  29.      */
  30.     public function __construct(AccessDecisionManagerInterface $decisionManagerEntityManagerInterface $entityManager)
  31.     {
  32.         $this->decisionManager $decisionManager;
  33.         $this->entityManager $entityManager;
  34.     }
  35.     /**
  36.      * determines if your voter should vote on the attribute/subject combination. If you return true, 
  37.      * voteOnAttribute() will be called. Otherwise, your voter is done: some other voter should process this
  38.      */
  39.     protected function supports($attribute$subject)
  40.     {
  41.         // if the attribute isn't one we support, return false
  42.         if (!in_array($attributeself::ACCESS)) {
  43.             return false;
  44.         }
  45.         // only vote on Vehicles objects inside this voter
  46.         if ($subject && !$subject instanceof Vehicles) {
  47.             return false;
  48.         }
  49.         return true;
  50.     }
  51.     /**
  52.      * If you return true from supports(), then this method is called. Your job is simple: return true to allow access 
  53.      * and false to deny access
  54.      */
  55.     protected function voteOnAttribute($attribute$vehicleTokenInterface $token)
  56.     {
  57.         $loggedinUser $token->getUser();
  58.         $em $this->entityManager;
  59.         $vehiclesRepo $em->getRepository(Vehicles::class);
  60.         if (!$loggedinUser instanceof Users) {
  61.             return false// the user must be logged in; if not, deny access
  62.         }
  63.         // ROLE_SUPER_ADMIN can do anything! The power! Calling decide() on the AccessDecisionManager is essentially the same
  64.         // as calling isGranted() from a controller or other places (it's just a little lower-level, which is necessary for a voter).
  65.         if ($this->decisionManager->decide($token, ['ROLE_SUPER_ADMIN'])) {
  66.             return true;
  67.         }
  68.         switch ($attribute) {
  69.             case self::ACCESS['vehicle_view_exit_permission']:
  70.                 return $this->decisionManager->decide($token, ['ROLE_ADMIN']) && $vehiclesRepo->isVehicleAllowedForDelivering($vehicle);
  71.             case self::ACCESS['vehicle_edit_tech_info']:
  72.                 return $this->decisionManager->decide($token, ['ROLE_VEHICLES_TECHNICIAN']); //only vehicles' technician can edit
  73.             case self::ACCESS['other_missing_parts']:
  74.                 return $this->decisionManager->decide($token, ['ROLE_VEHICLES_TECHNICIAN']); //only vehicles' technician can edit
  75.             case self::ACCESS['vehicle_sale_tax_fields']:
  76.                 return $this->decisionManager->decide($token, ['ROLE_ACCOUNT_MANAGER']);
  77.         }
  78.         throw new \LogicException('This code should not be reached!');
  79.     }
  80. }