src/Daikin/FrontendBundle/Security/RefillCalcVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Daikin\FrontendBundle\Security;
  3. use App\Daikin\BaseBundle\Entity\CoolingMachineCalculation;
  4. use App\Daikin\BaseBundle\Entity\EmergencyStock\Order;
  5. use App\Daikin\BaseBundle\Entity\User;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. /**
  10.  * Class RefillCalcVoter
  11.  * @package App\Daikin\FrontendBundle\Security
  12.  */
  13. class RefillCalcVoter extends Voter
  14. {
  15.     const REFILLCALCULATIONVIEW 'refillcalculationview';
  16.     private AccessDecisionManagerInterface $decisionManager;
  17.     /**
  18.      * OrderVoter constructor.
  19.      * @param AccessDecisionManagerInterface $decisionManager
  20.      */
  21.     public function __construct(AccessDecisionManagerInterface $decisionManager)
  22.     {
  23.         $this->decisionManager $decisionManager;
  24.     }
  25.     protected function supports($attribute$subject): bool
  26.     {
  27. //        * @Security("is_granted('ROLE_COOLANT_REFILL')")
  28.         if ($attribute != self::REFILLCALCULATIONVIEW ) {
  29.             return false;
  30.         }
  31. //        if (!$subject instanceof CoolingMachineCalculation) {
  32. //            return false;
  33. //        }
  34.         return true;
  35.     }
  36.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  37.     {
  38.         if (!$this->decisionManager->decide($token, ['ROLE_COOLANT_REFILL'])) {
  39.             return false;
  40.         }
  41.         if (!$subject instanceof CoolingMachineCalculation) {
  42.             return true;
  43.         }
  44.         $user $token->getUser();
  45.         if (!$user instanceof User) {
  46.             return false;
  47.         }
  48.         $coolingMachineCalculation $subject;
  49.         if (!$coolingMachineCalculation->getUser() || $coolingMachineCalculation->getUser()->getCompany() !== $user->getCompany()) {
  50.             return false;
  51.         }
  52.         return true;
  53.     }
  54. }