src/Security/DevVoter.php line 91

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/DevVoter.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 Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
  10. use Symfony\Component\Security\Core\Role\SwitchUserRole;
  11. use App\Entity\Access;
  12. use App\Entity\Security\Acl;
  13. class DevVoter extends Voter
  14. {
  15.     // Icon to use : <i class="las la-ghost"></i>
  16.     const OPEN_SESAME "open_sesame";
  17.     const OPEN_SESAME_OANALIVIA "open_sesame_oanalivia";
  18.     const OPEN_SESAME_ALEX "open_sesame_alex";
  19.     const OPEN_SESAME_DYLAN "open_sesame_dylan";
  20.     const UNDER_CONSTRUCTION "under_construction";
  21.     // This includes Johann
  22.     // Icon to use : <i class="fa fa-asterisk"></i>
  23.     const REKAPP_ADMIN "rekapp_admin";
  24.     const TYPES = array(
  25.         self::OPEN_SESAME,
  26.         self::OPEN_SESAME_OANALIVIA,
  27.         self::OPEN_SESAME_ALEX,
  28.         self::OPEN_SESAME_DYLAN,
  29.         self::UNDER_CONSTRUCTION,
  30.         self::REKAPP_ADMIN,                // This includes Johann
  31.     );
  32.     const DEVS = array(
  33.         'oanalivia',
  34.         'alexandre',
  35.         'dylan',
  36.         'perrier',
  37.     );
  38.     const DEV_ALEX = array(
  39.         'alexandre',
  40.         'a.peyrat@dvlpr.fr',
  41.     );
  42.     const DEV_OANALIVIA = array(
  43.         'oanalivia',
  44.         'o.peyrat@dvlpr.fr',
  45.         'oanalivia@gmail.com',
  46.     );
  47.     const DEV_DYLAN = array(
  48.         'dylan',
  49.         'dylan.bernard@unilim.fr',
  50.     );
  51.     // TODO
  52.     // 12/12/2022 : For docker local
  53.     const LOCAL_DEV = array(
  54.         'superadmin'
  55.     );
  56.     const PSEUDO_DEVS = array(
  57.         'o.peyrat@dvlpr.fr',
  58.         'a.peyrat@dvlpr.fr',
  59.         'oanalivia@gmail.com',
  60.         'dylan.bernard@unilim.fr',
  61.     );
  62.     const UNDER_CONSTRUCTION_WORKERS = array(
  63.         'o.peyrat@dvlpr.fr',
  64.         'a.peyrat@dvlpr.fr',
  65.         'oanalivia@gmail.com',
  66.         'oanalivia',
  67.         'dylan'
  68.     );
  69.     const REKAPP_ADMIN_USERS = array(
  70.         'oanalivia',
  71.         'alexandre',
  72.         'dylan',
  73.         'johann',
  74.         'perrier',
  75.     );
  76.     public function __construct(ManagerRegistry $doctrine)
  77.     {
  78.         $this->em $doctrine->getManager();
  79.     }
  80.     // Plan.io Task #4453 [See AccessVoter for details]
  81.     public function supportsAttribute(string $attribute): bool
  82.     {
  83.         return in_array($attributeself::TYPEStrue);
  84.     }
  85.     protected function supports(string $attribute$subject): bool
  86.     {
  87.         // if the attribute isn't one we support, return false
  88.         if (!in_array($attributeself::TYPES))
  89.         {
  90.             return false;
  91.         }
  92.         return true;
  93.     }
  94.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  95.     {
  96.         $user $token->getUser();
  97.         if (!$user instanceof Access)
  98.         {
  99.             // the user must be logged in; if not, deny access
  100.             return false;
  101.         }
  102.         $impersonatorUser null;
  103.         if ($token instanceof SwitchUserToken)
  104.         {
  105.             $impersonatorUser $token->getOriginalToken()->getUser();
  106.         }
  107.         $userToUse $user;
  108.         if ($impersonatorUser !== null and $impersonatorUser instanceof Access)
  109.         {
  110.             $userToUse $impersonatorUser;
  111.         }
  112.         switch ($attribute)
  113.         {
  114.             case self::OPEN_SESAME:
  115.                 return $this->openSesame($userToUse);
  116.             case self::OPEN_SESAME_OANALIVIA:
  117.                 return $this->openSesameOanaLivia($userToUse);
  118.             case self::OPEN_SESAME_ALEX:
  119.                 return $this->openSesameAlex($userToUse);
  120.             case self::OPEN_SESAME_DYLAN:
  121.                 return $this->openSesameDylan($userToUse);
  122.             case self::UNDER_CONSTRUCTION:
  123.                 return $this->underConstruction($userToUse);
  124.             case self::REKAPP_ADMIN:
  125.                 return $this->rekappAdmin($userToUse);
  126.         }
  127.         throw new \LogicException('This code should not be reached!');
  128.     }
  129.     private function openSesame(Access $access)
  130.     {
  131.         if (in_array($access->getUsername(), self::DEVS))
  132.         {
  133.             return true;
  134.         }
  135.         if (in_array($access->getUsername(), self::PSEUDO_DEVS))
  136.         {
  137.             return true;
  138.         }
  139.         if (in_array($access->getUsername(), self::DEV_ALEX))
  140.         {
  141.             return true;
  142.         }
  143.         if (in_array($access->getUsername(), self::DEV_OANALIVIA))
  144.         {
  145.             return true;
  146.         }
  147.         if (in_array($access->getUsername(), self::DEV_DYLAN))
  148.         {
  149.             return true;
  150.         }
  151.         // TODO
  152.         // if (in_array($access->getUsername(), self::LOCAL_DEV))
  153.         // {
  154.         //     if ($this->localRealm == 1)
  155.         //     {
  156.         //         return true;
  157.         //     }
  158.         // }
  159.         return false;
  160.     }
  161.     private function openSesameOanaLivia(Access $access)
  162.     {
  163.         if (in_array($access->getUsername(), self::DEV_OANALIVIA))
  164.         {
  165.             return true;
  166.         }
  167.         return false;
  168.     }
  169.     private function openSesameAlex(Access $access)
  170.     {
  171.         if (in_array($access->getUsername(), self::DEV_ALEX))
  172.         {
  173.             return true;
  174.         }
  175.         return false;
  176.     }
  177.     private function openSesameDylan(Access $access)
  178.     {
  179.         if (in_array($access->getUsername(), self::DEV_DYLAN))
  180.         {
  181.             return true;
  182.         }
  183.         return false;
  184.     }
  185.     private function underConstruction(Access $access)
  186.     {
  187.         if (in_array($access->getUsername(), self::UNDER_CONSTRUCTION_WORKERS))
  188.         {
  189.             return true;
  190.         }
  191.         return false;
  192.     }
  193.     private function rekappAdmin(Access $access)
  194.     {
  195.         if (in_array($access->getUsername(), self::REKAPP_ADMIN_USERS))
  196.         {
  197.             return true;
  198.         }
  199.         return false;
  200.     }
  201. }