src/Security/Voter/Extranet/AlertVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Extranet;
  3. use App\Entity\Extranet\Alert;
  4. use App\Entity\User;
  5. use App\Enum\UserRolesEnum;
  6. use App\Enum\VotersEnum;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. class AlertVoter extends Voter
  11. {
  12.     private Security $security;
  13.     private array $voters;
  14.     public function __construct(Security $security)
  15.     {
  16.         $this->security $security;
  17.         $this->voters = [
  18.             VotersEnum::LIST_ALERT,
  19.             VotersEnum::CREATE_ALERT,
  20.             VotersEnum::READ,
  21.             VotersEnum::UPDATE,
  22.             VotersEnum::DELETE,
  23.             VotersEnum::EXPORT_ALERT,
  24.         ];
  25.     }
  26.     protected function supports(string $attribute$subject): bool
  27.     {
  28.         // first check the $subject and last if the $attribute is supported,
  29.         // because there are attributes (with subject) used as well by other voters (like UPDATE, ...)
  30.         if ($subject && !$subject instanceof Alert) {
  31.             // only vote on these objects
  32.             return false;
  33.         }
  34.         if (in_array($attribute$this->voters)) {
  35.             // if the attribute is one we support
  36.             return true;
  37.         }
  38.         return false;
  39.     }
  40.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  41.     {
  42.         $user $token->getUser();
  43.         if (!$user instanceof User) {
  44.             // the user must be logged in; if not, deny access
  45.             return false;
  46.         }
  47.         switch ($attribute) {
  48.             case VotersEnum::LIST_ALERT:
  49.                 return $this->canList();
  50.             case VotersEnum::CREATE_ALERT:
  51.                 return $this->canCreate();
  52.             case VotersEnum::READ:
  53.                 return $this->canRead();
  54.             case VotersEnum::UPDATE:
  55.                 return $this->canUpdate();
  56.             case VotersEnum::DELETE:
  57.                 return $this->canDelete();
  58.             case VotersEnum::EXPORT_ALERT:
  59.                 return $this->canExport();
  60.             case VotersEnum::ACCEPT:
  61.                 return $this->canAccept();
  62.         }
  63.         throw new \LogicException('This code should not be reached!');
  64.     }
  65.     private function canList(): bool
  66.     {
  67.         return $this->isAdminUser();
  68.     }
  69.     private function canCreate(): bool
  70.     {
  71.         return $this->isAdminUser();
  72.     }
  73.     private function canRead(): bool
  74.     {
  75.         return $this->isAdminUser();
  76.     }
  77.     private function canUpdate(): bool
  78.     {
  79.         return $this->isAdminUser();
  80.     }
  81.     private function canDelete(): bool
  82.     {
  83.         return $this->isAdminUser();
  84.     }
  85.     private function canExport(): bool
  86.     {
  87.         return $this->isAdminUser();
  88.     }
  89.     private function canAccept(): bool
  90.     {
  91.         return $this->security->isGranted(UserRolesEnum::ROLE_USER_LONG);
  92.     }
  93.     private function isAdminUser(): bool
  94.     {
  95.         return $this->security->isGranted(UserRolesEnum::ROLE_ADMIN_LONG);
  96.     }
  97. }