<?php
namespace App\Security\Voter\OnlineShop;
use App\Entity\OnlineShop\Order;
use App\Entity\User;
use App\Enum\MenuRolesAssociatedEnum;
use App\Enum\MenuRolesManagerEnum;
use App\Enum\UserRolesEnum;
use App\Enum\VotersEnum;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
final class OrderSecurityVoter extends Voter
{
private Security $security;
private array $voters;
public function __construct(Security $security)
{
$this->security = $security;
$this->voters = [
VotersEnum::LIST_ORDER,
VotersEnum::LIST_ORDER_ASSOCIATED,
VotersEnum::LIST_ORDER_COORDINATOR,
VotersEnum::READ,
VotersEnum::UPDATE,
VotersEnum::DELETE,
VotersEnum::EXPORT_ORDER,
VotersEnum::LIST_SUPPLIER_PROMOTIONS,
VotersEnum::LIST_RATES_ONLINESHOP,
VotersEnum::LIST_PRODUCTS_ONLINESHOP,
VotersEnum::PLACE_ORDER_ONLINESHOP,
VotersEnum::PROCESS,
VotersEnum::REOPEN,
];
}
protected function supports(string $attribute, $subject): bool
{
// first check the $subject and last if the $attribute is supported,
// because there are attributes (with subject) used as well by other voters (like UPDATE, ...)
if ($subject && !$subject instanceof Order) {
// only vote on these objects
return false;
}
if (in_array($attribute, $this->voters)) {
// if the attribute is one we support
return true;
}
return false;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
switch ($attribute) {
case VotersEnum::LIST_ORDER:
return $this->canList();
case VotersEnum::LIST_ORDER_ASSOCIATED:
return $this->canListAssociated();
case VotersEnum::LIST_ORDER_COORDINATOR:
return $this->canListCoordinator();
case VotersEnum::READ:
return $this->canRead();
case VotersEnum::UPDATE:
return $this->canUpdate($subject);
case VotersEnum::DELETE:
return $this->canDelete();
case VotersEnum::EXPORT_ORDER:
return $this->canExport();
case VotersEnum::LIST_SUPPLIER_PROMOTIONS:
return $this->canListSupplierPromotions();
case VotersEnum::LIST_RATES_ONLINESHOP:
return $this->canListRates();
case VotersEnum::LIST_PRODUCTS_ONLINESHOP:
return $this->canListProducts();
case VotersEnum::PLACE_ORDER_ONLINESHOP:
return $this->canPlaceOrder();
case VotersEnum::PROCESS:
return $this->canProcessOrder($subject);
case VotersEnum::REOPEN:
return $this->canReopenOrder($subject);
}
throw new LogicException('This code should not be reached!');
}
private function canList(): bool
{
return $this->isAdminUser();
}
private function canListAssociated(): bool
{
return $this->isAssociatedUser() || $this->isProviderUser();
}
private function canListCoordinator(): bool
{
return $this->isCoordinatorUser();
}
private function canRead(): bool
{
return $this->isAdminUser()
|| $this->isCoordinatorUser()
|| $this->isAssociatedUser()
|| $this->isProviderUser()
;
}
private function canUpdate(Order $order): bool
{
return $this->isAdminUser() && $order->isPending();
}
private function canDelete(): bool
{
return $this->isAdminUser();
}
private function canExport(): bool
{
return $this->isAdminUser();
}
private function canListSupplierPromotions(): bool
{
return $this->isAdminUser()
|| $this->isCoordinatorUser()
|| $this->isAssociatedUser()
;
}
private function canListRates(): bool
{
return $this->isAdminUser()
|| $this->isCoordinatorUser()
|| $this->isAssociatedUser()
;
}
private function canListProducts(): bool
{
return $this->isAdminUser()
|| $this->isCoordinatorUser()
|| $this->isAssociatedUser()
|| $this->isProviderUser()
;
}
private function canPlaceOrder(): bool
{
return $this->isAdminUser()
|| $this->isCoordinatorUser()
|| $this->isAssociatedUser()
|| $this->isProviderUser()
;
}
private function canProcessOrder(Order $order): bool
{
return $this->isAdminUser() && $order->isPending();
}
private function canReopenOrder(Order $order): bool
{
return $this->isAdminUser() && $order->isPending();
}
private function isAssociatedUser(): bool
{
return $this->security->isGranted(MenuRolesAssociatedEnum::ROLE_MENU_ONLINE_SHOP_ASSOCIATED);
}
private function isProviderUser(): bool
{
return $this->security->isGranted(UserRolesEnum::ROLE_PROVIDER_LONG);
}
private function isCoordinatorUser(): bool
{
return $this->security->isGranted(UserRolesEnum::ROLE_COORDINATOR_LONG);
}
private function isAdminUser(): bool
{
return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_ONLINE_SHOP);
}
}