src/Security/Voter/BackendHowToStartVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use App\Utils\Commons;
  5. use LogicException;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. class BackendHowToStartVoter extends Voter
  11. {
  12.     const BACKEND_HOW_TO_START_VIEW 'BACKEND_HOW_TO_START_VIEW';
  13.     const BACKEND_HOW_TO_START_CHANGE 'BACKEND_HOW_TO_START_CHANGE';
  14.     /**
  15.      * @var Security
  16.      */
  17.     private $security;
  18.     /**
  19.      * @var Commons
  20.      */
  21.     private $commons;
  22.     /**
  23.      * @var array
  24.      */
  25.     private $permissions;
  26.     /**
  27.      * BackendHowToStartVoter constructor.
  28.      * @param Security $security
  29.      * @param Commons $commons
  30.      */
  31.     public function __construct(Security $securityCommons $commons)
  32.     {
  33.         $this->security $security;
  34.         $this->commons $commons;
  35.         $this->permissions = [];
  36.     }
  37.     /**
  38.      * @param string $attribute
  39.      * @param mixed $subject
  40.      * @return bool
  41.      */
  42.     protected function supports($attribute$subject): bool
  43.     {
  44.         if (!in_array($attribute,
  45.             [self::BACKEND_HOW_TO_START_VIEWself::BACKEND_HOW_TO_START_CHANGE]
  46.         )) {
  47.             return false;
  48.         }
  49.         return true;
  50.     }
  51.     /**
  52.      * @param string $attribute
  53.      * @param mixed $subject
  54.      * @param TokenInterface $token
  55.      * @return bool
  56.      */
  57.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  58.     {
  59.         /** @var User $user */
  60.         $user $token->getUser();
  61.         // if the user is anonymous, do not grant access
  62.         if (!$user instanceof UserInterface) {
  63.             return false;
  64.         }
  65.         $this->permissions $this->commons->getBackendPermissions($user);
  66.         if ($this->hasSuperUser() === true) {
  67.             return true;
  68.         }
  69.         switch ($attribute) {
  70.             case self::BACKEND_HOW_TO_START_VIEW:
  71.                 return $this->canViewList();
  72.             case self::BACKEND_HOW_TO_START_CHANGE:
  73.                 return $this->canChange();
  74.         }
  75.         throw new LogicException('Invalid attribute: ' $attribute);
  76.     }
  77.     /**
  78.      * Return True if have Super View Permission else return false
  79.      * @return bool
  80.      */
  81.     private function hasSuperUser(): bool
  82.     {
  83.         if (array_key_exists('superUser'$this->permissions)) {
  84.             if (in_array('Yes'$this->permissions['superUser'])) {
  85.                 return true;
  86.             }
  87.         }
  88.         return false;
  89.     }
  90.     /**
  91.      * Return True if have View Permission else return false
  92.      * @return bool
  93.      */
  94.     private function canViewList(): bool
  95.     {
  96.         if (array_key_exists('howToStart'$this->permissions)) {
  97.             if (in_array('View'$this->permissions['howToStart'])) {
  98.                 return true;
  99.             }
  100.         }
  101.         return false;
  102.     }
  103.     /**
  104.      * Return True if have Change Permission else return false
  105.      * @return bool
  106.      */
  107.     private function canChange(): bool
  108.     {
  109.         if (array_key_exists('howToStart'$this->permissions)) {
  110.             if (in_array('Edit'$this->permissions['howToStart'])) {
  111.                 return true;
  112.             }
  113.         }
  114.         return false;
  115.     }
  116. }