Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.2k
AddCacheableVoterInterface
into voters docs#20282
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
74 changes: 67 additions & 7 deletionssecurity/voters.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -48,15 +48,75 @@ which makes creating a voter even easier:: | ||
abstract protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool; | ||
} | ||
The Cacheable Voter Interface | ||
michnovka marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
----------------------------- | ||
When Symfony calls the ``isGranted()`` method during runtime, it checks each voter | ||
until it meets the access decision strategy set by the configuration. | ||
While this generally works well, it can slow down performance in some cases. | ||
Imagine a backend displaying 20 items, each with 6 properties and 3 actions | ||
(like edit, show, delete). Checking permissions for all these requires 180 calls | ||
to each voter. With 5 voters, that's 900 calls. | ||
Usually, voters only need to focus on a specific permission (e.g., ``EDIT_BLOG_POST``) | ||
or object type (e.g., ``User``). This focus makes voters cacheable, so Symfony 5.4 introduces a | ||
:class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\CacheableVoterInterface` | ||
michnovka marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
namespace Symfony\Component\Security\Core\Authorization\Voter; | ||
interface CacheableVoterInterface extends VoterInterface | ||
{ | ||
public function supportsAttribute(string $attribute): bool; | ||
// $subjectType is the value returned by get_class($subject) or get_debug_type($subject) | ||
public function supportsType(string $subjectType): bool; | ||
} | ||
If your voter returns false for these methods, it will cache this result, | ||
and your voter won't be called again for that attribute or type. | ||
If you extend from the abstract Voter class, you don't need to implement | ||
the interface directly - just override ``supportsAttribute()`` and/or ``supportsType()``. | ||
For instance, if your voter handles multiple object types but all permissions | ||
are prefixed with ``APPROVE_``, do this: | ||
namespace App\Security; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
class MyVoter extends Voter | ||
{ | ||
public function supportsAttribute(string $attribute): bool | ||
{ | ||
return str_starts_with($attribute, 'APPROVE_'); | ||
} | ||
// ... | ||
} | ||
If your voter handles various permissions for a specific type, do this: | ||
namespace App\Security; | ||
use App\Entity\BlogPost; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
class MyVoter extends Voter | ||
{ | ||
public function supportsType(string $subjectType): bool | ||
{ | ||
// you can't use a simple BlogPost::class === $subjectType comparison | ||
// here because the given subject type could be the proxy class used | ||
// by Doctrine when creating the entity object | ||
return is_a($subjectType, BlogPost::class, true); | ||
} | ||
// ... | ||
} | ||
.. _how-to-use-the-voter-in-a-controller: | ||
Setup: Checking for Access in a Controller | ||
------------------------------------------ | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.