You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: security.rst
+10-2Lines changed: 10 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -2346,12 +2346,13 @@ Granting Anonymous Users Access in a Custom Voter
2346
2346
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2347
2347
2348
2348
If you're using a :doc:`custom voter </security/voters>`, you can allow
2349
-
anonymous users access by checking if there is no user set on the token::
2349
+
anonymous users access by checking if the token is an instance of :class:`Symfony\\Component\\Security\\Core\\Authentication\\Token\\NullToken`::
2350
2350
2351
2351
// src/Security/PostVoter.php
2352
2352
namespace App\Security;
2353
2353
2354
2354
// ...
2355
+
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
2355
2356
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2356
2357
use Symfony\Component\Security\Core\Authentication\User\UserInterface;
2357
2358
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
@@ -2364,14 +2365,21 @@ anonymous users access by checking if there is no user set on the token::
2364
2365
{
2365
2366
// ...
2366
2367
2367
-
if (!$token->getUser() instanceof UserInterface) {
2368
+
if ($token instanceof NullToken) {
2368
2369
// the user is not authenticated, e.g. only allow them to
2369
2370
// see public posts
2370
2371
return $subject->isPublic();
2371
2372
}
2372
2373
}
2373
2374
}
2374
2375
2376
+
.. caution::
2377
+
2378
+
:class:`Symfony\\Component\\Security\\Core\\Authentication\\Token\\NullToken` is only available in voters
2379
+
(because :method:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\VoterInterface::vote` can't receive a null token). Outside of voters (controllers, other services...) there is no token in the
0 commit comments