src/Security/CustomizationVoter.php line 19

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/CustomizationVoter.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 CustomizationVoter extends Voter
  17. {
  18.     //--------------------------------------------------------------------------------
  19.     // is_granted constants
  20.     const IS_ACTIVE "customization_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.     protected function supports(string $attribute$subject null): bool
  36.     {
  37.         // if the attribute isn't one we support, return false
  38.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  39.         {
  40.             return false;
  41.         }
  42.         return true;
  43.     }
  44.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  45.     {
  46.         $user $token->getUser();
  47.         if (!$user instanceof Access)
  48.         {
  49.             // the user must be logged in; if not, deny access
  50.             return false;
  51.         }
  52.         // The user must have a function; if not deny access
  53.         $function $user->getFunction();
  54.         if ($function === null)        return false;
  55.         // Plan.io Task #3710 : Get current group
  56.         $currentGroup $user->getSocietyGroup();
  57.         if ($currentGroup === null)
  58.             return false;
  59.         // Module activated ?
  60.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_CUSTOMIZATION))
  61.         {
  62.             return false;
  63.         }
  64.         switch ($attribute)
  65.         {
  66.             case self::IS_ACTIVE:
  67.                 return true;
  68.         }
  69.         throw new \LogicException('This code should not be reached!');
  70.     }
  71. }