src/Security/InterventionTaskVoter.php line 19

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/InterventionTaskVoter.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\Config\Config;
  11. use App\Entity\Config\Module;
  12. use App\Entity\HR\AccessFunction;
  13. use App\Entity\Security\Acl;
  14. use App\Entity\Security\AclPermission;
  15. use App\Services\Config\ModuleTools;
  16. class InterventionTaskVoter extends Voter
  17. {
  18.     //--------------------------------------------------------------------------------
  19.     // is_granted constants
  20.     const IS_ACTIVE "itask_is_active";
  21.     const ITASK_LISTING "list_itasks";
  22.     const IS_GRANTED_CONSTANTS = array(
  23.         self::IS_ACTIVE,
  24.         self::ITASK_LISTING,
  25.     );
  26.     //--------------------------------------------------------------------------------
  27.     // acl constants
  28.     const ACL_PERM_ITASK_LISTING "itask_list";
  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.     
  43.     protected function supports(string $attribute$subject null): bool
  44.     {
  45.         // if the attribute isn't one we support, return false
  46.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  47.         {
  48.             return false;
  49.         }
  50.         return true;
  51.     }
  52.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  53.     {
  54.         $user $token->getUser();
  55.         if (!$user instanceof Access)
  56.         {
  57.             // the user must be logged in; if not, deny access
  58.             return false;
  59.         }
  60.         // The user must have a function; if not deny access
  61.         $function $user->getFunction();
  62.         if ($function === null)        return false;
  63.         // Plan.io Task #3710 : Get current group
  64.         $currentGroup $user->getSocietyGroup();
  65.         if ($currentGroup === null)
  66.             return false;
  67.         // Module activated ?
  68.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_ITASK))
  69.         {
  70.             return false;
  71.         }
  72.         // Required modules : Devis, Invoice, Planning
  73.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_DEVIS))
  74.         {
  75.             return false;
  76.         }
  77.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_INVOICE))
  78.         {
  79.             return false;
  80.         }
  81.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_PLANNING))
  82.         {
  83.             return false;
  84.         }
  85.         switch ($attribute)
  86.         {
  87.             case self::IS_ACTIVE:
  88.                 return true;
  89.             case self::ITASK_LISTING:
  90.                 return $this->canListITasks($user$function);
  91.         }
  92.         throw new \LogicException('This code should not be reached!');
  93.     }
  94.     private function canListITasks(Access $userAccessFunction $function)
  95.     {
  96.         // Get AclPermission
  97.         $aclPerm $this->aclPermissionRepository->findOneByName(self::ACL_PERM_ITASK_LISTING);
  98.         if ($aclPerm === null)        return false;
  99.         // Get Acl
  100.         $acl $this->aclRepository->findOneBy(array(
  101.             'function'        =>    $function,
  102.             'permission'    =>    $aclPerm
  103.         ));
  104.         if ($acl === null)        return false;
  105.         // Since only one acl type can exist
  106.         // we can return the result of the acl_permission
  107.         return $acl->getValue();
  108.     }
  109. }