src/Voter/__Accounting/AccountingInsuranceVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Voter\__Accounting;
  3. use App\Voter\VoterTrait;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class AccountingInsuranceVoter extends Voter
  8. {
  9.     use VoterTrait;
  10.     const LIST = "ACCOUNTING_INSURANCE_LIST";
  11.     const EXPORT "ACCOUNTING_INSURANCE_EXPORT";
  12.     protected function supports($attribute$subject): bool
  13.     {
  14.         return (in_array($attribute, [self::LIST, self::EXPORT]) && $subject === null);
  15.     }
  16.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  17.     {
  18.         $loggedUser $token->getUser();
  19.         if (!$loggedUser instanceof UserInterface) {
  20.             return false;
  21.         }
  22.         switch ($attribute) {
  23.             case self::LIST:
  24.                 return $this->voteOnList($loggedUser);
  25.             case self::EXPORT:
  26.                 return $this->voteOnExport($loggedUser);
  27.         }
  28.         return false;
  29.     }
  30.     private function voteOnList(UserInterface $loggedUser): bool
  31.     {
  32.         return in_array(self::LIST, $loggedUser->getRoles());
  33.     }
  34.     private function voteOnExport(UserInterface $loggedUser): bool
  35.     {
  36.         return in_array(self::EXPORT$loggedUser->getRoles());
  37.     }
  38. }