Skip to content

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