<?php
namespace App\Security\Voter;
use App\Entity\Company\User;
use App\Entity\Invoice\InvoicePayment;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class InvoicePaymentVoter extends Voter
{
public const EDIT = 'EDIT';
public const CREATE = 'CREATE';
public const DELETE = 'DELETE';
protected function supports(string $attribute, mixed $subject): bool
{
return $subject instanceof InvoicePayment;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
return match ($attribute) {
self::EDIT => $this->canPerformSimpleAction($subject, $user),
self::VIEW => $this->canPerformSimpleAction($subject, $user),
self::DELETE => $this->canPerformSimpleAction($subject, $user),
};
}
/**
*
* @param Invoice $invoice
* @param User $user
* @return boolean
*/
private function canPerformSimpleAction(InvoicePayment $invoicePayment, User $user): bool
{
return $invoicePayment->getInvoice()->getCompany()->getId() == $user->getCompany()->getId();
}
}