src/Security/SocietyGroupDirectoryVoter.php line 48

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/SocietyGroupDirectoryVoter.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\SocietyGroup;
  11. use App\Entity\Config\Config;
  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. use App\Services\Config\OptionConfigTools;
  18. class SocietyGroupDirectoryVoter extends Voter
  19. {
  20.     //--------------------------------------------------------------------------------
  21.     // is_granted constants
  22.     // Plan.io Task #4652 : Added IS_ACTIVE_HANDLE_FAV
  23.     const VIEW "view_society_group_directory";
  24.     const EDIT "edit_society_group_directory";    
  25.     const IS_ACTIVE_HANDLE_FAV "handle_society_group_fav_is_active";    
  26.     const IS_GRANTED_CONSTANTS = array(
  27.         self::VIEW,
  28.         self::EDIT,        
  29.     );
  30.     //--------------------------------------------------------------------------------
  31.     // acl constants
  32.     const ACL_PERM_VIEW "society_group_directory_view";
  33.     const ACL_PERM_EDIT "society_group_directory_edit";
  34.     //--------------------------------------------------------------------------------
  35.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleToolsOptionConfigTools $optionConfigTools)
  36.     {
  37.         $this->em $doctrine->getManager();
  38.         $this->moduleTools $moduleTools;
  39.         $this->optionConfigTools $optionConfigTools;
  40.         $this->aclRepository $this->em->getRepository(Acl::class);
  41.         $this->aclPermissionRepository $this->em->getRepository(AclPermission::class);
  42.     }
  43.     // Plan.io Task #4453 [See AccessVoter for details]
  44.     public function supportsAttribute(string $attribute): bool
  45.     {
  46.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  47.     }
  48.     
  49.     protected function supports(string $attribute$subject null): bool
  50.     {
  51.         // if the attribute isn't one we support, return false
  52.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  53.         {
  54.             return false;
  55.         }
  56.         // Only vote on SocietyGroup objects inside this voter
  57.         if ($subject !== null && !$subject instanceof SocietyGroup)
  58.         {
  59.             return false;
  60.         }
  61.         return true;
  62.     }
  63.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  64.     {
  65.         $user $token->getUser();
  66.         if (!$user instanceof Access)
  67.         {
  68.             // the user must be logged in; if not, deny access
  69.             return false;
  70.         }
  71.         // The user must have a function; if not deny access
  72.         $function $user->getFunction();
  73.         if ($function === null)        return false;
  74.         // Plan.io Task #3710 : Get current group
  75.         $currentGroup $user->getSocietyGroup();
  76.         if ($currentGroup === null)
  77.             return false;
  78.         // Module activated ?
  79.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_MISSION_PLUS))
  80.         {
  81.             return false;
  82.         }
  83.         // you know $subject is a SocietyGroup object, thanks to supports
  84.         /** @var SocietyGroup $societyGroup */
  85.         $societyGroup $subject;
  86.         switch ($attribute)
  87.         {
  88.             case self::IS_ACTIVE_HANDLE_FAV:
  89.             {
  90.                 return $this->optionConfigTools->isActive_HandleSocietyGroupFav($societyGroup);
  91.             }
  92.             case self::VIEW:
  93.             {
  94.                 return $this->canView($user$function$societyGroup);
  95.             }
  96.             case self::EDIT:
  97.             {
  98.                 return $this->canEdit($user$function);
  99.             }
  100.         }
  101.         throw new \LogicException('This code should not be reached!');
  102.     }
  103.     private function canView(Access $accessAccessFunction $function$societyGroup null)
  104.     {
  105.         $aclPerm $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW);
  106.         // If all are null, exit
  107.         if ($aclPerm === null)
  108.             return false;
  109.         // Get First one
  110.         if ($aclPerm !== null)
  111.         {
  112.             $acl $this->aclRepository->findOneBy(array(
  113.                 'function'        =>    $function,
  114.                 'permission'    =>    $aclPerm
  115.             ));
  116.             if ($acl !== null)
  117.             {
  118.                 if ($acl->getValue())
  119.                 {
  120.                     // A single positive answer is enough
  121.                     return true;
  122.                 }
  123.             }
  124.         }
  125.         // If we are here, all hope is lost
  126.         return false;
  127.     }
  128.     private function canEdit(Access $accessAccessFunction $function)
  129.     {
  130.         $aclPerm $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT);
  131.         // If all are null, exit
  132.         if ($aclPerm === null)
  133.             return false;
  134.         // Get First one
  135.         if ($aclPerm !== null)
  136.         {
  137.             $acl $this->aclRepository->findOneBy(array(
  138.                 'function'        =>    $function,
  139.                 'permission'    =>    $aclPerm
  140.             ));
  141.             if ($acl !== null)
  142.             {
  143.                 if ($acl->getValue())
  144.                 {
  145.                     // A single positive answer is enough
  146.                     return true;
  147.                 }
  148.             }
  149.         }
  150.         // If we are here, all hope is lost
  151.         return false;
  152.     }
  153. }