Using Laravel authorisation policies #250
Replies: 2 comments
-
Hey @ale1981 indeed we don't do any route model binding and that makes it a little harder to work with policies. We only have two options here since we don't know beforehand the models that the 1 - Create a general "CrudModel" interface and ask developers to implement it on their models, that way we can type-hint the interface instead of the specific model. 2 - overwrite the -public function update($id)
+public function update(Model $model) { $this->crud->entry = $model; return parent::update($model->id) } Don't forget that you need to use the default backpack guard middleware https://github.com/Laravel-Backpack/CRUD/blob/main/src/app/Http/Middleware/UseBackpackAuthGuardInsteadOfDefaultAuthGuard.php Or manually change it yourself before calling before the Alternatively set the backpack guard to null in the configuration so it will use the default Laravel one. Also note that Backpack does not ship with You should then be able to just use the authorizeResource in your controller: public function __construct()
{
parent::__construct();
$this->authorizeResource(\App\Models\Monster::class, 'id');
} Cheers |
Beta Was this translation helpful? Give feedback.
-
Hi @pxpm Thanks for the detailed response, I will probably end up doing 2. and overwriting the edit and update methods in the controllers. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have added the following to my UserCrudController and this fails on methods that require route binding to work with the error;
Is this because Backpack isn't doing any route binding, id is basically a string for the resource id?
Am I able to inject the model resource into the id parameter instead of it just being a string?
Beta Was this translation helpful? Give feedback.
All reactions