src/Security/Voter/OnlineShop/OrderSecurityVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\OnlineShop;
  3. use App\Entity\OnlineShop\Order;
  4. use App\Entity\User;
  5. use App\Enum\MenuRolesAssociatedEnum;
  6. use App\Enum\MenuRolesManagerEnum;
  7. use App\Enum\UserRolesEnum;
  8. use App\Enum\VotersEnum;
  9. use LogicException;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  12. use Symfony\Component\Security\Core\Security;
  13. final class OrderSecurityVoter extends Voter
  14. {
  15.     private Security $security;
  16.     private array $voters;
  17.     public function __construct(Security $security)
  18.     {
  19.         $this->security $security;
  20.         $this->voters = [
  21.             VotersEnum::LIST_ORDER,
  22.             VotersEnum::LIST_ORDER_ASSOCIATED,
  23.             VotersEnum::LIST_ORDER_COORDINATOR,
  24.             VotersEnum::READ,
  25.             VotersEnum::UPDATE,
  26.             VotersEnum::DELETE,
  27.             VotersEnum::EXPORT_ORDER,
  28.             VotersEnum::LIST_SUPPLIER_PROMOTIONS,
  29.             VotersEnum::LIST_RATES_ONLINESHOP,
  30.             VotersEnum::LIST_PRODUCTS_ONLINESHOP,
  31.             VotersEnum::PLACE_ORDER_ONLINESHOP,
  32.             VotersEnum::PROCESS,
  33.             VotersEnum::REOPEN,
  34.         ];
  35.     }
  36.     protected function supports(string $attribute$subject): bool
  37.     {
  38.         // first check the $subject and last if the $attribute is supported,
  39.         // because there are attributes (with subject) used as well by other voters (like UPDATE, ...)
  40.         if ($subject && !$subject instanceof Order) {
  41.             // only vote on these objects
  42.             return false;
  43.         }
  44.         if (in_array($attribute$this->voters)) {
  45.             // if the attribute is one we support
  46.             return true;
  47.         }
  48.         return false;
  49.     }
  50.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  51.     {
  52.         $user $token->getUser();
  53.         if (!$user instanceof User) {
  54.             // the user must be logged in; if not, deny access
  55.             return false;
  56.         }
  57.         switch ($attribute) {
  58.             case VotersEnum::LIST_ORDER:
  59.                 return $this->canList();
  60.             case VotersEnum::LIST_ORDER_ASSOCIATED:
  61.                 return $this->canListAssociated();
  62.             case VotersEnum::LIST_ORDER_COORDINATOR:
  63.                 return $this->canListCoordinator();
  64.             case VotersEnum::READ:
  65.                 return $this->canRead();
  66.             case VotersEnum::UPDATE:
  67.                 return $this->canUpdate($subject);
  68.             case VotersEnum::DELETE:
  69.                 return $this->canDelete();
  70.             case VotersEnum::EXPORT_ORDER:
  71.                 return $this->canExport();
  72.             case VotersEnum::LIST_SUPPLIER_PROMOTIONS:
  73.                 return $this->canListSupplierPromotions();
  74.             case VotersEnum::LIST_RATES_ONLINESHOP:
  75.                 return $this->canListRates();
  76.             case VotersEnum::LIST_PRODUCTS_ONLINESHOP:
  77.                 return $this->canListProducts();
  78.             case VotersEnum::PLACE_ORDER_ONLINESHOP:
  79.                 return $this->canPlaceOrder();
  80.             case VotersEnum::PROCESS:
  81.                 return $this->canProcessOrder($subject);
  82.             case VotersEnum::REOPEN:
  83.                 return $this->canReopenOrder($subject);
  84.         }
  85.         throw new LogicException('This code should not be reached!');
  86.     }
  87.     private function canList(): bool
  88.     {
  89.         return $this->isAdminUser();
  90.     }
  91.     private function canListAssociated(): bool
  92.     {
  93.         return $this->isAssociatedUser() || $this->isProviderUser();
  94.     }
  95.     private function canListCoordinator(): bool
  96.     {
  97.         return $this->isCoordinatorUser();
  98.     }
  99.     private function canRead(): bool
  100.     {
  101.         return $this->isAdminUser()
  102.             || $this->isCoordinatorUser()
  103.             || $this->isAssociatedUser()
  104.             || $this->isProviderUser()
  105.             ;
  106.     }
  107.     private function canUpdate(Order $order): bool
  108.     {
  109.         return $this->isAdminUser() && $order->isPending();
  110.     }
  111.     private function canDelete(): bool
  112.     {
  113.         return $this->isAdminUser();
  114.     }
  115.     private function canExport(): bool
  116.     {
  117.         return $this->isAdminUser();
  118.     }
  119.     private function canListSupplierPromotions(): bool
  120.     {
  121.         return $this->isAdminUser()
  122.             || $this->isCoordinatorUser()
  123.             || $this->isAssociatedUser()
  124.             ;
  125.     }
  126.     private function canListRates(): bool
  127.     {
  128.         return $this->isAdminUser()
  129.             || $this->isCoordinatorUser()
  130.             || $this->isAssociatedUser()
  131.             ;
  132.     }
  133.     private function canListProducts(): bool
  134.     {
  135.         return $this->isAdminUser()
  136.             || $this->isCoordinatorUser()
  137.             || $this->isAssociatedUser()
  138.             || $this->isProviderUser()
  139.             ;
  140.     }
  141.     private function canPlaceOrder(): bool
  142.     {
  143.         return $this->isAdminUser()
  144.             || $this->isCoordinatorUser()
  145.             || $this->isAssociatedUser()
  146.             || $this->isProviderUser()
  147.             ;
  148.     }
  149.     private function canProcessOrder(Order $order): bool
  150.     {
  151.         return $this->isAdminUser() && $order->isPending();
  152.     }
  153.     private function canReopenOrder(Order $order): bool
  154.     {
  155.         return $this->isAdminUser() && $order->isPending();
  156.     }
  157.     private function isAssociatedUser(): bool
  158.     {
  159.         return $this->security->isGranted(MenuRolesAssociatedEnum::ROLE_MENU_ONLINE_SHOP_ASSOCIATED);
  160.     }
  161.     private function isProviderUser(): bool
  162.     {
  163.         return $this->security->isGranted(UserRolesEnum::ROLE_PROVIDER_LONG);
  164.     }
  165.     private function isCoordinatorUser(): bool
  166.     {
  167.         return $this->security->isGranted(UserRolesEnum::ROLE_COORDINATOR_LONG);
  168.     }
  169.     private function isAdminUser(): bool
  170.     {
  171.         return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_ONLINE_SHOP);
  172.     }
  173. }