src/Security/Voter/Promotions/NationalPromotionVoter.php line 14

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