src/Security/Voter/BackendTeamVoter.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 BackendTeamVoter extends Voter
  11. {
  12.     const TEAM_VIEW 'TEAM_VIEW';
  13.     const TEAM_SHOW 'TEAM_SHOW';
  14.     const TEAM_CREATE 'TEAM_CREATE';
  15.     const TEAM_EDIT 'TEAM_EDIT';
  16.     const TEAM_DELETE 'TEAM_DELETE';
  17.     /**
  18.      * @var Security
  19.      */
  20.     private $security;
  21.     /**
  22.      * @var Commons
  23.      */
  24.     private $commons;
  25.     /**
  26.      * @var array
  27.      */
  28.     private $permissions;
  29.     public function __construct(Security $securityCommons $commons)
  30.     {
  31.         $this->security $security;
  32.         $this->commons $commons;
  33.         $this->permissions = [];
  34.     }
  35.     /**
  36.      * @param string $attribute
  37.      * @param mixed $subject
  38.      * @return bool
  39.      */
  40.     protected function supports($attribute$subject): bool
  41.     {
  42.         if (!in_array($attribute, [self::TEAM_VIEWself::TEAM_SHOWself::TEAM_EDITself::TEAM_CREATEself::TEAM_DELETE])) {
  43.             return false;
  44.         }
  45.         return true;
  46.     }
  47.     /**
  48.      * @param string $attribute
  49.      * @param mixed $subject
  50.      * @param TokenInterface $token
  51.      * @return bool
  52.      */
  53.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  54.     {
  55.         /** @var User $user */
  56.         $user $token->getUser();
  57.         // if the user is anonymous, do not grant access
  58.         if (!$user instanceof UserInterface) {
  59.             return false;
  60.         }
  61.         $this->permissions $this->commons->getBackendPermissions($user);
  62.         if ($this->hasSuperUser() === true) {
  63.             return true;
  64.         }
  65.         switch ($attribute) {
  66.             case self::TEAM_VIEW:
  67.                 return $this->canView();
  68.             case self::TEAM_CREATE:
  69.                 return $this->canCreate();
  70.             case self::TEAM_EDIT:
  71.                 return $this->canEdit();
  72.             case self::TEAM_DELETE:
  73.                 return $this->canDelete();
  74.             case self::TEAM_SHOW:
  75.                 return $this->canShow();
  76.         }
  77.         throw new LogicException('Invalid attribute: ' $attribute);
  78.     }
  79.     /**
  80.      * Return True if have Super View Permission else return false
  81.      * @return bool
  82.      */
  83.     private function hasSuperUser(): bool
  84.     {
  85.         if (array_key_exists('superUser'$this->permissions)) {
  86.             if (in_array('Yes'$this->permissions['superUser'])) {
  87.                 return true;
  88.             }
  89.         }
  90.         return false;
  91.     }
  92.     /**
  93.      * Return True if have View Permission else return false
  94.      * @return bool
  95.      */
  96.     private function canView(): bool
  97.     {
  98.         if (array_key_exists('teams'$this->permissions)) {
  99.             if (in_array('View'$this->permissions['teams'])) {
  100.                 return true;
  101.             }
  102.         }
  103.         return false;
  104.     }
  105.     /**
  106.      * Return True if have Show Permission else return false
  107.      * @return bool
  108.      */
  109.     private function canShow(): bool
  110.     {
  111.         if (array_key_exists('teams'$this->permissions)) {
  112.             if (in_array('Show'$this->permissions['teams'])) {
  113.                 return true;
  114.             }
  115.         }
  116.         return false;
  117.     }
  118.     /**
  119.      * Return True if have Edit Permission else return false
  120.      * @return bool
  121.      */
  122.     private function canEdit(): bool
  123.     {
  124.         if (array_key_exists('teams'$this->permissions)) {
  125.             if (in_array('Edit'$this->permissions['teams'])) {
  126.                 return true;
  127.             }
  128.         }
  129.         return false;
  130.     }
  131.     /**
  132.      * Return True if have Create Permission else return false
  133.      * @return bool
  134.      */
  135.     private function canCreate(): bool
  136.     {
  137.         if (array_key_exists('teams'$this->permissions)) {
  138.             if (in_array('Create'$this->permissions['teams'])) {
  139.                 return true;
  140.             }
  141.         }
  142.         return false;
  143.     }
  144.     /**
  145.      * Return True if have Delete Permission else return false
  146.      * @return bool
  147.      */
  148.     private function canDelete(): bool
  149.     {
  150.         if (array_key_exists('teams'$this->permissions)) {
  151.             if (in_array('Delete'$this->permissions['teams'])) {
  152.                 return true;
  153.             }
  154.         }
  155.         return false;
  156.     }
  157. }