1 year ago

Policies/Gate against multiple user types?

Posted 1 year ago by lmartins


I have a bit of an edge case which I'm not being able to resolve using the methods described in Laravel's documentation.

On this app, I have two user types, admin and front-end, which use different models for each case. I need to allow both user types to see a third model, lets say Products, with each user type having its own set of rules, but both using the same controller and view to see the product.

Let's say:

App\User can see model App\Product if App\User bought that product. App\Admin can see model App\Product if App\Admin is the author of that product.

The problem im facing is that when I check the policy on the App\Product controller, no matter what try I cannot get the admin to be authorised.

I've tried to simplify the test case, to something like this:

    public function view(Product $product)
        $this->authorize('view', $product);
        return view('products.view', compact('product'));

and then in ProductPolicy I'm returning true just to check if I'm getting to the gate method:

    public function view($user, Product $product)
        return true;

I'm also not type hinting the $user variable, just because they come from different models. My guess is that this is related to my problem here, but I'm not getting to figure this one out.

Anyone with experience in a similar use case?

Thanks so much!

Please sign in or create an account to participate in this conversation.