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_COMMERCIAL', $currentUser->getRoles(), true)) {
  48.             return $this->agentIsAuthorized($order, $currentUser);
  49.         }
  50.         // B2B Admins: can view orders of their own customer
  51.         if (in_array('ROLE_B2B_ADMIN', $currentUser->getRoles(), true)) {
  52.             return $order->getCustomer() === $currentUser->getCustomer();
  53.         }
  54.         // B2B Agents can view their own orders. Custom services are created by
  55.         // the back office, so an agent may also view a custom-service order for
  56.         // the customer to which they are attached.
  57.         if (in_array('ROLE_B2B_AGENT', $currentUser->getRoles(), true)) {
  58.             return $order->getUser() === $currentUser
  59.                 || ($order->getCustomer() === $currentUser->getCustomer()
  60.                     && $this->containsCustomService($order));
  61.         }
  62.         if (in_array('ROLE_B2C', $currentUser->getRoles(), true)) {
  63.             return $order->getCustomer() === $currentUser->getCustomer();
  64.         }
  65.         return false;
  66.     }
  67.     private function containsCustomService(Order $order): bool
  68.     {
  69.         foreach ($order->getOrderLines() as $orderLine) {
  70.             if ($orderLine->getProduct() instanceof CustomService) {
  71.                 return true;
  72.             }
  73.         }
  74.         return false;
  75.     }
  76.     private function agentIsAuthorized(Order $order, UserInterface $currentUser)
  77.     {
  78.         $personal = $currentUser->getPersonal();
  79.         if (!$personal) {
  80.             return false;
  81.         }
  82.         return $personal->getAuthAgencies()->contains($order->getAgency());
  83.     }
  84. }