<?php
namespace App\Security\Voter;
use App\Entity\Invoice;
use App\User\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* Security voter to control access to Invoice entities based on client ownership.
*/
class InvoiceVoter extends Voter
{
public const VIEW = 'view';
public const EDIT = 'edit';
public const DELETE = 'delete';
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [self::VIEW, self::EDIT, self::DELETE], true)
&& $subject instanceof Invoice;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// User must be logged in
if (!$user instanceof User) {
return false;
}
// ROLE_SUPER_USER can access everything
if (in_array('ROLE_SUPER_USER', $user->getRoles(), true)) {
return true;
}
/** @var Invoice $invoice */
$invoice = $subject;
// Get the client of the current user
$userClient = $user->getClient();
if (!$userClient) {
return false;
}
// Get the client of the invoice (through order)
$invoiceClient = $invoice->getClient();
if (!$invoiceClient) {
return false;
}
// Check if both belong to the same client
if ($userClient->getId() !== $invoiceClient->getId()) {
return false;
}
// User needs at least ROLE_MANAGER to access invoices
return in_array('ROLE_MANAGER', $user->getRoles(), true);
}
}