src/Security/Voter/SuperAdminVoter.php line 12

  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User ;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. // use Symfony\Component\Security\Core\Security;
  6. use Symfony\Bundle\SecurityBundle\Security;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Psr\Log\LoggerInterface;
  9. class SuperAdminVoter extends Voter
  10. {
  11.     const SUPER_ADMIN_ROLE "ROLE_SUPER_ADMIN";
  12.     private $logger;
  13.     public function __construct(Security $securityLoggerInterface $logger)
  14.     {
  15.         $this->logger $logger;
  16.         $this->security $security;
  17.     }
  18.     protected function supports($attribute$subject):bool
  19.     {
  20.         // if the attribute isn't one we support, return false
  21.         if ($this->security->getUser() == null || !in_array(self::SUPER_ADMIN_ROLE$this->security->getUser()->getRoles())) {
  22.             return false;
  23.         }
  24.         return true;
  25.     }
  26.     protected function voteOnAttribute($attribute$subjectTokenInterface $token):bool
  27.     {
  28.         $user $token->getUser();
  29.         $this->logger->debug('user voter voteOnAttribute: '.$user->getUserIdentifier());
  30.         if (!$user instanceof User) {
  31.             // the user must be logged in; if not, deny access
  32.             return false;
  33.         }
  34.         return $this->security->isGranted(self::SUPER_ADMIN_ROLE);
  35.         // id has user 
  36.         // throw new \LogicException('This code should not be reached!');
  37.     }
  38. }