src/Security/Voter/DemandTreeVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\DemandTree;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class DemandTreeVoter extends Voter
  8. {
  9.     protected function supports(string $attribute$subject): bool
  10.     {
  11.         // replace with your own logic
  12.         // https://symfony.com/doc/current/security/voters.html
  13.         return in_array($attribute, ['DEMAND_TREE_FILE_UPLOAD'], true)
  14.             && $subject instanceof DemandTree;
  15.     }
  16.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  17.     {
  18.         $user $token->getUser();
  19.         // if the user is anonymous, do not grant access
  20.         if (!$user instanceof User) {
  21.             return false;
  22.         }
  23.         /** @var DemandTree $demandTreeFile */
  24.         $demandTreeFile $subject;
  25.         // ... (check conditions and return true to grant permission) ...
  26.         switch ($attribute) {
  27.             case 'DEMAND_TREE_FILE_UPLOAD':
  28.                 return ($demandTreeFile->getDemand()->getUser() === $user)
  29.                     || in_array('ROLE_ADMIN'$user->getRoles(), true);
  30.         }
  31.         return false;
  32.     }
  33. }