src/Security/GuestSuiteVoter.php line 35

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/GuestSuiteVoter.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\Module;
  12. use App\Entity\Security\Acl;
  13. use App\Entity\Security\AclPermission;
  14. use App\Services\Config\ModuleTools;
  15. class GuestSuiteVoter extends Voter
  16. {
  17.     //--------------------------------------------------------------------------------
  18.     // is_granted constants
  19.     const IS_ACTIVE "guest_suite_is_active";
  20.     const IS_GRANTED_CONSTANTS = array(
  21.         self::IS_ACTIVE
  22.     );
  23.     //--------------------------------------------------------------------------------
  24.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleTools)
  25.     {
  26.         $this->em $doctrine->getManager();
  27.         $this->moduleTools $moduleTools;
  28.         $this->aclRepository $this->em->getRepository(Acl::class);
  29.         $this->aclPermissionRepository $this->em->getRepository(AclPermission::class);
  30.     }
  31.     // Plan.io Task #4453 [See AccessVoter for details]
  32.     public function supportsAttribute(string $attribute): bool
  33.     {
  34.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  35.     }
  36.     
  37.     protected function supports(string $attribute$subject): bool
  38.     {
  39.         // if the attribute isn't one we support, return false
  40.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  41.         {
  42.             return false;
  43.         }
  44.         return true;
  45.     }
  46.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  47.     {
  48.         $user $token->getUser();
  49.         // Plan.io Task #3707
  50.         if ($user instanceof AccessAPI)
  51.         {
  52.             if ($user->getAccess() === null)
  53.             {
  54.                 return false;
  55.             }
  56.             $user $user->getAccess();
  57.         }
  58.         // Plan.io Task #3707
  59.         // At this point $user is an object of Access type
  60.         // even if the $token->getUser() is AccessAPI
  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_GUEST_SUITE))
  75.         {
  76.             return false;
  77.         }
  78.         // Check current group affectation
  79.         if ($subject !== null)
  80.         {
  81.             $subjectSociety $subject->getSociety();
  82.             if ($subjectSociety === null)
  83.                 return false;
  84.             $subjectGroup $subjectSociety->getGroup();
  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.         }
  95.         throw new \LogicException('This code should not be reached!');
  96.     }
  97. }