src/Security/MapsVoter.php line 34

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/MapsVoter.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 MapsVoter extends Voter
  17. {
  18.     //--------------------------------------------------------------------------------
  19.     // is_granted constants
  20.     const IS_ACTIVE "maps_is_active";
  21.     const IS_GRANTED_CONSTANTS = array(
  22.         self::IS_ACTIVE,
  23.     );
  24.     //--------------------------------------------------------------------------------
  25.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleTools)
  26.     {
  27.         $this->em $doctrine->getManager();
  28.         $this->moduleTools $moduleTools;
  29.     }
  30.     // Plan.io Task #4453 [See AccessVoter for details]
  31.     public function supportsAttribute(string $attribute): bool
  32.     {
  33.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  34.     }
  35.     
  36.     protected function supports(string $attribute$subject null): bool
  37.     {
  38.         // if the attribute isn't one we support, return false
  39.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  40.         {
  41.             return false;
  42.         }
  43.         return true;
  44.     }
  45.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  46.     {
  47.         $user $token->getUser();
  48.         if (!$user instanceof Access)
  49.         {
  50.             // the user must be logged in; if not, deny access
  51.             return false;
  52.         }
  53.         // The user must have a function; if not deny access
  54.         $function $user->getFunction();
  55.         if ($function === null)        return false;
  56.         // Plan.io Task #3710 : Get current group
  57.         $currentGroup $user->getSocietyGroup();
  58.         if ($currentGroup === null)
  59.             return false;
  60.         // Module activated ?
  61.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_MAPS))
  62.         {
  63.             return false;
  64.         }
  65.         switch ($attribute)
  66.         {
  67.             case self::IS_ACTIVE:
  68.                 return true;
  69.         }
  70.         throw new \LogicException('This code should not be reached!');
  71.     }
  72. }