Skip to content

Conversation

@smmehdisharifi
Copy link

Summary

This PR adds a canAny method to Illuminate\Routing\Route to allow applying authorization middleware for multiple abilities at once. The route will be accessible if the authenticated user is authorized for at least one of the specified abilities.

Note: With the current structure, achieving this behavior requires defining separate routes for each ability. This method removes that need by allowing multiple abilities on a single route.

Example Usage

use App\Http\Controllers\DashboardController;

Route::get('/dashboard', DashboardController::class)
    ->canAny(['view-dashboard', 'admin-dashboard']);

Example Use Cases

  • Multi-role access: grant route access to users with any of several roles/permissions
  • Soft feature flags: allow access to a feature if the user has one of several related permissions

Implementation Details

  • Added Route::canAny(array $abilities, array|string $models = []) method
  • Added AuthorizeAny middleware that calls Gate::check() internally
  • Followed the same structure and style as the existing can method and Authorize middleware
  • Fully backward compatible; existing can() usage is unaffected
  • Registered middleware alias 'can.any' => \Illuminate\Auth\Middleware\AuthorizeAny::class

Tests

  • Unit test: middleware assignment

    • Route is accessible when the user has one of the abilities
    • Route is accessible when the user has multiple abilities
    • Route is forbidden when the user has none of the abilities
    • Proper behavior with and without a model parameter
  • Integration tests: enum-backed abilities

    • Guest forbidden when all abilities fail
    • Guest allowed when one ability passes

BC Impact

No breaking changes. Existing Route::can() functionality is untouched.

Notes

  • Code is PSR-12 compliant and formatted with Laravel Pint
  • Commit message: allow canAny method to be chained onto route for quick multiple-ability authorization middleware additions

@github-actions
Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@smmehdisharifi smmehdisharifi marked this pull request as ready for review September 11, 2025 09:28
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

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