src/Security/Voter/PointsCatalog/CatalogGiftCategoryVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\PointsCatalog;
  3. use App\Entity\PointsCatalog\CatalogGiftCategory;
  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 CatalogGiftCategoryVoter 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_CATALOG_GIFT_CATEGORY,
  20.             VotersEnum::CREATE_CATALOG_GIFT_CATEGORY,
  21.             VotersEnum::READ,
  22.             VotersEnum::UPDATE,
  23.             VotersEnum::DELETE,
  24.         ];
  25.     }
  26.     protected function supports(string $attribute$subject): bool
  27.     {
  28.         // first check the $subject and last if the $attribute is supported,
  29.         // because there are attributes (with subject) used as well by other voters (like UPDATE, ...)
  30.         if ($subject && !$subject instanceof CatalogGiftCategory) {
  31.             // only vote on these objects
  32.             return false;
  33.         }
  34.         if (in_array($attribute$this->voters)) {
  35.             // if the attribute is one we support
  36.             return true;
  37.         }
  38.         return false;
  39.     }
  40.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  41.     {
  42.         $user $token->getUser();
  43.         if (!$user instanceof User) {
  44.             // the user must be logged in; if not, deny access
  45.             return false;
  46.         }
  47.         switch ($attribute) {
  48.             case VotersEnum::LIST_CATALOG_GIFT_CATEGORY:
  49.                 return $this->canList();
  50.             case VotersEnum::CREATE_CATALOG_GIFT_CATEGORY:
  51.                 return $this->canCreate();
  52.             case VotersEnum::READ:
  53.                 return $this->canRead();
  54.             case VotersEnum::UPDATE:
  55.                 return $this->canUpdate();
  56.             case VotersEnum::DELETE:
  57.                 return $this->canDelete();
  58.         }
  59.         throw new LogicException('This code should not be reached!');
  60.     }
  61.     private function canList(): bool
  62.     {
  63.         return $this->isAdminUser();
  64.     }
  65.     private function canCreate(): bool
  66.     {
  67.         return $this->isAdminUser();
  68.     }
  69.     private function canRead(): bool
  70.     {
  71.         return $this->isAdminUser();
  72.     }
  73.     private function canUpdate(): bool
  74.     {
  75.         return $this->isAdminUser();
  76.     }
  77.     private function canDelete(): bool
  78.     {
  79.         return $this->isAdminUser();
  80.     }
  81.     private function canExport(): bool
  82.     {
  83.         return $this->isAdminUser();
  84.     }
  85.     private function isAdminUser(): bool
  86.     {
  87.         return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_CATALOG_POINTS);
  88.     }
  89. }