Skip to content

Commit c9b77ef

Browse files
committed
Add some informacion about why not using the Security service
1 parent 0e8d2b6 commit c9b77ef

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

security/voters.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,25 @@ with ``ROLE_SUPER_ADMIN``::
256256
}
257257
}
258258

259+
.. caution::
260+
261+
In the previous example, avoid using the following code to check if a role
262+
is granted permission::
263+
264+
// DON'T DO THIS
265+
use Symfony\Component\Security\Core\Security;
266+
// ...
267+
268+
if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
269+
// ...
270+
}
271+
272+
The ``Security::isGranted()`` method inside a voter has a significant
273+
drawback: it does not guarantee that the checks are performed on the same
274+
token as the one in your voter. The token in the token storage might have
275+
changed or could change in the meantime. Always use the ``AccessDecisionManager``
276+
instead.
277+
259278
If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`,
260279
you're done! Symfony will automatically pass the ``security.helper``
261280
service when instantiating your voter (thanks to autowiring).

0 commit comments

Comments
 (0)