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

[5.5] Implement iterable Gate::check() and Gate::any() #20084

Merged
merged 1 commit into from
Jul 15, 2017
Merged

[5.5] Implement iterable Gate::check() and Gate::any() #20084

merged 1 commit into from
Jul 15, 2017

Conversation

mikebronner
Copy link
Contributor

Premise

This is a simplified version of PR #19900, which adds two new capabilities to the Gate class:

  • updates check() method to allow an array or collection of abilities to test that every ability passes.
  • adds an any() method that takes an array or collection of abilities and checks if any one of those abilities passes.

In conjunction with the new Blade::if shorthand methods, this will make checking multiple abilities for a model in one go very simple.

Background

The initial PR was unable to be resolved as we couldn't come up with a good method name that succinctly captured the intent and different to the @can Blade directive, or the can() method in the Authorizable trait. Should a suitable name for the Gate::any() method be found, a subsequent PR will implement it as a new Blade directive and method on the Authorizable trait, as well as update the @can directive and can() method.

Examples

Blade::if('canEvery', function ($abilities, $arguments) {
    return app(Gate::class)->forUser(auth()->user())
        ->check($abilities, $arguments);
});

Blade::if('canAny', function ($abilities, $arguments) {
    return app(Gate::class)->forUser(auth()->user())
        ->any($abilities, $arguments);
});

@mikebronner mikebronner changed the title Implement iterable Gate::check() and Gate::any() [5.5] Implement iterable Gate::check() and Gate::any() Jul 15, 2017
@taylorotwell taylorotwell merged commit 2bd933a into laravel:master Jul 15, 2017
@taylorotwell
Copy link
Member

Thanks

@mikebronner
Copy link
Contributor Author

Thanks Taylor! :)

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.

2 participants