src/Security/TrustpilotVoter.php line 30

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/TrustpilotVoter.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\APIRest\AccessAPI;
  11. use App\Entity\Config\Module;
  12. use App\Services\Config\ModuleTools;
  13. class TrustpilotVoter extends Voter
  14. {
  15.     //--------------------------------------------------------------------------------
  16.     // is_granted constants
  17.     const IS_ACTIVE = "trustpilot_is_active";
  18.     const IS_GRANTED_CONSTANTS = array(
  19.         self::IS_ACTIVE
  20.     );
  21.     //--------------------------------------------------------------------------------
  22.     public function __construct(ManagerRegistry $doctrine, ModuleTools $moduleTools)
  23.     {
  24.         $this->em = $doctrine->getManager();
  25.         $this->moduleTools = $moduleTools;
  26.     }
  27.     // Plan.io Task #4453 [See AccessVoter for details]
  28.     public function supportsAttribute(string $attribute): bool
  29.     {
  30.         return in_array($attribute, self::IS_GRANTED_CONSTANTS, true);
  31.     }
  32.     
  33.     protected function supports(string $attribute, $subject): bool
  34.     {
  35.         // if the attribute isn't one we support, return false
  36.         if (!in_array($attribute, self::IS_GRANTED_CONSTANTS))
  37.         {
  38.             return false;
  39.         }
  40.         return true;
  41.     }
  42.     protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  43.     {
  44.         $user = $token->getUser();
  45.         // Plan.io Task #3707
  46.         if ($user instanceof AccessAPI)
  47.         {
  48.             if ($user->getAccess() === null)
  49.             {
  50.                 return false;
  51.             }
  52.             $user = $user->getAccess();
  53.         }
  54.         // Plan.io Task #3707
  55.         // At this point $user is an object of Access type
  56.         // even if the $token->getUser() is AccessAPI
  57.         if (!$user instanceof Access)
  58.         {
  59.             // the user must be logged in; if not, deny access
  60.             return false;
  61.         }
  62.         // The user must have a function; if not deny access
  63.         $function = $user->getFunction();
  64.         if ($function === null)        return false;
  65.         // Plan.io Task #3710 : Get current group
  66.         $currentGroup = $user->getSocietyGroup();
  67.         if ($currentGroup === null)
  68.             return false;
  69.         // Module activated ?
  70.         if ($this->moduleTools->isInactiveByCode($currentGroup, Module::MODULE_TRUSTPILOT))
  71.         {
  72.             return false;
  73.         }
  74.         // Check current group affectation
  75.         if ($subject !== null)
  76.         {
  77.             $subjectSociety = $subject->getSociety();
  78.             if ($subjectSociety === null)
  79.                 return false;
  80.             $subjectGroup = $subjectSociety->getGroup();
  81.             if ($subjectGroup === null)
  82.                 return false;
  83.             if (!$currentGroup->equals($subjectGroup))
  84.                 return false;
  85.         }
  86.         switch ($attribute)
  87.         {
  88.             case self::IS_ACTIVE:
  89.                 return true;
  90.         }
  91.         throw new \LogicException('This code should not be reached!');
  92.     }
  93. }