src/Invoice/Security/InvoiceDraftVoter.php line 26

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2020 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2020 TECLA Consulting Group oü
  16.  */
  17. namespace App\Invoice\Security;
  18. use App\Contact\Security\ContactContextInterface;
  19. use App\Invoice\Model\InvoiceDraftInterface;
  20. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  21. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  22. final class InvoiceDraftVoter extends Voter
  23. {
  24.     public const UPDATE 'invoice_draft.update';
  25.     public const COPY 'invoice_draft.copy';
  26.     public const EXPORT 'invoice_draft.export';
  27.     public const SEND 'invoice_draft.send';
  28.     public const CREATE_INVOICE 'invoice_draft.create_invoice';
  29.     public const SUPPORTED_ATTRIBUTES = [
  30.         self::UPDATE,
  31.         self::COPY,
  32.         self::EXPORT,
  33.         self::SEND,
  34.         self::CREATE_INVOICE,
  35.     ];
  36.     private ContactContextInterface $contactContext;
  37.     public function __construct(ContactContextInterface $contactContext)
  38.     {
  39.         $this->contactContext $contactContext;
  40.     }
  41.     protected function supports($attribute$subject): bool
  42.     {
  43.         if (true === \in_array($attributeself::SUPPORTED_ATTRIBUTEStrue)) {
  44.             return null === $subject || true === $subject instanceof InvoiceDraftInterface;
  45.         }
  46.         return false;
  47.     }
  48.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  49.     {
  50.         switch ($attribute) {
  51.             case self::UPDATE:
  52.                 return $this->canUpdate($subject);
  53.             case self::COPY:
  54.                 return $this->canCopy($subject);
  55.             case self::EXPORT:
  56.                 return $this->canExport($subject);
  57.             case self::SEND:
  58.                 return $this->canSend($subject);
  59.             case self::CREATE_INVOICE:
  60.                 return $this->canView($subject);
  61.         }
  62.         throw new \LogicException('This code should not be reached!');
  63.     }
  64.     private function canUpdate(?InvoiceDraftInterface $invoiceDraft): bool
  65.     {
  66.         return $this->canView($invoiceDraft);
  67.     }
  68.     private function canCopy(?InvoiceDraftInterface $draft): bool
  69.     {
  70.         return $this->canView($draft);
  71.     }
  72.     private function canExport(?InvoiceDraftInterface $invoiceDraft): bool
  73.     {
  74.         return $this->canView($invoiceDraft);
  75.     }
  76.     private function canSend(?InvoiceDraftInterface $draft): bool
  77.     {
  78.         return $this->canView($draft);
  79.     }
  80.     private function canView(?InvoiceDraftInterface $invoiceDraft): bool
  81.     {
  82.         if (null === $invoiceDraft) {
  83.             return false;
  84.         }
  85.         $currentContext $this->contactContext->getCurrent();
  86.         return null !== $currentContext && $invoiceDraft->getSupplier() === $currentContext;
  87.     }
  88. }