src/Security/SocietyGroupVoter.php line 41

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