src/Security/VehicleVoter.php line 11

  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 \Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface $decisionManager;
  21.     /**
  22.      * @var EntityManager|null
  23.      */
  24.     protected \Doctrine\ORM\EntityManagerInterface $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): bool
  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.         return !($subject && !$subject instanceof Vehicles);
  47.     }
  48.     /**
  49.      * If you return true from supports(), then this method is called. Your job is simple: return true to allow access
  50.      * and false to deny access
  51.      */
  52.     protected function voteOnAttribute($attribute$vehicleTokenInterface $token): bool
  53.     {
  54.         $loggedinUser $token->getUser();
  55.         $em $this->entityManager;
  56.         $vehiclesRepo $em->getRepository(Vehicles::class);
  57.         if (!$loggedinUser instanceof Users) {
  58.             return false// the user must be logged in; if not, deny access
  59.         }
  60.         // ROLE_SUPER_ADMIN can do anything! The power! Calling decide() on the AccessDecisionManager is essentially the same
  61.         // as calling isGranted() from a controller or other places (it's just a little lower-level, which is necessary for a voter).
  62.         if ($this->decisionManager->decide($token, ['ROLE_SUPER_ADMIN'])) {
  63.             return true;
  64.         }
  65.         switch ($attribute) {
  66.             case self::ACCESS['vehicle_view_exit_permission']:
  67.                 return $this->decisionManager->decide($token, ['ROLE_ADMIN']) && $vehiclesRepo->isVehicleAllowedForDelivering($vehicle);
  68.             case self::ACCESS['vehicle_edit_tech_info']: //only vehicles' technician can edit
  69.             case self::ACCESS['other_missing_parts']:
  70.                 return $this->decisionManager->decide($token, ['ROLE_VEHICLES_TECHNICIAN']); //only vehicles' technician can edit
  71.             case self::ACCESS['vehicle_sale_tax_fields']:
  72.                 return $this->decisionManager->decide($token, ['ROLE_ACCOUNT_MANAGER']);
  73.         }
  74.         throw new \LogicException('This code should not be reached!');
  75.     }
  76. }