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

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