src/Security/Voters/MessageVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\Manufacturer;
  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. class MessageVoter extends Voter
  8. {
  9.     private Security $security;
  10.     public const CAN_SEND 'message.can_send';
  11.     private const ATTRIBUTES = [
  12.         self::CAN_SEND,
  13.     ];
  14.     public function __construct(Security $security)
  15.     {
  16.         $this->security $security;
  17.     }
  18.     protected function supports($attribute$subject): bool
  19.     {
  20.         return in_array($attributeself::ATTRIBUTES);
  21.     }
  22.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  23.     {
  24.         $user $token->getUser();
  25.         switch ($attribute) {
  26.             case self::CAN_SEND:
  27.                 return $this->canSend();
  28.         }
  29.         throw new \LogicException('Invalid attribute: '.$attribute);
  30.     }
  31.     private function canSend(): bool
  32.     {
  33.         return $this->security->isGranted('ROLE_MODULE_USERS');
  34.     }
  35. }