src/Security/Voter/ConstructionSite/ConstructionSitePurchaseOrderVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\ConstructionSite;
  3. use App\Entity\Company\User;
  4. use App\Entity\ConstructionSite\ConstructionSitePurchaseOrder;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. class ConstructionSitePurchaseOrderVoter extends Voter
  9. {
  10.     public const EDIT 'EDIT';
  11.     public const VIEW 'VIEW';
  12.     protected function supports(string $attributemixed $subject): bool
  13.     {
  14.         return in_array($attribute, [self::EDITself::VIEW])
  15.             && $subject instanceof ConstructionSitePurchaseOrder;
  16.     }
  17.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  18.     {
  19.         $user $token->getUser();
  20.         // if the user is anonymous, do not grant access
  21.         if (!$user instanceof UserInterface) {
  22.             return false;
  23.         }
  24.         return match ($attribute) {
  25.             self::VIEW => $this->canView($subject$user),
  26.             default => false
  27.         };
  28.     }
  29.     private function canView(ConstructionSitePurchaseOrder $orderUser $user)
  30.     {
  31.         return $order->getConstructionSiteOffer()->getConstructionSite()->getCustomerCompany()->getId() == $user->getCustomerCompany()->getId();
  32.     }
  33. }