From e61fecb2ae8bc467e5ac2f24db16d468f788d555 Mon Sep 17 00:00:00 2001 From: Ayyoub AFW-ALLAH Date: Sat, 13 Dec 2025 22:23:37 +0100 Subject: [PATCH] [Make:Voter] Add Missing Vote param in `voteOnAttribute()` --- src/Maker/MakeVoter.php | 2 ++ templates/security/Voter.tpl.php | 4 +++- tests/fixtures/make-voter/expected/FooBarVoter.php | 5 ++++- tests/fixtures/make-voter/expected/not_final_FooBarVoter.php | 5 ++++- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Maker/MakeVoter.php b/src/Maker/MakeVoter.php index 0e12f28f1..b32696e2a 100644 --- a/src/Maker/MakeVoter.php +++ b/src/Maker/MakeVoter.php @@ -20,6 +20,7 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; 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\User\UserInterface; @@ -57,6 +58,7 @@ class: \sprintf('Security\Voter\%s', $input->getArgument('name')), TokenInterface::class, Voter::class, UserInterface::class, + Vote::class, ] ); diff --git a/templates/security/Voter.tpl.php b/templates/security/Voter.tpl.php index 2488efd38..084f4fe66 100644 --- a/templates/security/Voter.tpl.php +++ b/templates/security/Voter.tpl.php @@ -18,11 +18,13 @@ protected function supports(string $attribute, mixed $subject): bool && $subject instanceof \App\Entity\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(); // if the user is anonymous, do not grant access if (!$user instanceof UserInterface) { + $vote?->addReason('The user must be logged in to access this resource.'); + return false; } diff --git a/tests/fixtures/make-voter/expected/FooBarVoter.php b/tests/fixtures/make-voter/expected/FooBarVoter.php index 2cfc1ee22..7b13f3705 100644 --- a/tests/fixtures/make-voter/expected/FooBarVoter.php +++ b/tests/fixtures/make-voter/expected/FooBarVoter.php @@ -3,6 +3,7 @@ namespace App\Security\Voter; 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\User\UserInterface; @@ -19,12 +20,14 @@ protected function supports(string $attribute, mixed $subject): bool && $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(); // if the user is anonymous, do not grant access if (!$user instanceof UserInterface) { + $vote?->addReason('The user must be logged in to access this resource.'); + return false; } diff --git a/tests/fixtures/make-voter/expected/not_final_FooBarVoter.php b/tests/fixtures/make-voter/expected/not_final_FooBarVoter.php index e38fb4ad2..92d4901b9 100644 --- a/tests/fixtures/make-voter/expected/not_final_FooBarVoter.php +++ b/tests/fixtures/make-voter/expected/not_final_FooBarVoter.php @@ -3,6 +3,7 @@ namespace App\Security\Voter; 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\User\UserInterface; @@ -19,12 +20,14 @@ protected function supports(string $attribute, mixed $subject): bool && $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(); // if the user is anonymous, do not grant access if (!$user instanceof UserInterface) { + $vote?->addReason('The user must be logged in to access this resource.'); + return false; }