src/Security/ClientPlatform/DevisVoter.php line 34

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/ClientPlatform/DevisVoter.php
  4. //------------------------------------------------------------------------------
  5. namespace App\Security\ClientPlatform;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use App\Entity\AccessClient\AccessClient;
  11. use App\Entity\Platform\Devis\Devis;
  12. use App\Services\AccessClient\AccessClientTools;
  13. use App\Services\LogTools;
  14. use App\Services\Platform\DevisTools;
  15. class DevisVoter extends Voter
  16. {
  17.     //--------------------------------------------------------------------------------
  18.     // is_granted constants
  19.     const REFUSE "client_platform_refuse_devis";
  20.     const IS_GRANTED_CONSTANTS = array(
  21.         self::REFUSE,
  22.     );
  23.     //--------------------------------------------------------------------------------
  24.     public function __construct(Security $securityManagerRegistry $doctrineLogTools $logTools,
  25.                 AccessClientTools $accessClientToolsDevisTools $devisTools)
  26.     {
  27.         $this->security $security;
  28.         $this->em $doctrine->getManager();
  29.         $this->accessClientTools $accessClientTools;
  30.         $this->devisTools $devisTools;
  31.         $this->logTools $logTools;
  32.     }
  33.     protected function supports(string $attribute$subject): bool
  34.     {
  35.         // if the attribute isn't one we support, return false
  36.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  37.         {
  38.             return false;
  39.         }
  40.         // only vote on Devis objects inside this voter
  41.         if ($subject !== null && !$subject instanceof Devis)
  42.         {
  43.             return false;
  44.         }
  45.         return true;
  46.     }
  47.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  48.     {
  49.         // Only access ROLE_CLIENT
  50.         if (!$this->security->isGranted('ROLE_CLIENT'))
  51.         {
  52.             return false;
  53.         }
  54.         $user $token->getUser();
  55.         if (!$user instanceof AccessClient)
  56.         {
  57.             // the user must be logged in; if not, deny access
  58.             return false;
  59.         }
  60.         switch ($attribute)
  61.         {
  62.             case self::REFUSE:
  63.                 return $this->canRefuse($subject$user);
  64.         }
  65.         throw new \LogicException('This code should not be reached!');
  66.     }
  67.     // Id the $object related to $accessClient ?
  68.     private function checkOwnership(Devis $objectAccessClient $accessClient)
  69.     {
  70.         $receiver $object->getReceiver();
  71.         if ($receiver === null)
  72.         {
  73.             $mission $object->getMission();
  74.             if ($mission === null)
  75.             {
  76.                 return false;
  77.             }
  78.             $receiver $mission->getReceiver();
  79.             if ($receiver === null)
  80.             {
  81.                 return false;
  82.             }
  83.         }
  84.         if (!$this->accessClientTools->areLinked($receiver$accessClient))
  85.         {
  86.             return false;
  87.         }
  88.         return true;
  89.     }
  90.     private function canRefuse(Devis $devisAccessClient $user)
  91.     {
  92.         // Plan.io Task #4493
  93.         // Deny refusing Ikea Devis
  94.         if ($devis->hasIkeaTemplate())
  95.         {
  96.             return false;
  97.         }
  98.         // Check Ownership
  99.         if ($this->checkOwnership($devis$user))
  100.         {
  101.             return true;
  102.         }
  103.         // If we are here, all hope is lost
  104.         return false;
  105.     }
  106. }