src/Security/Voter/PointsCatalog/CatalogPointMovementVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\PointsCatalog;
  3. use App\Entity\PointsCatalog\CatalogPointMovement;
  4. use App\Entity\User;
  5. use App\Enum\MenuRolesAssociatedEnum;
  6. use App\Enum\MenuRolesManagerEnum;
  7. use App\Enum\UserRolesEnum;
  8. use App\Enum\VotersEnum;
  9. use LogicException;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  12. use Symfony\Component\Security\Core\Security;
  13. class CatalogPointMovementVoter extends Voter
  14. {
  15.     private Security $security;
  16.     private array $voters;
  17.     public function __construct(Security $security)
  18.     {
  19.         $this->security $security;
  20.         $this->voters = [
  21.             VotersEnum::LIST_CATALOG_POINT_MOVEMENT,
  22.             VotersEnum::CREATE_CATALOG_POINT_MOVEMENT,
  23.             VotersEnum::IMPORT_CATALOG_POINT_MOVEMENT,
  24.             VotersEnum::READ,
  25.             VotersEnum::UPDATE,
  26.             VotersEnum::DELETE,
  27.             VotersEnum::EXPORT_CATALOG_POINT_MOVEMENT,
  28.             VotersEnum::CONFIG_CATALOG_POINT,
  29.             VotersEnum::GARAGES_BALANCE_CATALOG_POINT_MOVEMENT,
  30.             VotersEnum::GARAGES_BALANCE_CATALOG_POINT_MOVEMENT_ASSOCIATED,
  31.         ];
  32.     }
  33.     protected function supports(string $attribute$subject): bool
  34.     {
  35.         // first check the $subject and last if the $attribute is supported,
  36.         // because there are attributes (with subject) used as well by other voters (like UPDATE, ...)
  37.         if ($subject && !$subject instanceof CatalogPointMovement) {
  38.             // only vote on these objects
  39.             return false;
  40.         }
  41.         if (in_array($attribute$this->voters)) {
  42.             // if the attribute is one we support
  43.             return true;
  44.         }
  45.         return false;
  46.     }
  47.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  48.     {
  49.         $user $token->getUser();
  50.         if (!$user instanceof User) {
  51.             // the user must be logged in; if not, deny access
  52.             return false;
  53.         }
  54.         switch ($attribute) {
  55.             case VotersEnum::LIST_CATALOG_POINT_MOVEMENT:
  56.                 return $this->canList();
  57.             case VotersEnum::CREATE_CATALOG_POINT_MOVEMENT:
  58.                 return $this->canCreate();
  59.             case VotersEnum::IMPORT_CATALOG_POINT_MOVEMENT:
  60.                 return $this->canImport();
  61.             case VotersEnum::READ:
  62.                 return $this->canRead();
  63.             case VotersEnum::UPDATE:
  64.                 return $this->canUpdate();
  65.             case VotersEnum::DELETE:
  66.                 return $this->canDelete();
  67.             case VotersEnum::EXPORT_CATALOG_POINT_MOVEMENT:
  68.                 return $this->canExport();
  69.             case VotersEnum::CONFIG_CATALOG_POINT:
  70.                 return $this->canConfig();
  71.             case VotersEnum::GARAGES_BALANCE_CATALOG_POINT_MOVEMENT:
  72.                 return $this->canGaragesBalance();
  73.             case VotersEnum::GARAGES_BALANCE_CATALOG_POINT_MOVEMENT_ASSOCIATED:
  74.                 return $this->canGaragesBalanceAssociated();
  75.         }
  76.         throw new LogicException('This code should not be reached!');
  77.     }
  78.     private function canList(): bool
  79.     {
  80.         return $this->isAdminUser() || $this->isCoordinatorUser();
  81.     }
  82.     private function canCreate(): bool
  83.     {
  84.         return $this->isAdminUser();
  85.     }
  86.     private function canImport(): bool
  87.     {
  88.         return $this->isAdminUser();
  89.     }
  90.     private function canRead(): bool
  91.     {
  92.         return $this->isAdminUser() || $this->isCoordinatorUser();
  93.     }
  94.     private function canUpdate(): bool
  95.     {
  96.         return $this->isAdminUser();
  97.     }
  98.     private function canDelete(): bool
  99.     {
  100.         return $this->isAdminUser();
  101.     }
  102.     private function canExport(): bool
  103.     {
  104.         return $this->isAdminUser();
  105.     }
  106.     private function canConfig(): bool
  107.     {
  108.         return $this->isAdminUser();
  109.     }
  110.     private function canGaragesBalance(): bool
  111.     {
  112.         return $this->isAdminUser() || $this->isCoordinatorUser();
  113.     }
  114.     private function canGaragesBalanceAssociated(): bool
  115.     {
  116.         return $this->isAssociatedUser();
  117.     }
  118.     private function isAssociatedUser(): bool
  119.     {
  120.         return $this->security->isGranted(MenuRolesAssociatedEnum::ROLE_MENU_CATALOG_POINTS_ASSOCIATED);
  121.     }
  122.     private function isCoordinatorUser(): bool
  123.     {
  124.         return $this->security->isGranted(UserRolesEnum::ROLE_COORDINATOR_LONG);
  125.     }
  126.     private function isAdminUser(): bool
  127.     {
  128.         return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_CATALOG_POINTS);
  129.     }
  130. }