src/BillingBundle/Security/InvoiceVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace BillingBundle\Security;
  3. use AppBundle\Entity\User;
  4. use BillingBundle\Entity\Invoice;
  5. use BillingBundle\Enum\InvoiceStatusEnum;
  6. use BillingBundle\Enum\PayableInvoiceStatusEnum;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. /**
  11.  * Class InvoiceVoter
  12.  */
  13. class InvoiceVoter extends Voter
  14. {
  15.     const EDIT 'EDIT';
  16.     const VIEW 'VIEW';
  17.     const SAVE_TO_DRIVE 'SAVE_TO_DRIVE';
  18.     const CAN_UPDATE_STATUS_IN_PAYED 'CAN_UPDATE_STATUS_IN_PAYED';
  19.     const CAN_UPDATE_STATUS_IN_WAITING_FOR_PAYMENT 'CAN_UPDATE_STATUS_IN_WAITING_FOR_PAYMENT';
  20.     /**
  21.      * @var AccessDecisionManagerInterface
  22.      */
  23.     private $decisionManager;
  24.     /**
  25.      * DocumentWorkflowVoter constructor.
  26.      * @param AccessDecisionManagerInterface $decisionManager
  27.      */
  28.     public function __construct(
  29.         AccessDecisionManagerInterface $decisionManager
  30.     ) {
  31.         $this->decisionManager $decisionManager;
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     protected function supports($attribute$subject)
  37.     {
  38.         return $subject instanceof Invoice && in_array($attribute, [
  39.             self::EDIT,
  40.             self::VIEW,
  41.             self::SAVE_TO_DRIVE,
  42.             self::CAN_UPDATE_STATUS_IN_PAYED,
  43.             self::CAN_UPDATE_STATUS_IN_WAITING_FOR_PAYMENT,
  44.         ], true);
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      * @param Invoice $subject
  49.      */
  50.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  51.     {
  52.         $user $token->getUser();
  53.         if (!$user instanceof User) {
  54.             return false;
  55.         }
  56.         switch ($attribute) {
  57.             case self::EDIT:
  58.                 return InvoiceStatusEnum::DRAFT === $subject->getStatus();
  59.             case self::VIEW:
  60.                 return InvoiceStatusEnum::DRAFT !== $subject->getStatus();
  61.             case self::SAVE_TO_DRIVE:
  62.                 return in_array($subject->getStatus(), PayableInvoiceStatusEnum::getInvoicesWithPdfStatuses());
  63.             case self::CAN_UPDATE_STATUS_IN_PAYED:
  64.                 return $this->canUpdateStatusInPayed($subject$token);
  65.             case self::CAN_UPDATE_STATUS_IN_WAITING_FOR_PAYMENT:
  66.                 return $this->canUpdateStatusInWaitingForPayment($subject$token);
  67.             default:
  68.                 return false;
  69.         }
  70.     }
  71.     /**
  72.      * Check whether invoice can be updated in waiting for payment status or not
  73.      *
  74.      * @param Invoice $invoice
  75.      * @param TokenInterface $token
  76.      * @return boolean
  77.      */
  78.     protected function canUpdateStatusInWaitingForPayment(Invoice $invoiceTokenInterface $token)
  79.     {
  80.         if (!$this->decisionManager->decide($token, ['ROLE_VALIDATE_INVOICE'])) {
  81.             return false;
  82.         }
  83.         $canBeUpdated true;
  84.         if (!(
  85.             !$invoice->isAsset() &&
  86.             ($invoice->isValidated() || ($invoice->isDraft() && $invoice->hasGeneratedFile())) &&
  87.             !$invoice->isFeePayedByTrust())
  88.         ) {
  89.             $canBeUpdated false;
  90.         }
  91.         return $canBeUpdated;
  92.     }
  93.     /**
  94.      * Check whether invoice can be updated in payed status or not
  95.      *
  96.      * @param Invoice $invoice
  97.      * @param TokenInterface $token
  98.      * @return boolean
  99.      */
  100.     protected function canUpdateStatusInPayed(Invoice $invoiceTokenInterface $token)
  101.     {
  102.         if (!$this->decisionManager->decide($token, ['ROLE_VALIDATE_INVOICE'])) {
  103.             return false;
  104.         }
  105.         $canBeUpdated true;
  106.         // Pour pouvoir passer en statut soldé il faut que la facture d'avoir
  107.         // soit validé ou en proforma avec le pdf généré
  108.         if (!(
  109.             ($invoice->isAsset() && ($invoice->isValidated() || ($invoice->isDraft() && $invoice->hasGeneratedFile()))) || ($invoice->isFeePayedByTrust() && ($invoice->isValidated() || ($invoice->isDraft() && $invoice->hasGeneratedFile())))
  110.             )) {
  111.             $canBeUpdated false;
  112.         }
  113.         return $canBeUpdated;
  114.     }
  115. }