Skip to content
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

[11.x] Allow the authorize method to accept Backed Enums directly #53330

Merged
merged 2 commits into from
Oct 29, 2024
Merged

[11.x] Allow the authorize method to accept Backed Enums directly #53330

merged 2 commits into from
Oct 29, 2024

Conversation

johanvanhelden
Copy link
Contributor

This PR improves the authorize method by allowing it to accept Backed Enums directly as the ability parameter.

In most of the applications I am working in, we are using BackedEnums for permissions:

enum DashboardPermission: string
{
    case VIEW = 'dashboard.view';
}

Currently, when performing authorization checks using the authorize method, developers need to explicitly access the value of the enum:

    public function index(): Response
    {
        $this->authorize(DashboardPermission::VIEW->value);

        //
    }

This PR modifies the authorize method to accept Backed Enums directly, so there is no need anymore to access the value property manually. With this change, the authorization check could now be:

    public function index(): Response
    {
        $this->authorize(DashboardPermission::VIEW);

        //
    }

Testing:
A test has been added to FoundationAuthorizesRequestsTraitTest to cover the new behavior.

@johanvanhelden johanvanhelden changed the title Allow authorize method to accept Backed Enums directly [11.x] Allow authorize method to accept Backed Enums directly Oct 29, 2024
@johanvanhelden johanvanhelden changed the title [11.x] Allow authorize method to accept Backed Enums directly [11.x] Allow the authorize method to accept Backed Enums directly Oct 29, 2024
@taylorotwell taylorotwell merged commit 55f435b into laravel:11.x Oct 29, 2024
31 checks passed
@johanvanhelden johanvanhelden deleted the authorize-enums branch October 29, 2024 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants