src/Security/MenuVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use Psr\Log\LoggerInterface;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Security;
  7. /**
  8.  * メニューアクセス権限所持チェック
  9.  * 
  10.  */
  11. class MenuVoter extends Voter
  12. {
  13.     private $security;
  14.     public function __construct(Security $security)
  15.     {
  16.         $this->security $security;
  17.     }
  18.     protected function supports(string $attribute$subject)
  19.     {
  20.         return strpos(strtoupper($attribute), "MENU_") !== false;
  21.     }
  22.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token):bool
  23.     {
  24.         if($this->security->isGranted('ROLE_ADMIN'))
  25.         {
  26.             return true;
  27.         }
  28.         
  29.         if(in_array(preg_replace("/^MENU_/"""$attribute), $token->getAttribute('menu'))) return true;
  30.         
  31.         return false;
  32.     }
  33. }