src/Security/RandomVoter.php line 17

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/RandomVoter.php
  4. //------------------------------------------------------------------------------
  5. namespace App\Security;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use App\Entity\Access;
  10. use App\Entity\Security\Acl;
  11. use App\Entity\Security\AclPermission;
  12. use App\Services\Config\ModuleTools;
  13. class RandomVoter extends Voter
  14. {
  15.     //--------------------------------------------------------------------------------
  16.     // is_granted constants
  17.     // This one is tricky and I had no idea how to do it otherwise
  18.     // In order to show the Notes Acl item in the Acl Dashboard
  19.     // we need to detect if either the Mission or Client modules are active
  20.     const IS_ACTIVE_CLIENT_OR_MISSION "client_or_mission_is_active";
  21.     const IS_REKTO "is_rekto";
  22.     const IS_JCAF "is_jcaf";
  23.     const IS_GRANTED_CONSTANTS = array(
  24.         self::IS_ACTIVE_CLIENT_OR_MISSION,
  25.         self::IS_REKTO,
  26.         self::IS_JCAF,
  27.     );
  28.     //--------------------------------------------------------------------------------
  29.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleTools)
  30.     {
  31.         $this->em $doctrine->getManager();
  32.         $this->moduleTools $moduleTools;
  33.         $this->aclRepository $this->em->getRepository(Acl::class);
  34.         $this->aclPermissionRepository $this->em->getRepository(AclPermission::class);
  35.     }
  36.     // Plan.io Task #4453 [See AccessVoter for details]
  37.     public function supportsAttribute(string $attribute): bool
  38.     {
  39.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  40.     }
  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.         $currentGroup $user->getSocietyGroup();
  63.         if ($currentGroup === null)
  64.             return false;
  65.         $this->currentGroup $currentGroup;
  66.         switch ($attribute)
  67.         {
  68.             case self::IS_REKTO:
  69.             {
  70.                 if ($this->currentGroup->isRekto())
  71.                     return true;
  72.                 return false;
  73.             }
  74.             case self::IS_JCAF:
  75.             {
  76.                 if ($this->currentGroup->isJcaf())
  77.                     return true;
  78.                 return false;
  79.             }
  80.             case self::IS_ACTIVE_CLIENT_OR_MISSION:
  81.             {
  82.                 return $this->moduleTools->isActiveClientOrMission($this->currentGroup);
  83.             }
  84.         }
  85.         throw new \LogicException('This code should not be reached!');
  86.     }
  87. }