src/Security/Voter/Whatsapp/WhatsappMessageTemplateVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Whatsapp;
  3. use App\Entity\User;
  4. use App\Entity\Whatsapp\WhatsappMessageTemplate;
  5. use App\Enum\UserRolesEnum;
  6. use App\Enum\VotersEnum;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. class WhatsappMessageTemplateVoter extends Voter
  11. {
  12.     private Security $security;
  13.     private array $voters;
  14.     public function __construct(Security $security)
  15.     {
  16.         $this->security $security;
  17.         $this->voters = [
  18.             VotersEnum::LIST_WHATSAPP_MESSAGE_TEMPLATE,
  19.             VotersEnum::CREATE_WHATSAPP_MESSAGE_TEMPLATE,
  20.             VotersEnum::READ,
  21.             VotersEnum::UPDATE,
  22.             VotersEnum::DELETE,
  23.         ];
  24.     }
  25.     protected function supports(string $attribute$subject): bool
  26.     {
  27.         // first check the $subject and last if the $attribute is supported,
  28.         // because there are attributes (with subject) used as well by other voters (like UPDATE, ...)
  29.         if ($subject && !$subject instanceof WhatsappMessageTemplate) {
  30.             // only vote on these objects
  31.             return false;
  32.         }
  33.         if (in_array($attribute$this->voters)) {
  34.             // if the attribute is one we support
  35.             return true;
  36.         }
  37.         return false;
  38.     }
  39.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  40.     {
  41.         $user $token->getUser();
  42.         if (!$user instanceof User) {
  43.             // the user must be logged in; if not, deny access
  44.             return false;
  45.         }
  46.         switch ($attribute) {
  47.             case VotersEnum::LIST_WHATSAPP_MESSAGE_TEMPLATE:
  48.                 return $this->canlist();
  49.             case VotersEnum::CREATE_WHATSAPP_MESSAGE_TEMPLATE:
  50.                 return $this->canCreate();
  51.             case VotersEnum::READ:
  52.                 return $this->canRead();
  53.             case VotersEnum::UPDATE:
  54.                 return $this->canUpdate();
  55.             case VotersEnum::DELETE:
  56.                 return $this->canDelete();
  57.         }
  58.         throw new \LogicException('This code should not be reached!');
  59.     }
  60.     private function canList(): bool
  61.     {
  62.         return $this->isAdminUser();
  63.     }
  64.     private function canCreate(): bool
  65.     {
  66.         return $this->isAdminUser();
  67.     }
  68.     private function canRead(): bool
  69.     {
  70.         return $this->isAdminUser();
  71.     }
  72.     private function canUpdate(): bool
  73.     {
  74.         return $this->isAdminUser();
  75.     }
  76.     private function canDelete(): bool
  77.     {
  78.         return $this->isAdminUser();
  79.     }
  80.     private function isAdminUser(): bool
  81.     {
  82.         return $this->security->isGranted(UserRolesEnum::ROLE_ADMIN_LONG);
  83.     }
  84. }