Skip to content

Commit f171762

Browse files
ayyoub-afwallahAyyoub AFW-ALLAH
authored andcommitted
[Make:Voter] Add Missing Vote param in voteOnAttribute()
1 parent eba3045 commit f171762

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

src/Maker/MakeVoter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Console\Input\InputArgument;
2121
use Symfony\Component\Console\Input\InputInterface;
2222
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
23+
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
2324
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
2425
use Symfony\Component\Security\Core\User\UserInterface;
2526

@@ -57,6 +58,7 @@ class: \sprintf('Security\Voter\%s', $input->getArgument('name')),
5758
TokenInterface::class,
5859
Voter::class,
5960
UserInterface::class,
61+
Vote::class,
6062
]
6163
);
6264

templates/security/Voter.tpl.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ protected function supports(string $attribute, mixed $subject): bool
1818
&& $subject instanceof \App\Entity\<?= str_replace('Voter', null, $class_data->getClassName()) ?>;
1919
}
2020

21-
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
21+
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token, ?Vote $vote = null): bool
2222
{
2323
$user = $token->getUser();
2424
// if the user is anonymous, do not grant access
2525
if (!$user instanceof UserInterface) {
26+
$vote?->addReason('The user must be logged in to access this resource.');
27+
2628
return false;
2729
}
2830

tests/fixtures/make-voter/expected/FooBarVoter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Security\Voter;
44

55
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
6+
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
67
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
78
use Symfony\Component\Security\Core\User\UserInterface;
89

@@ -19,12 +20,14 @@ protected function supports(string $attribute, mixed $subject): bool
1920
&& $subject instanceof \App\Entity\FooBar;
2021
}
2122

22-
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
23+
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token, ?Vote $vote = null): bool
2324
{
2425
$user = $token->getUser();
2526

2627
// if the user is anonymous, do not grant access
2728
if (!$user instanceof UserInterface) {
29+
$vote?->addReason('The user must be logged in to access this resource.');
30+
2831
return false;
2932
}
3033

tests/fixtures/make-voter/expected/not_final_FooBarVoter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Security\Voter;
44

55
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
6+
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
67
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
78
use Symfony\Component\Security\Core\User\UserInterface;
89

@@ -19,12 +20,14 @@ protected function supports(string $attribute, mixed $subject): bool
1920
&& $subject instanceof \App\Entity\FooBar;
2021
}
2122

22-
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
23+
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token, ?Vote $vote = null): bool
2324
{
2425
$user = $token->getUser();
2526

2627
// if the user is anonymous, do not grant access
2728
if (!$user instanceof UserInterface) {
29+
$vote?->addReason('The user must be logged in to access this resource.');
30+
2831
return false;
2932
}
3033

0 commit comments

Comments
 (0)