Skip to content

[Security] Allow caching of Voter::voteOnAttribute() #62749

@ttk

Description

@ttk

Description

This is an RFC to determine if this is a good idea or not.

In my particular case, I implemented a class CustomVoter extends Voter that implements the voteOnAttribute() function. This function does some database queries and therefore is slow.

Now, in my template, I end up calling the is_granted('CUSTOM_ATTRIBUTE', subject) many times, and it ends up calling the slow CustomVoter::voteOnAttribute() function multiple times as well, making the page render slowly.

There are two simple workarounds: 1) set the results of the is_granted() function to a variable in the template and then use that, or 2) Internally implement a cache from within my CustomVoter::voteOnAttribute() using a class member variable. However, this could be avoided if Symfony handled the caching internally.

My question: Is there a reason why caching of the vote can't be done by default by abstract class Voter()? Perhaps it's because there could be database changes between two is_granted() calls? Maybe caching can be an opt-in rather than a default? An opt-in could be implemented by adding a is_cacheable member variable to \Symfony\Component\Security\Core\Authorization\Voter\Vote that can be set from within voteOnAttribute().

I think it's worth exploring this potential optimization. Initial thoughts/comments welcomed. If there is interest, I could put together a PR.

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCRFC = Request For Comments (proposals about features that you want to be discussed)Security

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions