src/Security/InvoiceProformaVoter.php line 51

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/InvoiceProformaVoter.php
  4. //------------------------------------------------------------------------------
  5. namespace App\Security;
  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 App\Entity\Access;
  10. use App\Entity\APIRest\AccessAPI;
  11. use App\Entity\Config\Module;
  12. use App\Entity\HR\AccessFunction;
  13. use App\Entity\Platform\Devis\Devis;
  14. use App\Entity\Security\Acl;
  15. use App\Entity\Security\AclPermission;
  16. use App\Services\LogTools;
  17. use App\Services\Config\ModuleTools;
  18. use App\Services\Config\OptionConfigTools;
  19. // Task plan.io #3892
  20. class InvoiceProformaVoter extends Voter
  21. {
  22.     // For now manager = author (both)
  23.     //--------------------------------------------------------------------------------
  24.     // is_granted constants
  25.     const IS_ACTIVE "invoice_proforma_is_active";
  26.     const VIEW_PDF "view_pdf_invoice_proforma";
  27.     const IS_GRANTED_CONSTANTS = array(
  28.         self::IS_ACTIVE,
  29.         self::VIEW_PDF
  30.     );
  31.     //--------------------------------------------------------------------------------
  32.     // acl constants
  33.     const ACL_PERM_VIEW_PDF "invoice_proforma_view_pdf";
  34.     const ACL_PERM_VIEW_PDF_SOCIETY "invoice_proforma_view_pdf_society";
  35.     const ACL_PERM_VIEW_PDF_MANAGER "invoice_proforma_view_pdf_manager";
  36.     const ACL_PERM_VIEW_PDF_CLIENT_MANAGER "invoice_proforma_view_pdf_ind_manager";
  37.     //--------------------------------------------------------------------------------
  38.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleToolsOptionConfigTools $optionConfigToolsLogTools $logTools)
  39.     {
  40.         $this->em $doctrine->getManager();
  41.         $this->moduleTools $moduleTools;
  42.         $this->optionConfigTools $optionConfigTools;
  43.         $this->logTools $logTools;
  44.         $this->aclRepository $this->em->getRepository(Acl::class);
  45.         $this->aclPermissionRepository $this->em->getRepository(AclPermission::class);
  46.     }
  47.     // Plan.io Task #4453 [See AccessVoter for details]
  48.     public function supportsAttribute(string $attribute): bool
  49.     {
  50.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  51.     }
  52.     
  53.     protected function supports(string $attribute$subject): bool
  54.     {
  55.         // if the attribute isn't one we support, return false
  56.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  57.         {
  58.             return false;
  59.         }
  60.         // Only vote on Devis and Mission objects inside this voter
  61.         if ($subject !== null && !($subject instanceof Devis))
  62.         {
  63.             return false;
  64.         }
  65.         return true;
  66.     }
  67.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  68.     {
  69.         $user $token->getUser();
  70.         // Plan.io Task #3707
  71.         if ($user instanceof AccessAPI)
  72.         {
  73.             if ($user->getAccess() === null)
  74.             {
  75.                 return false;
  76.             }
  77.             $user $user->getAccess();
  78.         }
  79.         // Plan.io Task #3707
  80.         // At this point $user is an object of Access type
  81.         // even if the $token->getUser() is AccessAPI
  82.         if (!$user instanceof Access)
  83.         {
  84.             // the user must be logged in; if not, deny access
  85.             return false;
  86.         }
  87.         // The user must have a function; if not deny access
  88.         $function $user->getFunction();
  89.         if ($function === null)        return false;
  90.         // Plan.io Task #3710 : Get current group
  91.         $currentGroup $user->getSocietyGroup();
  92.         if ($currentGroup === null)
  93.             return false;
  94.         $this->currentGroup $currentGroup;
  95.         // Module activated ?
  96.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_DEVIS))
  97.         {
  98.             return false;
  99.         }
  100.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_INVOICE))
  101.         {
  102.             return false;
  103.         }
  104.         /** @var Devis */
  105.         $devis $subject;
  106.         switch ($attribute)
  107.         {
  108.             case self::IS_ACTIVE:
  109.                 return true;
  110.             case self::VIEW_PDF:
  111.             {
  112.                 if ($devis !== null)
  113.                 {
  114.                     return $this->viewPdf($devis$user$function);
  115.                 }
  116.                 else
  117.                 {
  118.                     return false;
  119.                 }
  120.             }
  121.         }
  122.         throw new \LogicException('This code should not be reached!');
  123.     }
  124.     // $access is the user trying to load the resource
  125.     // $devis is the resource being loaded
  126.     // Check if the Society of the resource
  127.     // belongs to the societies of the $access
  128.     private function checkSociety(Devis $devisAccess $access)
  129.     {
  130.         // Get all the societies of the access
  131.         $societies $access->getSocieties();
  132.         // Get the Society of the Devis
  133.         $devisSociety $devis->getSociety();
  134.         if ($devisSociety === null)
  135.             return false;
  136.         $found false;
  137.         foreach ($societies as $society)
  138.         {
  139.             if ($society->getId() == $devisSociety->getId())
  140.             {
  141.                 $found true;
  142.                 break;
  143.             }
  144.         }
  145.         return $found;
  146.     }
  147.     // Check if the $access is the manager / author of the $devis
  148.     private function checkManager(Devis $devisAccess $access)
  149.     {
  150.         // Get manager
  151.         $manager $devis->getManager();
  152.         $author $devis->getAuthor();
  153.         if ($manager === null && $author === null)
  154.             return false;
  155.         if ($manager !== null)
  156.             if ($manager->getId() === $access->getId())
  157.                 return true;
  158.         if ($author !== null)
  159.             if ($author->getId() === $access->getId())
  160.                 return true;
  161.         return false;
  162.     }
  163.     // Check if the $access is the manager of the $devis->getIndividual()
  164.     private function checkClientManager(Devis $devisAccess $access)
  165.     {
  166.         // Get Client
  167.         $client $devis->getReceiver();
  168.         if ($client === null)
  169.             return false;
  170.         if ($client->getIndividual() !== null)
  171.         {
  172.             // Only Individuals have managers
  173.             // Get manager
  174.             $manager $devis->getReceiver()->getIndividual()->getManager();
  175.             if ($manager === null)
  176.                 return false;
  177.             if ($manager->getId() === $access->getId())
  178.                 return true;
  179.         }
  180.         return false;
  181.     }
  182.     public function viewPdf(Devis $devisAccess $accessAccessFunction $function)
  183.     {
  184.         // Get Acl_Permissions
  185.         $aclPerm $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF);
  186.         $aclPermSociety $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_SOCIETY);
  187.         $aclPermManager $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_MANAGER);
  188.         $aclPermClientManager $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW_PDF_CLIENT_MANAGER);
  189.         // If all are null, exit
  190.         if ($aclPerm === null && $aclPermSociety === null && $aclPermManager === null && $aclPermClientManager === null)
  191.         {
  192.             return false;
  193.         }
  194.         // Get First one
  195.         if ($aclPerm !== null)
  196.         {
  197.             $acl $this->aclRepository->findOneBy(array(
  198.                 'function'        =>    $function,
  199.                 'permission'    =>    $aclPerm
  200.             ));
  201.             if ($acl !== null)
  202.             {
  203.                 if ($acl->getValue())
  204.                 {
  205.                     // A single positive answer is enough
  206.                     return true;
  207.                 }
  208.             }
  209.         }
  210.         // If we are here it means that nothing good has been found
  211.         // Load second permission
  212.         if ($aclPermSociety !== null)
  213.         {
  214.             $acl $this->aclRepository->findOneBy(array(
  215.                 'function'        =>    $function,
  216.                 'permission'    =>    $aclPermSociety
  217.             ));
  218.             if ($acl !== null)
  219.             {
  220.                 if ($acl->getValue())
  221.                 {
  222.                     return $this->checkSociety($devis$access);
  223.                 }
  224.             }
  225.         }
  226.         // If we are here it means that nothing good has been found
  227.         // Load third permission
  228.         if ($aclPermManager !== null)
  229.         {
  230.             $acl $this->aclRepository->findOneBy(array(
  231.                 'function'        =>    $function,
  232.                 'permission'    =>    $aclPermManager
  233.             ));
  234.             if ($acl !== null)
  235.             {
  236.                 if ($acl->getValue())
  237.                 {
  238.                     return $this->checkManager($devis$access);
  239.                 }
  240.             }
  241.         }
  242.         // If we are here it means that nothing good has been found
  243.         // Load fourth permission
  244.         if ($aclPermClientManager !== null)
  245.         {
  246.             $acl $this->aclRepository->findOneBy(array(
  247.                 'function'        =>    $function,
  248.                 'permission'    =>    $aclPermClientManager
  249.             ));
  250.             if ($acl !== null)
  251.             {
  252.                 if ($acl->getValue())
  253.                 {
  254.                     return $this->checkClientManager($devis$access);
  255.                 }
  256.             }
  257.         }
  258.         // If we are here, all hope is lost
  259.         return false;
  260.     }
  261. }