src/Voter/__Accounting/AccountingPaymentVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Voter\__Accounting;
  3. use App\Entity\Accounting\Payment;
  4. use App\Entity\Booking\Booking;
  5. use App\Entity\ParameterCodes;
  6. use App\Voter\VoterTrait;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. class AccountingPaymentVoter extends Voter
  11. {
  12.     use VoterTrait;
  13.     const LIST = "ACCOUNTING_PAYMENT_LIST";
  14.     const CREATE "ACCOUNTING_PAYMENT_CREATE";
  15.     const UPDATE "ACCOUNTING_PAYMENT_UPDATE";
  16.     const DELETE "ACCOUNTING_PAYMENT_DELETE";
  17.     const SEND_ACK "ACCOUNTING_PAYMENT_SEND_ACK";
  18.     protected function supports($attribute$subject): bool
  19.     {
  20.         return ($attribute == self::LIST && $subject === null) ||
  21.             ($attribute == self::CREATE && $subject instanceof Booking) ||
  22.             (in_array($attribute, [self::UPDATEself::DELETEself::SEND_ACK]) && $subject instanceof Payment);
  23.     }
  24.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  25.     {
  26.         $loggedUser $token->getUser();
  27.         if (!$loggedUser instanceof UserInterface) {
  28.             return false;
  29.         }
  30.         switch ($attribute) {
  31.             case self::LIST:
  32.                 return $this->voteOnList($loggedUser);
  33.             case self::CREATE:
  34.                 return $this->voteOnCreate($subject$loggedUser);
  35.             case self::UPDATE:
  36.                 return $this->voteOnUpdate($subject$loggedUser);
  37.             case self::DELETE:
  38.                 return $this->voteOnDelete($subject$loggedUser);
  39.             case self::SEND_ACK:
  40.                 return $this->voteOnSendAck($subject$loggedUser);
  41.         }
  42.         return false;
  43.     }
  44.     private function voteOnList(UserInterface $loggedUser): bool
  45.     {
  46.         return in_array(self::LIST, $loggedUser->getRoles());
  47.     }
  48.     private function voteOnCreate(?Booking $bookingUserInterface $loggedUser): bool
  49.     {
  50.         if (null === $booking) {
  51.             return false;
  52.         }
  53.         return in_array(self::CREATE$loggedUser->getRoles());
  54.     }
  55.     private function voteOnUpdate(?Payment $paymentUserInterface $loggedUser): bool
  56.     {
  57.         if (null === $payment) {
  58.             return false;
  59.         }
  60.         if ($payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_OGONE ||
  61.             $payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_WORLDLINE ||
  62.             $payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_FLOA) {
  63.             return false;
  64.         }
  65.         return in_array(self::UPDATE$loggedUser->getRoles());
  66.     }
  67.     private function voteOnDelete(?Payment $paymentUserInterface $loggedUser): bool
  68.     {
  69.         if (null === $payment) {
  70.             return false;
  71.         }
  72.         if ($payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_OGONE ||
  73.             $payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_WORLDLINE ||
  74.             $payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_FLOA) {
  75.             return false;
  76.         }
  77.         return in_array(self::DELETE$loggedUser->getRoles());
  78.     }
  79.     private function voteOnSendAck(?Payment $paymentUserInterface $loggedUser): bool
  80.     {
  81.         if (null === $payment) {
  82.             return false;
  83.         }
  84.         if ($payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_OGONE ||
  85.             $payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_WORLDLINE ||
  86.             $payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_FLOA) {
  87.             return false;
  88.         }
  89.         return in_array(self::SEND_ACK$loggedUser->getRoles());
  90.     }
  91. }