src/Security/Voter/PointsCatalog/CatalogFaqVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\PointsCatalog;
  3. use App\Entity\PointsCatalog\CatalogFaq;
  4. use App\Entity\User;
  5. use App\Enum\MenuRolesAssociatedEnum;
  6. use App\Enum\MenuRolesManagerEnum;
  7. use App\Enum\VotersEnum;
  8. use LogicException;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\Security;
  12. class CatalogFaqVoter extends Voter
  13. {
  14.     private Security $security;
  15.     private array $voters;
  16.     public function __construct(Security $security)
  17.     {
  18.         $this->security $security;
  19.         $this->voters = [
  20.             VotersEnum::LIST_CATALOG_FAQ,
  21.             VotersEnum::LIST_CATALOG_FAQ_ASSOCIATED,
  22.             VotersEnum::CREATE_CATALOG_FAQ,
  23.             VotersEnum::READ,
  24.             VotersEnum::UPDATE,
  25.             VotersEnum::DELETE,
  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 CatalogFaq) {
  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_CATALOG_FAQ:
  51.                 return $this->canList();
  52.             case VotersEnum::LIST_CATALOG_FAQ_ASSOCIATED:
  53.                 return $this->canListAssociated();
  54.             case VotersEnum::CREATE_CATALOG_FAQ:
  55.                 return $this->canCreate();
  56.             case VotersEnum::READ:
  57.                 return $this->canRead();
  58.             case VotersEnum::UPDATE:
  59.                 return $this->canUpdate();
  60.             case VotersEnum::DELETE:
  61.                 return $this->canDelete();
  62.         }
  63.         throw new LogicException('This code should not be reached!');
  64.     }
  65.     private function canList(): bool
  66.     {
  67.         return $this->isAdminUser();
  68.     }
  69.     private function canListAssociated(): bool
  70.     {
  71.         return $this->isAssociatedUser();
  72.     }
  73.     private function canCreate(): bool
  74.     {
  75.         return $this->isAdminUser();
  76.     }
  77.     private function canRead(): bool
  78.     {
  79.         return $this->isAdminUser();
  80.     }
  81.     private function canUpdate(): bool
  82.     {
  83.         return $this->isAdminUser();
  84.     }
  85.     private function canDelete(): bool
  86.     {
  87.         return $this->isAdminUser();
  88.     }
  89.     private function isAssociatedUser(): bool
  90.     {
  91.         return $this->security->isGranted(MenuRolesAssociatedEnum::ROLE_MENU_CATALOG_POINTS_ASSOCIATED);
  92.     }
  93.     private function isAdminUser(): bool
  94.     {
  95.         return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_CATALOG_POINTS);
  96.     }
  97. }