src/Security/ClientPlatform/MissionVoter.php line 35

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/ClientPlatform/MissionVoter.php
  4. //------------------------------------------------------------------------------
  5. namespace App\Security\ClientPlatform;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. use App\Entity\AccessClient\AccessClient;
  11. use App\Entity\Mission\Mission;
  12. use App\Services\AccessClient\AccessClientTools;
  13. use App\Services\Config\OptionConfigTools;
  14. use App\Services\LogTools;
  15. class MissionVoter extends Voter
  16. {
  17.     //--------------------------------------------------------------------------------
  18.     // is_granted constants
  19.     const VIEW "client_view_mission";
  20.     const EDIT "client_edit_mission";
  21.     const CAN_BOOK_SLOT "can_book_slot_mission";
  22.     const IS_GRANTED_CONSTANTS = array(
  23.         self::VIEW,
  24.         self::EDIT,
  25.         self::CAN_BOOK_SLOT,
  26.     );
  27.     //--------------------------------------------------------------------------------
  28.     public function __construct(Security $securityManagerRegistry $doctrineAccessClientTools $accessClientToolsLogTools $logToolsOptionConfigTools $optionConfigTools)
  29.     {
  30.         $this->security $security;
  31.         $this->em $doctrine->getManager();
  32.         $this->accessClientTools $accessClientTools;
  33.         $this->logTools $logTools;
  34.         $this->optionConfigTools $optionConfigTools;
  35.     }
  36.     protected function supports(string $attribute$subject): bool
  37.     {
  38.         // if the attribute isn't one we support, return false
  39.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  40.         {
  41.             return false;
  42.         }
  43.         // only vote on Mission objects inside this voter
  44.         if ($subject !== null && !$subject instanceof Mission)
  45.         {
  46.             return false;
  47.         }
  48.         return true;
  49.     }
  50.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  51.     {
  52.         // Only access ROLE_CLIENT
  53.         if (!$this->security->isGranted('ROLE_CLIENT'))
  54.         {
  55.             return false;
  56.         }
  57.         $user $token->getUser();
  58.         if (!$user instanceof AccessClient)
  59.         {
  60.             // the user must be logged in; if not, deny access
  61.             return false;
  62.         }
  63.         switch ($attribute)
  64.         {
  65.             case self::VIEW:
  66.                 return $this->canView($subject$user);
  67.             case self::EDIT:
  68.                 return $this->canEdit($subject$user);
  69.             case self::CAN_BOOK_SLOT:
  70.                 return $this->canBookSlot($subject$user);
  71.         }
  72.         throw new \LogicException('This code should not be reached!');
  73.     }
  74.     // Id the $object related to $accessClient ?
  75.     private function checkOwnership(Mission $objectAccessClient $accessClient)
  76.     {
  77.         $clientObjects $this->accessClientTools->getMissionsForAccessClient($accessClient);
  78.         foreach ($clientObjects as $clientObject)
  79.         {
  80.             if ($clientObject->equals($object))
  81.             {
  82.                 return true;
  83.             }
  84.         }
  85.         return false;
  86.     }
  87.     private function canView(Mission $missionAccessClient $user)
  88.     {
  89.         // Check Ownership
  90.         if ($this->checkOwnership($mission$user))
  91.         {
  92.             return true;
  93.         }
  94.         // If we are here, all hope is lost
  95.         return false;
  96.     }
  97.     private function canEdit(Mission $missionAccessClient $user)
  98.     {
  99.         // Check Ownership
  100.         if ($this->checkOwnership($mission$user))
  101.         {
  102.             return true;
  103.         }
  104.         // If we are here, all hope is lost
  105.         return false;
  106.     }
  107.     private function canBookSlot(Mission $missionAccessClient $user)
  108.     {
  109.         if (!$this->canView($mission$user))
  110.         {
  111.             return false;
  112.         }
  113.         // NOTE: Use SocietyGroupOwner, we want the permission from the current mission manager (SocietyGroup) 
  114.         $societyGroupOwner $mission->getSocietyGroupOwner();
  115.         if (!$this->optionConfigTools->isActive_OnlineBooking($societyGroupOwner))
  116.         {
  117.             return false;
  118.         }
  119.         if (!$this->optionConfigTools->isActive_planningOptimisation($societyGroupOwner))
  120.         {
  121.             return false;
  122.         }
  123.         return true;
  124.     }
  125. }