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

Skip to content

[Security] Use instanceof NullToken in voters #17141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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: 1 addition & 1 deletion reference/forms/types/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ the data can be a ``DateTime`` object, a string, a timestamp or an array.
+---------------------------+-----------------------------------------------------------------------------+
| Underlying Data Type | can be ``DateTime``, string, timestamp, or array (see the ``input`` option) |
+---------------------------+-----------------------------------------------------------------------------+
| Rendered as | single text box or five select fields |
| Rendered as | single text box or five select fields |
+---------------------------+-----------------------------------------------------------------------------+
| Default invalid message | Please enter a valid date and time. |
+---------------------------+-----------------------------------------------------------------------------+
Expand Down
14 changes: 11 additions & 3 deletions security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2346,14 +2346,15 @@ Granting Anonymous Users Access in a Custom Voter
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you're using a :doc:`custom voter </security/voters>`, you can allow
anonymous users access by checking if there is no user set on the token::
anonymous users access by checking if the token is an instance of
:class:`Symfony\\Component\\Security\\Core\\Authentication\\Token\\NullToken`::

// src/Security/PostVoter.php
namespace App\Security;

// ...
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authentication\User\UserInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;

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

if (!$token->getUser() instanceof UserInterface) {
if ($token instanceof NullToken) {
// the user is not authenticated, e.g. only allow them to
// see public posts
return $subject->isPublic();
}
}
}

.. caution::

:class:`Symfony\\Component\\Security\\Core\\Authentication\\Token\\NullToken` is only available in voters
(because the :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
:class:`Symfony\\Component\\Security\\Core\\Authentication\\Token\\Storage\\TokenStorageInterface`
implementation when the user is not logged.

Setting Individual User Permissions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down