src/Security/TaskItemVoter.php line 48

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/TaskItemVoter.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\Security\Acl;
  15. use App\Entity\Security\AclPermission;
  16. use App\Services\Config\ModuleTools;
  17. class TaskItemVoter extends Voter
  18. {
  19.     //--------------------------------------------------------------------------------
  20.     // is_granted / acl constants
  21.     const ACL_PERM_DEVIS_PDF "item_planning_task_devis_pdf";
  22.     const ACL_PERM_DEVIS_TOTAL "item_planning_task_devis_total";
  23.     // const ACL_PERM_FILES = "item_planning_task_client_attachments";
  24.     // const ACL_PERM_FORMS = "item_planning_task_client_webapp_forms";
  25.     // const ACL_PERM_IA = "item_planning_task_intervention_address";
  26.     // const ACL_PERM_TYPE = "item_planning_task_type";
  27.     // const ACL_PERM_STATUS = "item_planning_task_status";
  28.     // const ACL_PERM_INFO = "item_planning_task_info";
  29.     // const ACL_PERM_DEVIS_FILES = "item_planning_task_devis_attachments";
  30.     const ACL_PERM_TH_DURATION "item_planning_task_theoretical_duration";
  31.     const IS_GRANTED_CONSTANTS = array(
  32.         self::ACL_PERM_DEVIS_PDF,
  33.         self::ACL_PERM_DEVIS_TOTAL,
  34.         self::ACL_PERM_TH_DURATION,
  35.     );
  36.     //--------------------------------------------------------------------------------
  37.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleTools)
  38.     {
  39.         $this->em $doctrine->getManager();
  40.         $this->moduleTools $moduleTools;
  41.         $this->aclRepository $this->em->getRepository(Acl::class);
  42.         $this->aclPermissionRepository $this->em->getRepository(AclPermission::class);
  43.     }
  44.     // Plan.io Task #4453 [See AccessVoter for details]
  45.     public function supportsAttribute(string $attribute): bool
  46.     {
  47.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  48.     }
  49.     protected function supports(string $attribute$subject null): bool
  50.     {
  51.         // if the attribute isn't one we support, return false
  52.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  53.         {
  54.             return false;
  55.         }
  56.         return true;
  57.     }
  58.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  59.     {
  60.         $user $token->getUser();
  61.         // Plan.io Task #3707
  62.         if ($user instanceof AccessAPI)
  63.         {
  64.             if ($user->getAccess() === null)
  65.             {
  66.                 return false;
  67.             }
  68.             $user $user->getAccess();
  69.         }
  70.         // Plan.io Task #3707
  71.         // At this point $user is an object of Access type
  72.         // even if the $token->getUser() is AccessAPI
  73.         if (!$user instanceof Access)
  74.         {
  75.             // the user must be logged in; if not, deny access
  76.             return false;
  77.         }
  78.         // The user must have a function; if not deny access
  79.         $function $user->getFunction();
  80.         if ($function === null)        return false;
  81.         // Plan.io Task #3710 : Get current group
  82.         $currentGroup $user->getSocietyGroup();
  83.         if ($currentGroup === null)
  84.             return false;
  85.         // Module activated ?
  86.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_PLANNING))
  87.         {
  88.             return false;
  89.         }
  90.         return $this->canViewItem($attribute$function);
  91.         throw new \LogicException('This code should not be reached!');
  92.     }
  93.     private function canViewItem($attributeAccessFunction $function)
  94.     {
  95.         // Get Acl_Permission
  96.         $aclPerm $this->aclPermissionRepository->findOneByName($attribute);
  97.         if ($aclPerm === null)        return false;
  98.         // Get Acl
  99.         $acl $this->aclRepository->findOneBy(array(
  100.             'function'        =>    $function,
  101.             'permission'    =>    $aclPerm
  102.         ));
  103.         if ($acl === null)        return false;
  104.         return $acl->getValue();
  105.     }
  106. }