src/Security/Voter/FormVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Security;
  4. use Symfony\Component\Security\Core\User\UserInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. class FormVoter extends Voter
  9. {
  10.     public const EDIT 'EDIT_FORM';
  11.     public const VIEW 'VIEW_FORM';
  12.     public function __construct(public Security $security, private ParameterBagInterface $serviceParams)
  13.     {
  14.     }
  15.     
  16.     protected function supports(string $attribute$subject): bool
  17.     {
  18.         // replace with your own logic
  19.         // https://symfony.com/doc/current/security/voters.html
  20.         return in_array($attribute, [self::EDITself::VIEW]);
  21.     }
  22.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  23.     {
  24.         $user $token->getUser();
  25.         // if the user is anonymous, do not grant access
  26.         if (!$user instanceof UserInterface) {
  27.             return false;
  28.         }
  29.         $accessGranted false;
  30.         // ... (check conditions and return true to grant permission) ...
  31.         $accessGranted = match($attribute){
  32.             self::VIEW => $this->security->isGranted($this->serviceParams->get('role_admin')) or $this->security->isGranted($this->serviceParams->get('role_comercial')),
  33.             self::EDIT => $this->security->isGranted($this->serviceParams->get('role_admin')) or $this->security->isGranted($this->serviceParams->get('role_comercial'))
  34.         };
  35.         return $accessGranted;
  36.     }
  37. }