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] Add BackedEnum support to Gate methods #52677

Merged
merged 3 commits into from
Sep 6, 2024

Conversation

diaafares
Copy link
Contributor

Hi,
Using Enums enhances code readability and reduces typos; it also improves IDE support by providing better autocomplete

This change is backward compatible and shouldn't bring any breaking changes.

enum Abilities: string {
    case VIEW_DASHBOARD = 'view-dashboard';
    case EDIT = 'edit';
    case UPDATE = 'update';
}


// Before
Gate::define('view-dashboard', function (User $user) {
    return $user->isAdmin;
});

Gate::authorize('view-dashboard');
Gate::inspect('view-dashboard');
Gate::check('view-dashboard');
Gate::any(['edit', 'update], $post);
Gate::none(['edit', 'update]], $post);
Gate::allows('update', $post)
Gate::denies('update', $post)

// After
Gate::define(Abilities::VIEW_DASHBOARD, function (User $user) {
    return $user->isAdmin;
});

Gate::authorize(Abilities::VIEW_DASHBOARD);
Gate::inspect(Abilities::VIEW_DASHBOARD);
Gate::check(Abilities::VIEW_DASHBOARD);
Gate::any([Abilities::EDIT, Abilities::UPDATE], $post);
Gate::none([Abilities::EDIT, Abilities::UPDATE], $post);
Gate::allows(Abilities::UPDATE, $post)
Gate::denies(Abilities::UPDATE, $post)

@taylorotwell taylorotwell merged commit 32daea1 into laravel:11.x Sep 6, 2024
32 of 33 checks passed
@iBotPeaches
Copy link
Contributor

@diaafares - once these 2 (#52677, #52679) got merged together they led to build failures due to the enum collide of

enum AbilitiesEnum: string {
    case VIEW_DASHBOARD

@diaafares
Copy link
Contributor Author

@iBotPeaches I added a new PR to fix the issue
#52683
Can you please check it to see if it solves the problem?

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