src/Security/EInvoicingVoter.php line 20

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/EInvoicingVoter.php
  4. // Plan.io Task #4634 — E-invoicing module ACL
  5. //------------------------------------------------------------------------------
  6. namespace App\Security;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use App\Entity\Access;
  11. use App\Entity\APIRest\AccessAPI;
  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 EInvoicingVoter extends Voter
  18. {
  19.     const IS_ACTIVE   'einvoicing_is_active';
  20.     const IS_GRANTED_CONSTANTS = [
  21.         self::IS_ACTIVE
  22.     ];
  23.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleTools)
  24.     {
  25.         $this->em $doctrine->getManager();
  26.         $this->moduleTools $moduleTools;
  27.         $this->aclRepository $this->em->getRepository(Acl::class);
  28.         $this->aclPermissionRepository $this->em->getRepository(AclPermission::class);
  29.     }
  30.     public function supportsAttribute(string $attribute): bool
  31.     {
  32.         return \in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  33.     }
  34.     protected function supports(string $attribute$subject): bool
  35.     {
  36.         // if the attribute isn't one we support, return false
  37.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  38.         {
  39.             return false;
  40.         }
  41.         // only vote on Invoice objects inside this voter
  42.         // if ($subject !== null && !($subject instanceof Invoice))
  43.         // {
  44.         //     return false;
  45.         // }
  46.         return true;
  47.     }
  48.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  49.     {
  50.         $user $token->getUser();
  51.         // Plan.io Task #3707
  52.         if ($user instanceof AccessAPI
  53.         {
  54.             if ($user->getAccess() === null
  55.             {
  56.                 return false;
  57.             }
  58.             $user $user->getAccess();
  59.         }
  60.         // Plan.io Task #3707
  61.         // At this point $user is an object of Access type
  62.         // even if the $token->getUser() is AccessAPI
  63.         if (!$user instanceof Access
  64.         {
  65.             // the user must be logged in; if not, deny access
  66.             return false;
  67.         }
  68.         // The user must have a function; if not deny access
  69.         $function $user->getFunction();
  70.         if ($function === null
  71.         {
  72.             return false;
  73.         }
  74.         $currentGroup $user->getSocietyGroup();
  75.         if ($currentGroup === null
  76.         {
  77.             return false;
  78.         }
  79.         // Module activated ?
  80.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_EINVOICING)) 
  81.         {
  82.             return false;
  83.         }
  84.         // Check current group affectation
  85.         if ($subject !== null)
  86.         {
  87.             $subjectSociety $subject->getSociety();
  88.             if ($subjectSociety === null)
  89.                 return false;
  90.             $subjectGroup $subjectSociety->getGroup();
  91.             if ($subjectGroup === null)
  92.                 return false;
  93.             if (!$currentGroup->equals($subjectGroup))
  94.                 return false;
  95.         }
  96.         switch ($attribute
  97.         {
  98.             case self::IS_ACTIVE:
  99.                 return true;
  100.         }
  101.         throw new \LogicException('This code should not be reached!');
  102.     }
  103. }