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

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\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. final class InformativePillSecurityVoter 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_INFORMATIVE_PILL,
  22.             VotersEnum::LIST_INFORMATIVE_PILL_ASSOCIATED,
  23.             VotersEnum::CREATE_INFORMATIVE_PILL,
  24.             VotersEnum::READ,
  25.             VotersEnum::UPDATE,
  26.             VotersEnum::DELETE,
  27.             VotersEnum::EXPORT_INFORMATIVE_PILL_REGISTRATIONS,
  28.             VotersEnum::ACTIVATE,
  29.             VotersEnum::DEACTIVATE,
  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 InformativePill) {
  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.         /** @var InformativePill $informativePill */
  54.         $informativePill $subject;
  55.         switch ($attribute) {
  56.             case VotersEnum::LIST_INFORMATIVE_PILL:
  57.                 return $this->canList();
  58.             case VotersEnum::LIST_INFORMATIVE_PILL_ASSOCIATED:
  59.                 return $this->canListAssociated();
  60.             case VotersEnum::CREATE_INFORMATIVE_PILL:
  61.                 return $this->canCreate();
  62.             case VotersEnum::READ:
  63.                 return $this->canRead();
  64.             case VotersEnum::UPDATE:
  65.                 return $this->canUpdate();
  66.             case VotersEnum::DELETE:
  67.                 return $this->canDelete();
  68.             case VotersEnum::EXPORT_INFORMATIVE_PILL_REGISTRATIONS:
  69.                 return $this->canExport();
  70.             case VotersEnum::ACTIVATE:
  71.                 return $this->canActivate($informativePill);
  72.             case VotersEnum::DEACTIVATE:
  73.                 return $this->canDeactivate($informativePill);
  74.         }
  75.         throw new LogicException('This code should not be reached!');
  76.     }
  77.     private function canList(): bool
  78.     {
  79.         return $this->isAdminUser();
  80.     }
  81.     private function canListAssociated(): bool
  82.     {
  83.         return $this->isAdminUser()
  84.             || $this->security->isGranted(MenuRolesAssociatedEnum::ROLE_MENU_LEARNING_PLATFORM_ASOCIATED)
  85.             || $this->security->isGranted(UserRolesEnum::ROLE_ASSOCIATED_EMPLOYEE_LONG)
  86.             ;
  87.     }
  88.     private function canCreate(): bool
  89.     {
  90.         return $this->isAdminUser();
  91.     }
  92.     private function canRead(): bool
  93.     {
  94.         return $this->isAdminUser();
  95.     }
  96.     private function canUpdate(): bool
  97.     {
  98.         return $this->isAdminUser();
  99.     }
  100.     private function canDelete(): bool
  101.     {
  102.         return $this->isAdminUser();
  103.     }
  104.     private function canExport(): bool
  105.     {
  106.         return $this->isAdminUser();
  107.     }
  108.     private function canActivate(InformativePill $informativePill): bool
  109.     {
  110.         return $this->canUpdate() && !$informativePill->isActive();
  111.     }
  112.     private function canDeactivate(InformativePill $informativePill): bool
  113.     {
  114.         return $this->canUpdate() && $informativePill->isActive();
  115.     }
  116.     private function isAdminUser(): bool
  117.     {
  118.         return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_LEARNING_PLATFORM);
  119.     }
  120. }