src/Security/UserSpecificVoter.php line 54

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/UserSpecificVoter.php
  4. //------------------------------------------------------------------------------
  5. namespace App\Security;
  6. use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use App\Entity\Access;
  12. use App\Entity\Config\Config;
  13. use App\Entity\Config\Module;
  14. use App\Entity\HR\AccessFunction;
  15. use App\Entity\Security\Acl;
  16. use App\Entity\Security\AclPermission;
  17. use App\Services\Config\ModuleTools;
  18. class UserSpecificVoter extends Voter
  19. {
  20.     const TEST_ITASKS "test_itasks";
  21.     const TEST_SEND_EMAIL_PROSPECT "test_send_email_prospect";
  22.     const IS_GRANTED_CONSTANTS = array(
  23.         self::TEST_ITASKS,
  24.         self::TEST_SEND_EMAIL_PROSPECT,
  25.     );
  26.     const JOHANN = array(
  27.         'johann',
  28.         'jg.johanngilbert@gmail.com',
  29.         'jg.johann.gilbert@gmail.com',
  30.         'johann.gilbert@sofacile.fr',
  31.     );
  32.     const DEVS = array(
  33.         'oanalivia',
  34.         'alexandre',
  35.         'dylan',
  36.         'perrier',
  37.     );
  38.     const PSEUDO_DEVS = array(
  39.         'o.peyrat@dvlpr.fr',
  40.         'a.peyrat@dvlpr.fr',
  41.         'oanalivia@gmail.com',
  42.         'dylan.bernard@unilim.fr',
  43.     );
  44.     public function __construct(Security $securityManagerRegistry $doctrine)
  45.     {
  46.         $this->security $security;
  47.         $this->em $doctrine->getManager();
  48.     }
  49.     // Plan.io Task #4453 [See AccessVoter for details]
  50.     public function supportsAttribute(string $attribute): bool
  51.     {
  52.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  53.     }
  54.     protected function supports(string $attribute$subject null): bool
  55.     {
  56.         // if the attribute isn't one we support, return false
  57.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  58.         {
  59.             return false;
  60.         }
  61.         return true;
  62.     }
  63.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  64.     {
  65.         $user $token->getUser();
  66.         if (!$user instanceof Access)
  67.         {
  68.             // the user must be logged in; if not, deny access
  69.             return false;
  70.         }
  71.         $impersonatorUser null;
  72.         $token $this->security->getToken();
  73.         if ($token instanceof SwitchUserToken)
  74.         {
  75.             $impersonatorUser $token->getOriginalToken()->getUser();
  76.         }
  77.         switch ($attribute)
  78.         {
  79.             case self::TEST_ITASKS:
  80.                 return $this->canTestITasks($user$impersonatorUser);
  81.             case self::TEST_SEND_EMAIL_PROSPECT:
  82.                 return $this->canTestSEndEmailProspect($user$impersonatorUser);
  83.         }
  84.         throw new \LogicException('This code should not be reached!');
  85.     }
  86.     private function canTestITasks(Access $access$impersonatorUser)
  87.     {
  88.         if (in_array($access->getUserIdentifier(), self::JOHANN))
  89.         {
  90.             return true;
  91.         }
  92.         if (in_array($access->getUserIdentifier(), self::DEVS))
  93.         {
  94.             return true;
  95.         }
  96.         if (in_array($access->getUserIdentifier(), self::PSEUDO_DEVS))
  97.         {
  98.             return true;
  99.         }
  100.         if ($impersonatorUser !== null)
  101.         {
  102.             if (in_array($impersonatorUser->getUserIdentifier(), self::JOHANN))
  103.             {
  104.                 return true;
  105.             }
  106.             if (in_array($impersonatorUser->getUserIdentifier(), self::DEVS))
  107.             {
  108.                 return true;
  109.             }
  110.             if (in_array($impersonatorUser->getUserIdentifier(), self::PSEUDO_DEVS))
  111.             {
  112.                 return true;
  113.             }
  114.         }
  115.         return false;
  116.     }
  117.     private function canTestSEndEmailProspect(Access $access$impersonatorUser)
  118.     {
  119.         if (in_array($access->getUserIdentifier(), self::JOHANN))
  120.         {
  121.             return true;
  122.         }
  123.         if (in_array($access->getUserIdentifier(), self::DEVS))
  124.         {
  125.             return true;
  126.         }
  127.         if (in_array($access->getUserIdentifier(), self::PSEUDO_DEVS))
  128.         {
  129.             return true;
  130.         }
  131.         if ($impersonatorUser !== null)
  132.         {
  133.             if (in_array($impersonatorUser->getUserIdentifier(), self::JOHANN))
  134.             {
  135.                 return true;
  136.             }
  137.             if (in_array($impersonatorUser->getUserIdentifier(), self::DEVS))
  138.             {
  139.                 return true;
  140.             }
  141.             if (in_array($impersonatorUser->getUserIdentifier(), self::PSEUDO_DEVS))
  142.             {
  143.                 return true;
  144.             }
  145.         }
  146.         return false;
  147.     }
  148. }