src/V4/Voters/SectionVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\V4\Voters;
  3. use App\Security\User;
  4. use LogicException;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. class SectionVoter extends Voter
  9. {
  10.     const SECTION_MANAGE_LIST 'section_manage_list';
  11.     const SECTION_SHOW_LIST 'section_show_list';
  12.     const SECTION_ADD_EDIT 'section_add_edit';
  13.     /**
  14.      * @var Security
  15.      */
  16.     private $security;
  17.     public function __construct(Security $security)
  18.     {
  19.         $this->security $security;
  20.     }
  21.     /**
  22.      * @param $attribute
  23.      * @param $subject
  24.      *
  25.      * @return bool
  26.      */
  27.     protected function supports($attribute$subject): bool
  28.     {
  29.         return in_array($attribute, [
  30.             self::SECTION_MANAGE_LIST,
  31.             self::SECTION_SHOW_LIST,
  32.             self::SECTION_ADD_EDIT,
  33.         ], true);
  34.     }
  35.     /**
  36.      * @param $attribute
  37.      * @param $subject
  38.      * @param TokenInterface $token
  39.      *
  40.      * @return bool
  41.      *
  42.      * @throws LogicException
  43.      */
  44.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  45.     {
  46.         $user $token->getUser();
  47.         if (!$user instanceof User) {
  48.             return false;
  49.         }
  50.         switch ($attribute) {
  51.             case self::SECTION_MANAGE_LIST:
  52.                 return $this->canSectionManageList();
  53.             case self::SECTION_SHOW_LIST:
  54.                 return $this->canSectionShowList();
  55.             case self::SECTION_ADD_EDIT:
  56.                 return $this->canSectionAddEdit();
  57.         }
  58.         throw new LogicException('This should never happen');
  59.     }
  60.     private function canSectionManageList(): bool
  61.     {
  62.         return $this->canAccessSection();
  63.     }
  64.     private function canSectionShowList(): bool
  65.     {
  66.         return $this->canAccessSection();
  67.     }
  68.     private function canSectionAddEdit(): bool
  69.     {
  70.         return $this->canAccessSection();
  71.     }
  72.     private function canAccessSection(): bool
  73.     {
  74.         // @todo gérer sécurité lorsque les roles seront en place
  75.         return true;
  76.     }
  77. }