src/V4/Voters/TabVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\V4\Voters;
  3. use App\Security\User;
  4. use App\V4\Model\Tab\Tab;
  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. class TabVoter extends Voter
  10. {
  11.     const TAB_MANAGE_LIST 'tab_manage_list';
  12.     const TAB_SHOW_LIST 'tab_show_list';
  13.     const TAB_SHOW_LIST_ADMIN 'tab_show_list_admin';
  14.     const TAB_ADD_EDIT 'tab_add_edit';
  15.     const TAB_SHOW 'tab_show';
  16.     const TAB_SHOW_ADMIN 'tab_show_admin';
  17.     /**
  18.      * @var Security
  19.      */
  20.     private $security;
  21.     public function __construct(Security $security)
  22.     {
  23.         $this->security $security;
  24.     }
  25.     /**
  26.      * @param $attribute
  27.      * @param $subject
  28.      *
  29.      * @return bool
  30.      */
  31.     protected function supports($attribute$subject): bool
  32.     {
  33.         if ($subject instanceof Tab && in_array($attribute, [
  34.                 self::TAB_SHOW,
  35.                 self::TAB_SHOW_ADMIN,
  36.             ], true)) {
  37.             return true;
  38.         }
  39.         return in_array($attribute, [
  40.             self::TAB_MANAGE_LIST,
  41.             self::TAB_SHOW_LIST,
  42.             self::TAB_SHOW_LIST_ADMIN,
  43.             self::TAB_ADD_EDIT,
  44.         ], true);
  45.     }
  46.     /**
  47.      * @param $attribute
  48.      * @param $subject
  49.      * @param TokenInterface $token
  50.      *
  51.      * @return bool
  52.      *
  53.      * @throws LogicException
  54.      */
  55.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  56.     {
  57.         $user $token->getUser();
  58.         if (!$user instanceof User) {
  59.             return false;
  60.         }
  61.         switch ($attribute) {
  62.             case self::TAB_MANAGE_LIST:
  63.                 return $this->canTabManageList();
  64.             case self::TAB_SHOW_LIST:
  65.                 return $this->canTabShowList();
  66.             case self::TAB_SHOW_LIST_ADMIN:
  67.                 return $this->canTabShowListAdmin($user);
  68.             case self::TAB_ADD_EDIT:
  69.                 return $this->canTabAddEdit();
  70.             case self::TAB_SHOW:
  71.                 return $this->canAccessTab($user$subject);
  72.             case self::TAB_SHOW_ADMIN:
  73.                 return $this->canAccessTabAdmin($user$subject);
  74.         }
  75.         throw new LogicException('This should never happen');
  76.     }
  77.     /**
  78.      * @return bool
  79.      */
  80.     private function canTabManageList(): bool
  81.     {
  82.         return true;
  83.     }
  84.     /**
  85.      * @return bool
  86.      */
  87.     private function canTabShowList(): bool
  88.     {
  89.         return true;
  90.     }
  91.     /**
  92.      * @param User $user
  93.      *
  94.      * @return bool
  95.      */
  96.     private function canTabShowListAdmin(User $user): bool
  97.     {
  98.         return $user->isAdmin();
  99.     }
  100.     /**
  101.      * @return bool
  102.      */
  103.     private function canTabAddEdit(): bool
  104.     {
  105.         return true;
  106.     }
  107.     /**
  108.      * @param User $user
  109.      * @param Tab  $tab
  110.      *
  111.      * @return bool
  112.      */
  113.     private function canAccessTab(User $userTab $tab): bool
  114.     {
  115.         return null === $tab->getRole() || in_array($tab->getRole(), $user->getRoles(), true);
  116.     }
  117.     /**
  118.      * @param User $user
  119.      * @param Tab  $tab
  120.      *
  121.      * @return bool
  122.      */
  123.     private function canAccessTabAdmin(User $userTab $tab): bool
  124.     {
  125.         return $user->isAdmin();
  126.     }
  127. }