<?php
namespace App\Security\Voter\PointsCatalog;
use App\Entity\Garages\Garage;
use App\Entity\PointsCatalog\CatalogOrder;
use App\Entity\Province;
use App\Entity\User;
use App\Enum\MenuRolesAssociatedEnum;
use App\Enum\MenuRolesManagerEnum;
use App\Enum\UserRolesEnum;
use App\Enum\VotersEnum;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class CatalogOrderVoter extends Voter
{
private Security $security;
private array $voters;
public function __construct(Security $security)
{
$this->security = $security;
$this->voters = [
VotersEnum::LIST_CATALOG_ORDER,
VotersEnum::VIEW,
VotersEnum::DELETE,
VotersEnum::PROCESS,
VotersEnum::REOPEN,
VotersEnum::EXPORT_CATALOG_ORDER,
];
}
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 CatalogOrder) {
// 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_CATALOG_ORDER:
return $this->canList();
case VotersEnum::VIEW:
return $this->canView($subject, $user);
case VotersEnum::DELETE:
return $this->canDelete($subject, $user);
case VotersEnum::PROCESS:
return $this->canProcessOrder($subject);
case VotersEnum::REOPEN:
return $this->canReopenOrder($subject);
case VotersEnum::EXPORT_CATALOG_ORDER:
return $this->canExport();
}
throw new \LogicException('This code should not be reached!');
}
private function canList(): bool
{
return $this->isAdminUser()
|| $this->isCoordinatorUser()
|| $this->isAssociatedManagerUser()
|| $this->isAssociatedUser();
}
private function canView(CatalogOrder $catalogOrder, User $user): bool
{
return $this->isAdminUser()
|| $this->isCoordinatorUser()
|| $this->isAssociatedManagerUser()
|| ($this->isAssociatedUser() && $this->isOwner($catalogOrder, $user));
}
private function canDelete(CatalogOrder $catalogOrder, User $user): bool
{
return $this->isOwner($catalogOrder, $user) && $catalogOrder->isPending();
}
private function canProcessOrder(CatalogOrder $catalogOrder): bool
{
return $this->isAdminUser() && $catalogOrder->isPending();
}
private function canReopenOrder(CatalogOrder $catalogOrder): bool
{
return $this->isAdminUser() && $catalogOrder->isPending();
}
private function canExport(): bool
{
return $this->isAdminUser();
}
private function isOwner(CatalogOrder $catalogOrder, User $user): bool
{
if ($this->isAdminUser()) {
return true;
} elseif ($this->security->isGranted(UserRolesEnum::ROLE_COORDINATOR_LONG)) {
/** @var Province $province */
foreach ($user->getWorkProvinces() as $province) {
if ($catalogOrder->getGarage()->getAddress() && $catalogOrder->getGarage()->getAddress()->getProvince() && $province->getId() === $catalogOrder->getGarage()->getAddress()->getProvince()->getId()) {
return true;
}
}
} elseif ($user->hasRole(UserRolesEnum::ROLE_ASSOCIATED_LONG) || ($user->hasRole(UserRolesEnum::ROLE_ASSOCIATED_MANAGER_LONG) && !$user->hasRole(MenuRolesManagerEnum::ROLE_MENU_CATALOG_POINTS))) {
// orders make by admins take their own user id, instead of the garage user id =/=> $result = $catalogOrder->getUser()->getId() === $user->getId();
/** @var Garage $garage */
foreach ($user->getGarages() as $garage) {
if ($catalogOrder->getGarage()->getId() === $garage->getId()) {
return true;
}
}
}
return false;
}
private function isAssociatedUser(): bool
{
return $this->security->isGranted(MenuRolesAssociatedEnum::ROLE_MENU_CATALOG_POINTS_ASSOCIATED);
}
private function isAssociatedManagerUser(): bool
{
return $this->security->isGranted(UserRolesEnum::ROLE_ASSOCIATED_MANAGER_LONG);
}
private function isCoordinatorUser(): bool
{
return $this->security->isGranted(UserRolesEnum::ROLE_COORDINATOR_LONG);
}
private function isAdminUser(): bool
{
return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_CATALOG_POINTS);
}
}