[Make:Voter] Add Missing Vote param in voteOnAttribute()

This commit is contained in:
Ayyoub AFW-ALLAH
2025-12-13 22:23:37 +01:00
committed by Jérôme Tamarelle
parent 372357599a
commit 8aec4857cb
4 changed files with 13 additions and 3 deletions

View File

@@ -20,6 +20,7 @@ use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
use Symfony\Component\Security\Core\Authorization\Voter\Voter; use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
@@ -57,6 +58,7 @@ final class MakeVoter extends AbstractMaker
TokenInterface::class, TokenInterface::class,
Voter::class, Voter::class,
UserInterface::class, UserInterface::class,
Vote::class,
] ]
); );

View File

@@ -18,11 +18,13 @@ namespace <?= $class_data->getNamespace(); ?>;
&& $subject instanceof \App\Entity\<?= str_replace('Voter', null, $class_data->getClassName()) ?>; && $subject instanceof \App\Entity\<?= str_replace('Voter', null, $class_data->getClassName()) ?>;
} }
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token, ?Vote $vote = null): bool
{ {
$user = $token->getUser(); $user = $token->getUser();
// if the user is anonymous, do not grant access // if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) { if (!$user instanceof UserInterface) {
$vote?->addReason('The user must be logged in to access this resource.');
return false; return false;
} }

View File

@@ -3,6 +3,7 @@
namespace App\Security\Voter; namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
use Symfony\Component\Security\Core\Authorization\Voter\Voter; use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
@@ -19,12 +20,14 @@ final class FooBarVoter extends Voter
&& $subject instanceof \App\Entity\FooBar; && $subject instanceof \App\Entity\FooBar;
} }
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token, ?Vote $vote = null): bool
{ {
$user = $token->getUser(); $user = $token->getUser();
// if the user is anonymous, do not grant access // if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) { if (!$user instanceof UserInterface) {
$vote?->addReason('The user must be logged in to access this resource.');
return false; return false;
} }

View File

@@ -3,6 +3,7 @@
namespace App\Security\Voter; namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
use Symfony\Component\Security\Core\Authorization\Voter\Voter; use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
@@ -19,12 +20,14 @@ class FooBarVoter extends Voter
&& $subject instanceof \App\Entity\FooBar; && $subject instanceof \App\Entity\FooBar;
} }
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token, ?Vote $vote = null): bool
{ {
$user = $token->getUser(); $user = $token->getUser();
// if the user is anonymous, do not grant access // if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) { if (!$user instanceof UserInterface) {
$vote?->addReason('The user must be logged in to access this resource.');
return false; return false;
} }