src/Security/CommonVoter.php line 50

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/CommonVoter.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\SocietyGroup;
  11. use App\Entity\Config\Config;
  12. use App\Entity\Config\OptionConfig;
  13. use App\Entity\Config\Module;
  14. use App\Entity\Config\ModuleConfig;
  15. use App\Entity\Security\Acl;
  16. use App\Services\Config\ModuleTools;
  17. use App\Services\Config\OptionConfigTools;
  18. use App\Services\LogTools;
  19. class CommonVoter extends Voter
  20. {
  21.     //--------------------------------------------------------------------------------
  22.     // Plan.io Task #3605
  23.     const IS_ACTIVE_GHOST_INVOICING "is_active_ghost_invoicing";
  24.     // Plan.io Task #3664
  25.     const IS_ACTIVE_IKEA_DEVIS_TEMPLATE "is_active_ikea_devis_template";
  26.     // Plan.io Task #4327
  27.     const IS_ACTIVE_JCAF "is_active_jcaf";
  28.     // Plan.io Task #4327
  29.     const IS_ACTIVE_CLIENT_ACCOUNT "is_active_client_account";
  30.     // Plan.io Task #4383
  31.     const IS_ACTIVE_ONLINE_BOOKING "is_active_online_booking";
  32.     // Plan.io Task #4327
  33.     const IS_ACTIVE_CLIENT_ACCOUNT_AUTO "is_active_client_account_auto";
  34.     const IS_GRANTED_CONSTANTS = array(
  35.         self::IS_ACTIVE_GHOST_INVOICING,
  36.         self::IS_ACTIVE_IKEA_DEVIS_TEMPLATE,
  37.         self::IS_ACTIVE_JCAF,
  38.         self::IS_ACTIVE_CLIENT_ACCOUNT,
  39.         self::IS_ACTIVE_ONLINE_BOOKING,
  40.         self::IS_ACTIVE_CLIENT_ACCOUNT_AUTO,
  41.     );
  42.     //--------------------------------------------------------------------------------
  43.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleToolsOptionConfigTools $optionConfigToolsLogTools $logTools)
  44.     {
  45.         $this->em $doctrine->getManager();
  46.         $this->moduleTools $moduleTools;
  47.         $this->optionConfigTools $optionConfigTools;
  48.         $this->logTools $logTools;
  49.     }
  50.     // Plan.io Task #4453 [See AccessVoter for details]
  51.     public function supportsAttribute(string $attribute): bool
  52.     {
  53.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  54.     }
  55.     protected function supports(string $attribute$subject): bool
  56.     {
  57.         // if the attribute isn't one we support, return false
  58.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  59.         {
  60.             return false;
  61.         }
  62.         // no subject for now
  63.         if ($subject !== null)
  64.         {
  65.             return false;
  66.         }
  67.         return true;
  68.     }
  69.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  70.     {
  71.         $user $token->getUser();
  72.         if (!$user instanceof Access)
  73.         {
  74.             // the user must be logged in; if not, deny access
  75.             return false;
  76.         }
  77.         // The user must have a function; if not deny access
  78.         $function $user->getFunction();
  79.         if ($function === null)        return false;
  80.         // Plan.io Task #3710 : Get current group
  81.         $currentGroup $user->getSocietyGroup();
  82.         if ($currentGroup === null)
  83.             return false;
  84.         $this->currentGroup $currentGroup;
  85.         switch ($attribute)
  86.         {
  87.             case self::IS_ACTIVE_GHOST_INVOICING:
  88.                 return $this->isGrantedGhostInvoicing($currentGroup);
  89.             case self::IS_ACTIVE_IKEA_DEVIS_TEMPLATE:
  90.                 return $this->isGrantedIkeaDevisTemplate($currentGroup);
  91.             case self::IS_ACTIVE_JCAF:
  92.                 return $this->isActiveJcaf();
  93.             case self::IS_ACTIVE_CLIENT_ACCOUNT:
  94.                 return $this->isActiveClientAccount();
  95.             case self::IS_ACTIVE_ONLINE_BOOKING:
  96.                 return $this->isActiveOnlineBooking();
  97.             case self::IS_ACTIVE_CLIENT_ACCOUNT_AUTO:
  98.                 return $this->isActiveClientAccountAuto();
  99.         }
  100.         throw new \LogicException('This code should not be reached!');
  101.     }
  102.     private function isActiveClientAccount()
  103.     {
  104.         $module $this->em->getRepository(Module::class)->findOneByCode(Module::MODULE_CLIENT_ACCOUNT);
  105.         if ($module === null)
  106.         {
  107.             return false;
  108.         }
  109.         $moduleConfig $this->em->getRepository(ModuleConfig::class)->findOneBy(array(
  110.             'societyGroup'        =>    $this->currentGroup,
  111.             'module'            =>    $module,
  112.         ));
  113.         if ($moduleConfig === null)
  114.         {
  115.             return false;
  116.         }
  117.         if ($moduleConfig->getValue())
  118.         {
  119.             return true;
  120.         }
  121.         return false;
  122.     }
  123.     private function isActiveOnlineBooking()
  124.     {
  125.         if (!$this->optionConfigTools->isActive_planningOptimisation($this->currentGroup))
  126.         {
  127.             return false;
  128.         }
  129.         if (!$this->isActiveClientAccount())
  130.         {
  131.             return false;
  132.         }
  133.         return $this->optionConfigTools->isActive_OnlineBooking($this->currentGroup);
  134.         return false;
  135.     }
  136.     private function isActiveClientAccountAuto()
  137.     {
  138.         if (!$this->isActiveClientAccount())
  139.         {
  140.             return false;
  141.         }
  142.         return $this->optionConfigTools->isActive_ClientAccountAuto($this->currentGroup);
  143.         return false;
  144.     }
  145.     private function isActiveJcaf()
  146.     {
  147.         $config $this->em->getRepository(Config::class)->findOneByName(Config::JCAF_STATE);
  148.         if ($config === null)
  149.         {
  150.             return false;
  151.         }
  152.         return boolval($config->getValue());
  153.     }
  154.     private function isGrantedGhostInvoicing(SocietyGroup $societyGroup)
  155.     {
  156.         $optionConfig $this->em->getRepository(OptionConfig::class)
  157.             ->findOneBy(array(
  158.                 'societyGroup'        =>    $societyGroup,
  159.                 'code'                =>    OptionConfig::GHOST_INVOICING_CODE,
  160.                 'value'                =>    1,
  161.             ));
  162.         if ($optionConfig !== null)
  163.         {
  164.             return true;
  165.         }
  166.         return false;
  167.     }
  168.     // Plan.io Task #3664
  169.     private function isGrantedIkeaDevisTemplate(SocietyGroup $societyGroup)
  170.     {
  171.         $optionConfig $this->em->getRepository(OptionConfig::class)
  172.             ->findOneBy(array(
  173.                 'societyGroup'        =>    $societyGroup,
  174.                 'code'                =>    OptionConfig::IKEA_DEVIS_TEMPLATE_CODE,
  175.                 'value'                =>    1,
  176.             ));
  177.         if ($optionConfig !== null)
  178.         {
  179.             return true;
  180.         }
  181.         return false;
  182.     }
  183. }