Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Maker/MakeVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -57,6 +58,7 @@ class: \sprintf('Security\Voter\%s', $input->getArgument('name')),
TokenInterface::class,
Voter::class,
UserInterface::class,
Vote::class,
]
);

Expand Down
4 changes: 3 additions & 1 deletion templates/security/Voter.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ protected function supports(string $attribute, mixed $subject): bool
&& $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();
// 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;
}

Expand Down
5 changes: 4 additions & 1 deletion tests/fixtures/make-voter/expected/FooBarVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion tests/fixtures/make-voter/expected/not_final_FooBarVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down
Loading