src/Security/Voter/RequirementsVoter.php line 15

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\Requirements;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use App\Model\Constants\Modules;
  9. use App\Security\ACLManager;
  10. class RequirementsVoter extends Voter {
  11.     
  12.     private $acl;
  13.     
  14.     public function __construct(ACLManager $acl) {
  15.         $this->acl $acl;
  16.     }
  17.     
  18.     protected function supports($attribute$subject) {
  19.         //replace with your own logic
  20.         //https://symfony.com/doc/current/security/voters.html
  21.         return in_array($attribute, ['GET_REQUIREMENT']) && $subject instanceof Requirements;
  22.     }
  23.     
  24.     protected function voteOnAttribute($attribute$subjectTokenInterface $token) {
  25.         
  26.         $user $token->getUser();
  27.         //if the user is anonymous, do not grant access
  28.         if (!$user instanceof UserInterface) {
  29.             return false;
  30.         }
  31.         
  32.         // ... (check conditions and return true to grant permission) ...
  33.         switch ($attribute) {
  34.             case 'GET_REQUIREMENT':
  35.                 //get access from database
  36.                 $hasAccess $this->acl->hasAccessToModule($userModules::COOP_REQUESTS);
  37.                 
  38.                 if ($hasAccess) {
  39.                     return true;
  40.                 }
  41.                 
  42.                 return false;
  43.                 break;
  44.         }
  45.         
  46.         throw new \Exception(sprintf('Unhandled attribute "%s"'$attribute));
  47.     }
  48. }