src/Security/Voter/MediaLibrary/CooperativeMediaCategoryFamilyVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\MediaLibrary;
  3. use App\Entity\MediaLibrary\CooperativeMediaCategoryFamily;
  4. use App\Entity\User;
  5. use App\Enum\MenuRolesManagerEnum;
  6. use App\Enum\UserRolesEnum;
  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 CooperativeMediaCategoryFamilyVoter 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_COOPERATIVE_MEDIA_CATEGORY_FAMILY,
  21.             VotersEnum::CREATE_COOPERATIVE_MEDIA_CATEGORY_FAMILY,
  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 CooperativeMediaCategoryFamily) {
  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_COOPERATIVE_MEDIA_CATEGORY_FAMILY:
  50.                 return $this->canList();
  51.             case VotersEnum::CREATE_COOPERATIVE_MEDIA_CATEGORY_FAMILY:
  52.                 return $this->canCreate();
  53.             case VotersEnum::READ:
  54.                 return $this->canRead();
  55.             case VotersEnum::UPDATE:
  56.                 return $this->canUpdate();
  57.             case VotersEnum::DELETE:
  58.                 return $this->canDelete();
  59.         }
  60.         throw new LogicException('This code should not be reached!');
  61.     }
  62.     private function canList(): bool
  63.     {
  64.         return $this->isAdminUser();
  65.     }
  66.     private function canCreate(): bool
  67.     {
  68.         return $this->isAdminUser();
  69.     }
  70.     private function canRead(): bool
  71.     {
  72.         return $this->isAdminUser();
  73.     }
  74.     private function canUpdate(): bool
  75.     {
  76.         return $this->isAdminUser();
  77.     }
  78.     private function canDelete(): bool
  79.     {
  80.         return $this->isAdminUser();
  81.     }
  82.     private function isAdminUser(): bool
  83.     {
  84.         return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_MEDIA_LIBRARY);
  85.     }
  86. }