Laravel-Backpack / PermissionManager

Admin interface for managing users, roles, permissions, using Backpack CRUD
http://backpackforlaravel.com
Other
516 stars 166 forks source link

Call to undefined method Spatie\Permission\Models\Permission::translationEnabled() #306

Closed NARTONIC closed 2 years ago

NARTONIC commented 2 years ago

Bug report

After create a Role, when I want to edit, returns this error Call to undefined method Spatie\Permission\Models\Permission::translationEnabled()

What I did:

Follows the guide

What I expected to happen:

Edit the role I've created

What happened:

Returns error Call to undefined method Spatie\Permission\Models\Permission::translationEnabled()

What I've already tried to fix it:

Backpack, Laravel, PHP, DB version:

Back Pack latests version 5.3 PHP 8.1

welcome[bot] commented 2 years ago

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication mediums:

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

-- Justin Case The Backpack Robot

kasperbjerby commented 2 years ago

Can confirm this bug

PHP 8.1.8 Laravel 9.19.0 Backpack 5.1.4

CamusMX7 commented 2 years ago

The permissions and roles worked perfectly but in some update they stopped working. II try to edit a role and get error: Call to undefined method Spatie\Permission\Models\Permission::translationEnabled() Not in all roles, only in some cases.

PHP 8.1.2 Laravel 9.1.0 Backpack 5.1.3

kiddtang commented 2 years ago

I try to troubleshoot... The culprit is the backpack/crud v5.1.3

✔️ - Upgrading spatie/laravel-permission (5.5.4 => 5.5.5) ✔️ - Upgrading backpack/crud (5.1.0 => 5.1.1) ✔️ - Upgrading backpack/crud (5.1.1 => 5.1.2) ❌ - Upgrading backpack/crud (5.1.2 => 5.1.3)

After backpack/crud is upgraded to 5.1.3, Role unable to edit and receive the error

BadMethodCallException
Call to undefined method Spatie\Permission\Models\Permission::translationEnabled()

Upgrade to the latest 5.1.4 doesn't help. temporary downgrade to 5.1.2

composer update backpack/crud:5.1.2

Commented out setupRelatedModelLocale in src/app/Library/CrudPanel/Traits/Update.php then the issue disappeared. It is introduced in backpack/crud v5.1.3 PR - Related models should have the main entry locale too #4359

pxpm commented 2 years ago

Hey @kiddtang @CamusMX7 @NARTONIC and @kasperbjerby

I am trying to work out this issue .. The problem is that the model you guys are using does not use the CrudTrait. The function translationEnabled() is a CrudTrait method... So I think we have two ways to go here: 1 - Add the CrudTrait to the model (since you are using Crud functions, the models you use should have the CrudTrait) 2 - Add pre-check to check if model has the crud trait before checking if the translations are enabled .

Let me talk with @tabacitu to try to figure out the best way to solve this, It will be fixed later today when we tag a new version, one way or another.

Thanks for the report and sorry for the bad experience.

Cheers

tabacitu commented 2 years ago

Wow such a good reply thank you @kiddtang !

Quick fix

@pxpm what if we also check that translationEnable() exists on the model? Would this fix it?

-if ($model->translationEnabled()) {
+if (method_exists($model, 'translationEnabled') && $model->translationEnabled()) {

Root cause analysis

The problem is interesting though. Like Pedro said... we probably should even get to this point.

Why does this even try to create/update on the Spatie\Permission\Models\Permission model? It should be trying to do that on Backpack\PermissionManager\app\Models\Permission::class, since:

tabacitu commented 2 years ago

Wait wait wait! I think I understand now! 🤯 This error appears when you:

The problem is in CRUD - that it now requires all related models to have CrudTrait - which is not ok ❌

In this case... I think the quick-fix I suggested above will also be a good long-term fix. Can you double-check my findings @pxpm ?

pxpm commented 2 years ago

@tabacitu I didn't get so deep on why the model is backpack or spatie because you can manually change those and use whatever you want as model, but you are right 💯 on the fact that not using the CrudTrait in ANY related model will trigger the error, and that is the real problem. So I also 💯 agree with the solution you proposed, similar to what I suggedted in 2. We can check if method exists or for CrudTrait presence (since the method is there).

I will submit a PR with with your suggestion then.

Cheers

kiddtang commented 2 years ago

Just FYI. I have done many customizations, so I extend the Spatie\Permission\Models\Role

in my config/backpack/permissionmanager.php

'models' => [
        'user'       => \App\Models\User::class,
        'permission' => \App\Models\Permission::class,
        'role'       => \App\Models\Role::class,
    ],

Also, app/Models/Role.php

use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Spatie\Permission\Models\Role as OriginalRole;

class Role extends OriginalRole
{
    use CrudTrait;
...

Also, app/Models/Permission.php

use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Spatie\Permission\Models\Permission as OriginalPermission;

class Permission extends OriginalPermission
{
    use CrudTrait;
...

So, thx @pxpm @tabacitu comments...

I think I should fix this issue by swapping the models defined in the config/permission.php

'models' => [
        'permission' => \App\Models\Permission::class,
        'role' => \App\Models\Role::class,
    ],
pxpm commented 2 years ago

Nope @kiddtang , this one is on our side :) unfortunately it slipped me, I have already sent a PR to fix it.

Cheers

tabacitu commented 2 years ago

Fixed in https://github.com/Laravel-Backpack/CRUD/pull/4514