src/Security/AvailabilityVoter.php line 43

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/AvailabilityVoter.php
  4. //------------------------------------------------------------------------------
  5. namespace App\Security;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use App\Entity\Access;
  10. use App\Entity\APIRest\AccessAPI;
  11. use App\Entity\Config\Config;
  12. use App\Entity\Config\Module;
  13. use App\Entity\HR\AccessFunction;
  14. use App\Entity\HR\Availability;
  15. use App\Entity\Security\Acl;
  16. use App\Entity\Security\AclAvailability;
  17. use App\Entity\Security\AclPermission;
  18. use App\Services\Config\ModuleTools;
  19. class AvailabilityVoter extends Voter
  20. {
  21.     //--------------------------------------------------------------------------------
  22.     // is_granted constants    
  23.     const USE = "declare_availability";
  24.     const IS_GRANTED_CONSTANTS = array(
  25.         self::USE,
  26.     );
  27.     //--------------------------------------------------------------------------------
  28.     // acl constants    
  29.     //--------------------------------------------------------------------------------
  30.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleTools)
  31.     {
  32.         $this->em $doctrine->getManager();
  33.         $this->moduleTools $moduleTools;
  34.         $this->aclRepository $this->em->getRepository(Acl::class);
  35.         $this->aclPermissionRepository $this->em->getRepository(AclPermission::class);
  36.     }
  37.     // Plan.io Task #4453 [See AccessVoter for details]
  38.     public function supportsAttribute(string $attribute): bool
  39.     {
  40.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  41.     }
  42.     protected function supports(string $attribute$subject): bool
  43.     {
  44.         // if the attribute isn't one we support, return false
  45.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  46.         {
  47.             return false;
  48.         }
  49.         // only vote on Availability objects inside this voter
  50.         if ($subject !== null && !$subject instanceof Availability)
  51.         {
  52.             return false;
  53.         }
  54.         return true;
  55.     }
  56.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  57.     {
  58.         $user $token->getUser();
  59.         // Plan.io Task #3707
  60.         if ($user instanceof AccessAPI)
  61.         {
  62.             if ($user->getAccess() === null)
  63.             {
  64.                 return false;
  65.             }
  66.             $user $user->getAccess();
  67.         }
  68.         // Plan.io Task #3707
  69.         // At this point $user is an object of Access type
  70.         // even if the $token->getUser() is AccessAPI
  71.         if (!$user instanceof Access)
  72.         {
  73.             // the user must be logged in; if not, deny access
  74.             return false;
  75.         }
  76.         // The user must have a function; if not deny access
  77.         $function $user->getFunction();
  78.         if ($function === null)        return false;
  79.         // Plan.io Task #3710 : Get current group
  80.         $currentGroup $user->getSocietyGroup();
  81.         if ($currentGroup === null)
  82.             return false;
  83.         // Access must have $hasAvailability : true
  84.         if (!$function->getHasAvailability())
  85.         {
  86.             return false;
  87.         }
  88.         // you know $subject is a Availability object, thanks to supports
  89.         /** @var Availability $availability */
  90.         $availability $subject;
  91.         // Check current group affectation
  92.         if ($subject !== null)
  93.         {
  94.             $subjectGroup $subject->getSocietyGroup();
  95.             if ($subjectGroup === null)
  96.                 return false;
  97.             if (!$currentGroup->equals($subjectGroup))
  98.                 return false;
  99.         }
  100.         switch ($attribute)
  101.         {
  102.             case self::USE:
  103.             {
  104.                 if ($subject === null)
  105.                 {
  106.                     return true;
  107.                 }
  108.                 else
  109.                 {
  110.                     return $this->isConcerned($availability$user);
  111.                 }
  112.             }            
  113.         }
  114.         throw new \LogicException('This code should not be reached!');
  115.     }
  116.     // $access is the user trying to load the resource
  117.     // $availability is the resource being loaded
  118.     private function isConcerned(Availability $availabilityAccess $access): bool
  119.     {
  120.         $owner $availability->getAccess();
  121.         if ($owner === null)
  122.         {
  123.             return false;
  124.         }
  125.         return $owner->equals($access);
  126.     }
  127. }