src/Security/Voter/InvoiceVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\DataTransferObject\Invoice;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class InvoiceVoter extends Voter
  8. {
  9.     public const VIEW 'VIEW';
  10.     /**
  11.      * @param string $attribute
  12.      * @param mixed  $subject
  13.      *
  14.      * @return bool
  15.      */
  16.     protected function supports($attribute$subject)
  17.     {
  18.         if ($attribute !== static::VIEW) {
  19.             return false;
  20.         }
  21.         if (!$subject instanceof Invoice) {
  22.             return false;
  23.         }
  24.         return true;
  25.     }
  26.     /**
  27.      * @param string $attribute
  28.      * @param mixed  $subject
  29.      *
  30.      * @return bool
  31.      */
  32.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  33.     {
  34.         $user $token->getUser();
  35.         if (!$user instanceof User) {
  36.             return false;
  37.         }
  38.         if (static::VIEW === $attribute) {
  39.             return $this->canView($subject$user);
  40.         }
  41.         return false;
  42.     }
  43.     protected function canView(Invoice $invoiceUser $user): bool
  44.     {
  45.         if ($user->isMainAccount()) {
  46.             return $invoice->getCustomerNumber() === $user->getCustomerNumber();
  47.         }
  48.         $intersection array_intersect($invoice->getContractNumbers(), $user->getContactInformation()->getContractNumbers());
  49.         return count($intersection) > 0;
  50.     }
  51. }