src/Security/PdfFillVoter.php line 20

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/PdfFillVoter.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\PDF\PdfFiller;
  14. use App\Entity\Security\Acl;
  15. use App\Entity\Security\AclPermission;
  16. use App\Services\Config\ModuleTools;
  17. class PdfFillVoter extends Voter
  18. {
  19.     //--------------------------------------------------------------------------------
  20.     // is_granted constants
  21.     const IS_ACTIVE "pdf_fill_is_active";
  22.     const USE = "use_pdf_fill";
  23.     const IS_GRANTED_CONSTANTS = array(
  24.         self::IS_ACTIVE,
  25.         self::USE,
  26.     );
  27.     //--------------------------------------------------------------------------------
  28.     // acl constants
  29.     const ACL_PERM_USE "pdf_fill_use";
  30.     //--------------------------------------------------------------------------------
  31.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleTools)
  32.     {
  33.         $this->em $doctrine->getManager();
  34.         $this->moduleTools $moduleTools;
  35.         $this->aclRepository $this->em->getRepository(Acl::class);
  36.         $this->aclPermissionRepository $this->em->getRepository(AclPermission::class);
  37.     }
  38.     // Plan.io Task #4453 [See AccessVoter for details]
  39.     public function supportsAttribute(string $attribute): bool
  40.     {
  41.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  42.     }
  43.     
  44.     protected function supports(string $attribute$subject null): bool
  45.     {
  46.         // if the attribute isn't one we support, return false
  47.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  48.         {
  49.             return false;
  50.         }
  51.         // only vote on PdfFiller objects inside this voter
  52.         if ($subject !== null && !$subject instanceof PdfFiller)
  53.         {
  54.             return false;
  55.         }
  56.         return true;
  57.     }
  58.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  59.     {
  60.         $user $token->getUser();
  61.         if (!$user instanceof Access)
  62.         {
  63.             // the user must be logged in; if not, deny access
  64.             return false;
  65.         }
  66.         // The user must have a function; if not deny access
  67.         $function $user->getFunction();
  68.         if ($function === null)        return false;
  69.         // Plan.io Task #3710 : Get current group
  70.         $currentGroup $user->getSocietyGroup();
  71.         if ($currentGroup === null)
  72.             return false;
  73.         // Module activated ?
  74.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_PDF_FILL))
  75.         {
  76.             return false;
  77.         }
  78.         // you know $subject is a PdfFiller object, thanks to supports
  79.         /** @var PdfFiller $pdf */
  80.         $pdf $subject;
  81.         // Check current group affectation
  82.         if ($subject !== null)
  83.         {
  84.             $subjectGroup $subject->getSocietyGroup();
  85.             if ($subjectGroup === null)
  86.                 return false;
  87.             if (!$currentGroup->equals($subjectGroup))
  88.                 return false;
  89.         }
  90.         switch ($attribute)
  91.         {
  92.             case self::IS_ACTIVE:
  93.                 return true;
  94.             case self::USE:
  95.                 return $this->canUse($user$function);
  96.         }
  97.         throw new \LogicException('This code should not be reached!');
  98.     }
  99.     private function canUse(Access $accessAccessFunction $function)
  100.     {
  101.         // Get Acl_Permission
  102.         $aclPerm $this->aclPermissionRepository->findOneByName(self::ACL_PERM_USE);
  103.         if ($aclPerm === null)        return false;
  104.         // Get Acl
  105.         $acl $this->aclRepository->findOneBy(array(
  106.             'function'        =>    $function,
  107.             'permission'    =>    $aclPerm
  108.         ));
  109.         if ($acl === null)        return false;
  110.         return $acl->getValue();
  111.     }
  112. }