src/Voter/__Accounting/AccountingInsurancePriceVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Voter\__Accounting;
  3. use App\Entity\__Accounting\InsurancePrice;
  4. use App\Entity\Product\ProductVariant;
  5. use App\Voter\VoterTrait;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class AccountingInsurancePriceVoter extends Voter
  10. {
  11.     use VoterTrait;
  12.     const LIST = "ACCOUNTING_INSURANCE_PRICE_LIST";
  13.     const CREATE "ACCOUNTING_INSURANCE_PRICE_CREATE";
  14.     const UPDATE "ACCOUNTING_INSURANCE_PRICE_UPDATE";
  15.     const DELETE "ACCOUNTING_INSURANCE_PRICE_DELETE";
  16.     protected function supports($attribute$subject): bool
  17.     {
  18.         return $attribute == self::LIST ||
  19.             ($attribute == self::CREATE && $subject instanceof ProductVariant) ||
  20.             (in_array($attribute, [self::UPDATEself::DELETE]) && $subject instanceof InsurancePrice);
  21.     }
  22.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  23.     {
  24.         $loggedUser $token->getUser();
  25.         if (!$loggedUser instanceof UserInterface) {
  26.             return false;
  27.         }
  28.         switch ($attribute) {
  29.             case self::LIST:
  30.                 return $this->voteOnList($loggedUser);
  31.             case self::CREATE:
  32.                 return $this->voteOnCreate($subject$loggedUser);
  33.             case self::UPDATE:
  34.                 return $this->voteOnUpdate($subject$loggedUser);
  35.             case self::DELETE:
  36.                 return $this->voteOnDelete($subject$loggedUser);
  37.         }
  38.         return false;
  39.     }
  40.     private function voteOnList(UserInterface $loggedUser): bool
  41.     {
  42.         return in_array(self::LIST, $loggedUser->getRoles());
  43.     }
  44.     private function voteOnCreate(?ProductVariant $productVariantUserInterface $loggedUser): bool
  45.     {
  46.         if (null === $productVariant) {
  47.             return false;
  48.         }
  49.         return in_array(self::CREATE$loggedUser->getRoles());
  50.     }
  51.     private function voteOnUpdate(?InsurancePrice $insurancePriceUserInterface $loggedUser): bool
  52.     {
  53.         if (null === $insurancePrice) {
  54.             return false;
  55.         }
  56.         return in_array(self::UPDATE$loggedUser->getRoles());
  57.     }
  58.     private function voteOnDelete(?InsurancePrice $insurancePriceUserInterface $loggedUser): bool
  59.     {
  60.         if (null === $insurancePrice) {
  61.             return false;
  62.         }
  63.         return in_array(self::DELETE$loggedUser->getRoles());
  64.     }
  65. }