src/Security/Voter/GeneralVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use Symfony\Component\Security\Core\Security;
  7. class GeneralVoter extends Voter {
  8.   private $security;
  9.   public function __construct(Security $security) {
  10.     $this->security $security;
  11.   }
  12.   protected function supports($attribute$subject) {
  13.     // replace with your own logic
  14.     // https://symfony.com/doc/current/security/voters.html
  15.     return in_array($attribute, ['_EDIT''_VIEW''_CREATE''_DELETE']);
  16.   }
  17.   protected function voteOnAttribute($attribute$subjectTokenInterface $token) {
  18.     $user $token->getUser();
  19.     // if the user is anonymous, do not grant access
  20.     if (!$user instanceof UserInterface) {
  21.       return false;
  22.     }
  23.     if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  24.       return true;
  25.     }
  26.     if (in_array($subject '_ALL'$user->getRoles())) {
  27.       return true;
  28.     }
  29.     if (in_array(strtoupper($subject $attribute), $user->getRoles())) {
  30.       return true;
  31.     }
  32.     return false;
  33.   }
  34. }