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

Introduce model policy fallback #54495

Closed
wants to merge 1 commit into from

Conversation

jasonmccreary
Copy link
Contributor

@jasonmccreary jasonmccreary commented Feb 6, 2025

I find model policies methods to be redundant. Often, model policies look something like:

class ModelPolicy
{
    // ...

    public function create(User $user)
    {
        return true;
    }
    
    public function update(User $user, $model)
    {
        return $user->id == $model->user_id;
    }
    
    public function delete(User $user, $model)
    {
        return $user->id == $model->user_id;
    }
    
    // ...
}

This PR introduces a fallback method which allows policies to be streamlined. Using fallback, the model policy above could look something like:

class ModelPolicy
{
    // ...

    public function fallback(User $user, $ability, $model, ...$arguments)
    {
        return $ability === 'create' || $user->id == $model?->user_id;
    }
}

Much like before, fallback is only called if prior methods in the chain do not exist or return null. It also receives the authenticated user, name of the original ability, model (optional), and any additional arguments (optional).

While this is technically a breaking change, it would only affect model policies which define a fallback method. I'd consider that a very low impact compared to what is gained. Nonetheless, I am targeting Laravel 12.


Why not something else?
I went through a lot of combinations in my livestream, In the end, a singular method (i.e. not userFallback, guestFallback, withoutModelFallback, etc) provided the most flexibility when combined with the existing methods while having the least impact.

As far as the name, its behavior is much more akin to Route::fallback than an after callback in the sense that it is only called if prior methods return null and not simply after them. It can also affect the outcome.

@shaedrich
Copy link
Contributor

 public function fallback(User $user, $ability, $model, ...$arguments)
    {
        return $ability === 'create' || $user->id == $model?->user_id;
    }

isn't this essentially just __call()?

@jasonmccreary
Copy link
Contributor Author

@shaedrich, yep. Just formalizing it and with necessary parameters.

@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.

3 participants