src/Security/TakeoutVoter.php line 19

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/TakeoutVoter.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\Security\Acl;
  14. use App\Entity\Security\AclPermission;
  15. use App\Services\Config\ModuleTools;
  16. class TakeoutVoter extends Voter
  17. {
  18.     //--------------------------------------------------------------------------------
  19.     // is_granted constants
  20.     const IS_ACTIVE "takeout_is_active";
  21.     const ALLOW_TAKEOUT "allow_takeout";
  22.     const IS_GRANTED_CONSTANTS = array(
  23.         self::IS_ACTIVE,
  24.         self::ALLOW_TAKEOUT,
  25.     );
  26.     //--------------------------------------------------------------------------------
  27.     // acl constants
  28.     const ACL_ALLOW_TAKEOUT "takeout";
  29.     //--------------------------------------------------------------------------------
  30.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleTools)
  31.     {
  32.         $this->em $doctrine->getManager();
  33.         $this->moduleTools $moduleTools;
  34.         $this->aclRepository $this->em->getRepository(Acl::class);
  35.         $this->aclPermissionRepository $this->em->getRepository(AclPermission::class);
  36.     }
  37.     // Plan.io Task #4453 [See AccessVoter for details]
  38.     public function supportsAttribute(string $attribute): bool
  39.     {
  40.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  41.     }
  42.     protected function supports(string $attribute$subject null): bool
  43.     {
  44.         // if the attribute isn't one we support, return false
  45.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  46.         {
  47.             return false;
  48.         }
  49.         return true;
  50.     }
  51.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  52.     {
  53.         $user $token->getUser();
  54.         if (!$user instanceof Access)
  55.         {
  56.             // the user must be logged in; if not, deny access
  57.             return false;
  58.         }
  59.         // The user must have a function; if not deny access
  60.         $function $user->getFunction();
  61.         if ($function === null)        return false;
  62.         // Plan.io Task #3710 : Get current group
  63.         $currentGroup $user->getSocietyGroup();
  64.         if ($currentGroup === null)
  65.             return false;
  66.         // Module activated ?
  67.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_TAKEOUT))
  68.         {
  69.             return false;
  70.         }
  71.         switch ($attribute)
  72.         {
  73.             case self::IS_ACTIVE:
  74.                 return true;
  75.             case self::ALLOW_TAKEOUT:
  76.                 return $this->allowTakeout($user$function);
  77.         }
  78.         throw new \LogicException('This code should not be reached!');
  79.     }
  80.     private function allowTakeout(Access $userAccessFunction $function)
  81.     {
  82.         // Get Acl_Permission
  83.         $aclPerm $this->aclPermissionRepository->findOneByName(self::ACL_ALLOW_TAKEOUT);
  84.         if ($aclPerm === null)        return false;
  85.         // Get Acl
  86.         $acl $this->aclRepository->findOneBy(array(
  87.             'function'        =>    $function,
  88.             'permission'    =>    $aclPerm
  89.         ));
  90.         if ($acl === null)        return false;
  91.         // Since only one acl type can exist
  92.         // we can return the result of the acl_permission
  93.         return $acl->getValue();
  94.     }
  95. }