src/Security/Voter/MediaLibrary/CooperativeMediaVoter.php line 16

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