<?php
namespace App\Security\Voter\ConstructionSite;
use App\Entity\Company\User;
use App\Entity\ConstructionSite\ConstructionSitePurchaseOrder;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class ConstructionSitePurchaseOrderVoter extends Voter
{
public const EDIT = 'EDIT';
public const VIEW = 'VIEW';
protected function supports(string $attribute, mixed $subject): bool
{
return in_array($attribute, [self::EDIT, self::VIEW])
&& $subject instanceof ConstructionSitePurchaseOrder;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
return match ($attribute) {
self::VIEW => $this->canView($subject, $user),
default => false
};
}
private function canView(ConstructionSitePurchaseOrder $order, User $user)
{
return $order->getConstructionSiteOffer()->getConstructionSite()->getCustomerCompany()->getId() == $user->getCustomerCompany()->getId();
}
}