Skip to content

Add support for Enums in Voters #61235

@DRaichev

Description

@DRaichev

Description

I think Enums should be supported better in voters. This idea comes from a project I am working on. Below is a description of my current situation, and why I feel the need for Enum support.

Right now I have a Feature enum used as:
$security->isGranted(Feature::MyFeature->name)

I decided to improve the DX and make it work as:
$this->security->isGranted(Feature::MyFeature)
Since I kept forgetting to add the ->name at the end.

The issue is in the controllers where I’m using the #[IsGranted()]
#[IsGranted(Feature::MyFeature->name)] works fine, but #[IsGranted(Feature::MyFeature)] won’t work because of type mismatch. And since Enums in php can’t extend I don’t have the option to extend Expression, nor does it make much sense to do so anyway.

So now I have to support Feature|string in my voter and handle the string case with a Feature::fromName() method, just because of the attribute.

At a glance adding support for Enums in the attribute seems like a good idea.
I think the only missing piece for this to work 100% is a typehint in the IsGranted attribute, but I might be missing something.

Please advise.

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions