src/Security/Voter/InvoiceVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Invoice;
  4. use App\Entity\User;
  5. use App\Repository\InvoiceRepository;
  6. use App\Utils\Commons;
  7. use LogicException;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. class InvoiceVoter extends Voter
  13. {
  14.     const INVOICE_VIEW 'INVOICE_VIEW';
  15.     const INVOICE_SHOW 'INVOICE_SHOW';
  16.     /**
  17.      * @var Security
  18.      */
  19.     private $security;
  20.     /**
  21.      * @var Commons
  22.      */
  23.     private $commons;
  24.     /**
  25.      * @var array
  26.      */
  27.     private $permissions;
  28.     /**
  29.      * @var InvoiceRepository
  30.      */
  31.     private $invoiceRepository;
  32.     /**
  33.      * SupportVoter constructor.
  34.      * @param Security $security
  35.      * @param Commons $commons
  36.      * @param InvoiceRepository $invoiceRepository
  37.      */
  38.     public function __construct(Security $securityCommons $commonsInvoiceRepository $invoiceRepository)
  39.     {
  40.         $this->security $security;
  41.         $this->commons $commons;
  42.         $this->invoiceRepository $invoiceRepository;
  43.         $this->permissions = [];
  44.     }
  45.     /**
  46.      * @param string $attribute
  47.      * @param mixed $subject
  48.      * @return bool
  49.      */
  50.     protected function supports($attribute$subject): bool
  51.     {
  52.         // if the attribute isn't one we support, return false
  53.         if (!in_array($attribute,
  54.             [self::INVOICE_VIEWself::INVOICE_SHOW]
  55.         )) {
  56.             return false;
  57.         }
  58.         return true;
  59.     }
  60.     /**
  61.      * @param string $attribute
  62.      * @param mixed $subject
  63.      * @param TokenInterface $token
  64.      * @return bool
  65.      */
  66.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  67.     {
  68.         /** @var User $user */
  69.         $user $token->getUser();
  70.         // if the user is anonymous, do not grant access
  71.         if (!$user instanceof UserInterface) {
  72.             return false;
  73.         }
  74.         $this->permissions $this->commons->getUserPermissions($user);
  75.         // ... (check conditions and return true to grant permission) ...
  76.         switch ($attribute) {
  77.             case self::INVOICE_VIEW:
  78.                 return $this->canView();
  79.             case self::INVOICE_SHOW:
  80.                 return ($this->canShow() && $this->isOwner($user$subject));
  81.         }
  82.         throw new LogicException('Invalid attribute: ' $attribute);
  83.     }
  84.     /**
  85.      * @param User|null $user
  86.      * @param $subject
  87.      * @return bool
  88.      */
  89.     private function isOwner(User $user$subject): bool
  90.     {
  91.         if (!is_object($subject)) {
  92.             $subject $this->invoiceRepository->findOneBy(["id" => intval($subject)]);
  93.         }
  94.         if (!$subject instanceof Invoice) {
  95.             return false;
  96.         }
  97.         return $user->getWorkspace()->getId() === $subject->getWorkspace()->getId();
  98.     }
  99.     /**
  100.      * Return True if have View Permission else return false
  101.      * @return bool
  102.      */
  103.     private function canView(): bool
  104.     {
  105.         if ($this->security->isGranted('ROLE_ADMIN')) {
  106.             return true;
  107.         }
  108.         if (array_key_exists('orders'$this->permissions)) {
  109.             if (in_array('View'$this->permissions['orders'])) {
  110.                 return true;
  111.             }
  112.         }
  113.         return false;
  114.     }
  115.     /**
  116.      * Return True if have Show Permission else return false
  117.      * @return bool
  118.      */
  119.     private function canShow(): bool
  120.     {
  121.         if ($this->security->isGranted('ROLE_ADMIN')) {
  122.             return true;
  123.         }
  124.         if (array_key_exists('orders'$this->permissions)) {
  125.             if (in_array('Show'$this->permissions['orders'])) {
  126.                 return true;
  127.             }
  128.         }
  129.         return false;
  130.     }
  131. }