src/Security/Voter/ActionVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Action;
  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. use Symfony\Component\Security\Core\User\UserInterface;
  8. class ActionVoter extends Voter
  9. {
  10.     public const EDIT 'POST_EDIT';
  11.     public const VIEW 'POST_VIEW';
  12.     private Security $security;
  13.     public function __construct(Security $security)
  14.     {
  15.         $this->security $security;
  16.     }
  17.     protected function supports(string $attribute$subject): bool
  18.     {
  19.         //echo $attribute."<br>";
  20.         // replace with your own logic
  21.         // https://symfony.com/doc/current/security/voters.html
  22.         return in_array($attribute, ['ADMIN_ACTION_EDIT'])
  23.             && $subject instanceof Action;
  24.     }
  25.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  26.     {
  27.         $user $token->getUser();
  28.         // if the user is anonymous, do not grant access
  29.         if (!$user instanceof UserInterface) {
  30.             return false;
  31.         }
  32.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  33.             return true;
  34.         }
  35.         // ... (check conditions and return true to grant permission) ...
  36.         switch ($attribute) {
  37.             case 'ADMIN_ACTION_EDIT':
  38.                 /** @var Action $subject */
  39.                 return $user === $subject->getBin()->getColumns()->getWork()->getUser() || $this->security->isGranted('ROLE_SUPER_ADMIN');;
  40.                 break;
  41.         }
  42.         return false;
  43.     }
  44. }