JosephSilber / bouncer

Laravel Eloquent roles and abilities.
MIT License
3.44k stars 329 forks source link

Table permission inside the field entity_type, custom Role class name into table name role? #533

Closed liuzhaobo1999 closed 3 years ago

liuzhaobo1999 commented 4 years ago

Recently updated the laravel version and bouncer version, and then the previous permissions are invalidated, and the verification fails. I originally used laravel5.4, bouncer: v1.0.0-beta.3, the field entity_type in the table permission stores the custom Role class name; now I upgrade to the latest version of laravel7, bouncer: v1.0.0-rc.8; before The existing permission verification failed. After querying with Laravel Telescope, I found that entity_type=roles; how to modify this?

  1. Migrate table permission data directly, change the value of the entity_type field from the class name to the table name?
  2. Configured in other places, can entity_type continue to use the class name? Which plan should I use and how to modify it?
dmillot commented 3 years ago

Currently using laravel 8 and bouncer v1.0.0-rc.9 and I face the same issue. I created a custom model Role by using Bouncer IsRole trait and defining the $table name and $fillable fields. I also told Bouncer to use my custom model in the AppServiceProvider with BouncerFacade::useRoleModel(Role::class);

Unfortunately, the polymorphic table Permissions saves in the entity_type field roles instead of App\Models\Role.

Is there any solution on this?

JosephSilber commented 3 years ago

I'm not sure what you are both complaining about.

Maybe read this to get a clearer picture of polymorphic keys:

How to rid your database of PHP class names in Eloquent's Polymorphic tables

dmillot commented 3 years ago

Thanks for the resource.

If it helps anyone I just had to specify in the boot method of the AppServiceProvider this :

Relation::morphMap([
    'roles' => \App\Models\Role::class,
]);

By using morphMap method, I tell Laravel to not use the fully qualified class name but the custom name roles.

Here is what the entire method looks like:

public function boot()
{
    Relation::morphMap([
        'roles' => \App\Models\Role::class,
    ]);

    BouncerFacade::useRoleModel(\App\Models\Role::class);
}

Laravel 8 Custom Polymorphic Types