Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38a9ee1

Browse files
committed
Use instanceof NullToken in voters
To test if the user is not logged.
1 parent 37b1226 commit 38a9ee1

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

reference/forms/types/datetime.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ the data can be a ``DateTime`` object, a string, a timestamp or an array.
1313
+---------------------------+-----------------------------------------------------------------------------+
1414
| Underlying Data Type | can be ``DateTime``, string, timestamp, or array (see the ``input`` option) |
1515
+---------------------------+-----------------------------------------------------------------------------+
16-
| Rendered as | single text box or five select fields |
16+
| Rendered as | single text box or five select fields |
1717
+---------------------------+-----------------------------------------------------------------------------+
1818
| Default invalid message | Please enter a valid date and time. |
1919
+---------------------------+-----------------------------------------------------------------------------+

security.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,14 +2346,15 @@ Granting Anonymous Users Access in a Custom Voter
23462346
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23472347

23482348
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
2350+
:class:`Symfony\\Component\\Security\\Core\\Authentication\\Token\\NullToken`::
23502351

23512352
// src/Security/PostVoter.php
23522353
namespace App\Security;
23532354

23542355
// ...
2356+
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
23552357
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2356-
use Symfony\Component\Security\Core\Authentication\User\UserInterface;
23572358
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
23582359

23592360
class PostVoter extends Voter
@@ -2364,14 +2365,21 @@ anonymous users access by checking if there is no user set on the token::
23642365
{
23652366
// ...
23662367

2367-
if (!$token->getUser() instanceof UserInterface) {
2368+
if ($token instanceof NullToken) {
23682369
// the user is not authenticated, e.g. only allow them to
23692370
// see public posts
23702371
return $subject->isPublic();
23712372
}
23722373
}
23732374
}
23742375

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
2380+
:class:`Symfony\\Component\\Security\\Core\\Authentication\\Token\\Storage\\TokenStorageInterface`
2381+
implementation when the user is not logged.
2382+
23752383
Setting Individual User Permissions
23762384
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23772385

0 commit comments

Comments
 (0)