src/Security/EmailVoter.php line 30

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/EmailVoter.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\Services\Config\ModuleTools;
  15. class EmailVoter extends Voter
  16. {
  17.     //--------------------------------------------------------------------------------
  18.     // is_granted constants
  19.     const IS_ACTIVE "email_is_active";
  20.     const IS_GRANTED_CONSTANTS = array(
  21.         self::IS_ACTIVE,
  22.     );
  23.     //--------------------------------------------------------------------------------
  24.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleTools)
  25.     {
  26.         $this->em $doctrine->getManager();
  27.         $this->moduleTools $moduleTools;
  28.     }
  29.     // Plan.io Task #4453 [See AccessVoter for details]
  30.     public function supportsAttribute(string $attribute): bool
  31.     {
  32.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  33.     }
  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_EMAIL))
  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. }