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

Multi-Resource Models issue #227

Open
invaders-xx opened this issue Feb 19, 2023 · 11 comments
Open

Multi-Resource Models issue #227

invaders-xx opened this issue Feb 19, 2023 · 11 comments

Comments

@invaders-xx
Copy link

I followed the doc by creating a proxy, link this proxy to the policy and I have an issue with my policy's delete function parameter type :
Argument #2 ($item) must be of type Illuminate\Database\Eloquent\Model, App\JsonApi\Proxies\Model given.

My delete policy declaration is as followed :

public function delete(User $user, Model $item): bool
{
}

My proxy declaration is as followed:

class Model extends Proxy
{
   
    public function __construct(OtherModel $model = null)
    {
        parent::__construct($model ?: new OtherModel());
    }
}

and OtherModel extends Illuminate\Database\Eloquent\Model

Any thoughts ?

@lindyhopchris
Copy link
Contributor

Hi! This is covered in the docs already here - https://laraveljsonapi.io/docs/3.0/digging-deeper/proxies.html#authorization

I.e. you need to tell Laravel what policy to use for the Proxy. It's described in that section in the docs. The tip under the code block is also worth reading.

I'm going to close this as I don't believe there is an issue. But if you're following that section of the docs and you think there's a bug, then reopen with more details as to why you think this is a bug.

@invaders-xx
Copy link
Author

invaders-xx commented Feb 21, 2023

@lindyhopchris I did associate this Proxy model with the right policy in AuthServiceProvider that's why I mentioned that the delete function within this policy returns that message

@lindyhopchris lindyhopchris reopened this Feb 21, 2023
@lindyhopchris
Copy link
Contributor

Can you share the authorizer code (for both authorisers - the proxy and the model), plus what you've put in the AuthServiceProvider?

@lindyhopchris
Copy link
Contributor

Worth mentioning I do have this in a production app, and it's definitely working. So I suspect it's something to do with your setup (until proved otherwise!)

@invaders-xx
Copy link
Author

My proxy is :

namespace App\JsonApi\Proxies;

use LaravelJsonApi\Eloquent\Proxy;
use Modules\Core\Entities\Company;

class Customer extends Proxy
{
  
    public function __construct(Company $customer = null)
    {
        parent::__construct($customer ?: new Company());
    }
}

in AuthServiceProvider :

protected $policies = [
        Role::class => RolePolicy::class,
        User::class => UserPolicy::class,
        \App\JsonApi\Proxies\Supplier::class => CompanyPolicy::class,
        \App\JsonApi\Proxies\Customer::class => CompanyPolicy::class,
    ];

@lindyhopchris
Copy link
Contributor

The issue won't be in the proxy.

What's your CompanyPolicy code?

@invaders-xx
Copy link
Author

public function delete(User $user, Model $item): bool
{
return $user->can('delete-company');
}

@lindyhopchris
Copy link
Contributor

lindyhopchris commented Feb 21, 2023

What about the import statements? Can I see the whole class?

@invaders-xx
Copy link
Author

invaders-xx commented Feb 21, 2023

What do you mean by import statements ?

@invaders-xx
Copy link
Author

Hum, in the destroy function of the customer controller, should I call $customer->toBase()->delete(); instead of $customer->delete(); That's maybe the issue ?

@lindyhopchris
Copy link
Contributor

Ah sorry, so seems the scope of this has changed. I was asking about import statements (use statements) because in your original message you were saying there was a problem with the class of the object passed to the policy.

It seems you now have a problem in the controller?

delete() should forward from the proxy to the model. What error are you getting now?

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

No branches or pull requests

2 participants