src/Security/Voter/PointsCatalog/CatalogGiftVoter.php line 17

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