src/Security/Voter/OrderVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\CustomService;
  4. use App\Entity\Order;
  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 OrderVoter extends Voter
  9. {
  10.     public const VIEW = 'ORDER_VIEW';
  11.     public const EDIT = 'ORDER_EDIT';
  12.     /**
  13.      * @inheritDoc
  14.      */
  15.     protected function supports(string $attribute, mixed $subject): bool
  16.     {
  17.         // Only vote on User objects and supported attributes
  18.         return in_array($attribute, [self::VIEW, self::EDIT])
  19.             && $subject instanceof Order;
  20.     }
  21.     /**
  22.      * @inheritDoc
  23.      */
  24.     protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
  25.     {
  26.         $currentUser = $token->getUser();
  27.         if (!$currentUser instanceof UserInterface) {
  28.             return false; // not logged in
  29.         }
  30.         /** @var Order $order */
  31.         $order = $subject;
  32.         return match ($attribute) {
  33.             self::VIEW => $this->canView($order, $currentUser),
  34.             default => false,
  35.         };
  36.     }
  37.     private function canView(Order $order, UserInterface $currentUser): bool
  38.     {
  39.         // Admins can view every thing
  40.         if (in_array('ROLE_SUPER_ADMIN', $currentUser->getRoles(), true)) {
  41.             return true;
  42.         }
  43.         // Admins can view authorized orders
  44.         if (in_array('ROLE_ADMIN', $currentUser->getRoles(), true)) {
  45.             return $this->agentIsAuthorized($order, $currentUser);
  46.         }
  47.         if (in_array('ROLE_BRANCH', $currentUser->getRoles(), true)) {
  48.             return $this->agentIsAuthorized($order, $currentUser);
  49.         }
  50.         if (in_array('ROLE_COMMERCIAL', $currentUser->getRoles(), true)) {
  51.             return $this->agentIsAuthorized($order, $currentUser);
  52.         }
  53.         // B2B Admins: can view orders of their own customer
  54.         if (in_array('ROLE_B2B_ADMIN', $currentUser->getRoles(), true)) {
  55.             return $order->getCustomer() === $currentUser->getCustomer();
  56.         }
  57.         // B2B Agents can view their own orders. Custom services are created by
  58.         // the back office, so an agent may also view a custom-service order for
  59.         // the customer to which they are attached.
  60.         if (in_array('ROLE_B2B_AGENT', $currentUser->getRoles(), true)) {
  61.             return $order->getUser() === $currentUser
  62.                 || ($order->getCustomer() === $currentUser->getCustomer()
  63.                     && $this->containsCustomService($order));
  64.         }
  65.         if (in_array('ROLE_B2C', $currentUser->getRoles(), true)) {
  66.             return $order->getCustomer() === $currentUser->getCustomer();
  67.         }
  68.         return false;
  69.     }
  70.     private function containsCustomService(Order $order): bool
  71.     {
  72.         foreach ($order->getOrderLines() as $orderLine) {
  73.             if ($orderLine->getProduct() instanceof CustomService) {
  74.                 return true;
  75.             }
  76.         }
  77.         return false;
  78.     }
  79.     private function agentIsAuthorized(Order $order, UserInterface $currentUser)
  80.     {
  81.         $personal = $currentUser->getPersonal();
  82.         if (!$personal) {
  83.             return false;
  84.         }
  85.         return $personal->getAuthAgencies()->contains($order->getAgency());
  86.     }
  87. }