src/Voters/CampaignVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Voters;
  3. use App\Security\SecurityConfig;
  4. use App\Security\User;
  5. use LogicException;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. class CampaignVoter extends Voter
  10. {
  11.     const CAMPAIGN_CREATE 'campaign_create';
  12.     /**
  13.      * @var Security
  14.      */
  15.     private $security;
  16.     public function __construct(Security $security)
  17.     {
  18.         $this->security $security;
  19.     }
  20.     protected function supports($attribute$subject): bool
  21.     {
  22.         return in_array($attribute, [
  23.             self::CAMPAIGN_CREATE,
  24.         ], true);
  25.     }
  26.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  27.     {
  28.         $user $token->getUser();
  29.         if (!$user instanceof User) {
  30.             return false;
  31.         }
  32.         switch ($attribute) {
  33.             case self::CAMPAIGN_CREATE:
  34.                 return $this->canCreateCampaigns();
  35.         }
  36.         throw new LogicException('This should never happen');
  37.     }
  38.     private function canAccessModuleCampaign(): bool
  39.     {
  40.         return $this->security->isGranted(SecurityConfig::MODULE_CAMPAIGN);
  41.     }
  42.     private function canCreateCampaigns(): bool
  43.     {
  44.         return $this->canAccessModuleCampaign();
  45.     }
  46. }