src/Security/Voter/InformativePills/InformativePillSecurityVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\InformativePills;
  3. use App\Entity\InformativePills\InformativePill;
  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. final class InformativePillSecurityVoter 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_INFORMATIVE_PILL,
  21.             VotersEnum::LIST_INFORMATIVE_PILL_ASSOCIATED,
  22.             VotersEnum::CREATE_INFORMATIVE_PILL,
  23.             VotersEnum::READ,
  24.             VotersEnum::UPDATE,
  25.             VotersEnum::DELETE,
  26.             VotersEnum::EXPORT_INFORMATIVE_PILL_REGISTRATIONS,
  27.             VotersEnum::ACTIVATE,
  28.             VotersEnum::DEACTIVATE,
  29.         ];
  30.     }
  31.     protected function supports(string $attribute$subject): bool
  32.     {
  33.         // first check the $subject and last if the $attribute is supported,
  34.         // because there are attributes (with subject) used as well by other voters (like UPDATE, ...)
  35.         if ($subject && !$subject instanceof InformativePill) {
  36.             // only vote on these objects
  37.             return false;
  38.         }
  39.         if (in_array($attribute$this->voters)) {
  40.             // if the attribute is one we support
  41.             return true;
  42.         }
  43.         return false;
  44.     }
  45.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  46.     {
  47.         $user $token->getUser();
  48.         if (!$user instanceof User) {
  49.             // the user must be logged in; if not, deny access
  50.             return false;
  51.         }
  52.         /** @var InformativePill $informativePill */
  53.         $informativePill $subject;
  54.         switch ($attribute) {
  55.             case VotersEnum::LIST_INFORMATIVE_PILL:
  56.                 return $this->canList();
  57.             case VotersEnum::LIST_INFORMATIVE_PILL_ASSOCIATED:
  58.                 return $this->canListAssociated();
  59.             case VotersEnum::CREATE_INFORMATIVE_PILL:
  60.                 return $this->canCreate();
  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_INFORMATIVE_PILL_REGISTRATIONS:
  68.                 return $this->canExport();
  69.             case VotersEnum::ACTIVATE:
  70.                 return $this->canActivate($informativePill);
  71.             case VotersEnum::DEACTIVATE:
  72.                 return $this->canDeactivate($informativePill);
  73.         }
  74.         throw new LogicException('This code should not be reached!');
  75.     }
  76.     private function canList(): bool
  77.     {
  78.         return $this->isAdminUser();
  79.     }
  80.     private function canListAssociated(): bool
  81.     {
  82.         return $this->isAdminUser()
  83.             || $this->security->isGranted(MenuRolesAssociatedEnum::ROLE_MENU_LEARNING_PLATFORM_ASOCIATED)
  84.             ;
  85.     }
  86.     private function canCreate(): bool
  87.     {
  88.         return $this->isAdminUser();
  89.     }
  90.     private function canRead(): bool
  91.     {
  92.         return $this->isAdminUser();
  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 canActivate(InformativePill $informativePill): bool
  107.     {
  108.         return $this->canUpdate() && !$informativePill->isActive();
  109.     }
  110.     private function canDeactivate(InformativePill $informativePill): bool
  111.     {
  112.         return $this->canUpdate() && $informativePill->isActive();
  113.     }
  114.     private function isAdminUser(): bool
  115.     {
  116.         return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_LEARNING_PLATFORM);
  117.     }
  118. }