src/Security/Voter/RequestVoter.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 App\Entity\RemovalRequest;
  7. class RequestVoter extends Voter
  8. {
  9.     protected function supports($attribute$subject)
  10.     {
  11.         // replace with your own logic
  12.         // https://symfony.com/doc/current/security/voters.html
  13.         return in_array($attribute, ['create''update','delete','view'])
  14.             && $subject instanceof \App\Entity\RemovalRequest;
  15.     }
  16.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  17.     {
  18.         $user $token->getUser();
  19.         // if the user is anonymous, do not grant access
  20.         if (!$user instanceof UserInterface) {
  21.             return false;
  22.         }
  23.         $enterprise $user->getEnterprise();
  24.         if ($user->getRoles()[0] == "ROLE_ADMIN") { return true;
  25.         }
  26.         // ... (check conditions and return true to grant permission) ...
  27.         switch ($attribute) {
  28.         case 'create':
  29.             if($subject->getCustomer() == $enterprise) { return true;
  30.             }
  31.             break;
  32.         case 'update':
  33.             if($subject->getCustomer() == $enterprise ) { return true;
  34.             }
  35.             break;
  36.         case 'delete':
  37.             if($subject->getCustomer() == $enterprise && $subject->getStatus() == RemovalRequest::STATUT_DRAFT ) { return true;
  38.             }
  39.             break;
  40.         case 'view':
  41.             if($subject->getCustomer() == $enterprise 
  42.             { 
  43.                 return true;
  44.             }
  45.             break;
  46.         }
  47.         return false;
  48.     }
  49. }