src/Security/Voter/QuestionnaireVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Questionnaire;
  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 QuestionnaireVoter extends Voter
  9. {
  10.     private Security $security;
  11.     public function __construct(Security $security)
  12.     {
  13.         $this->security $security;
  14.     }
  15.     protected function supports(string $attribute$subject): bool
  16.     {
  17.         // replace with your own logic
  18.         // https://symfony.com/doc/current/security/voters.html
  19.         return in_array($attribute, ['DELETE'])
  20.             && $subject instanceof Questionnaire;
  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.         // ... (check conditions and return true to grant permission) ...
  30.         switch ($attribute) {
  31.             case 'DELETE':
  32.                 if ($this->security->isGranted('ROLE_ADMIN_GIRCI')) {
  33.                     return true;
  34.                 }
  35.         }
  36.         return false;
  37.     }
  38. }