Kevin Tanudjaja
Kevin Tanudjaja

Reputation: 886

Laravel Backpack Permission seeding "role does not exist"

I would like to ask about Seeding in Laravel BackPack Permission Manager,

I'm trying default Backpack Permission Manager configuration, and trying to seed it with dummy data.

Here is my Roles and Permission seeder method:

    public function run()
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // create permissions
        Permission::create(['name' => 'view members']);
        Permission::create(['name' => 'add members']);
        Permission::create(['name' => 'edit members']);
        Permission::create(['name' => 'delete members']);

        Role::create(['name' => 'super-admin'])
            ->givePermissionTo(Permission::all());

        Role::create(['name' => 'admin'])
            ->givePermissionTo('add members');
    }

And here is my User seeder method:

    public function run()
    {
        factory(User::class)->create([
            'name' => 'Arianna',
            'email' => '[email protected]'
        ])->assignRole('super-admin');
    }

These Seeder is fine, but I realized that the Users CRUD interface doesn't show roles correctly

users roles shown as "-",

roles shown as -

enter image description here

and when I checked the database, model_has_roles shown App\User as model type

App\User model_type:

enter image description here

I'm sure model_type should be App\Models\BackpackUser , so I tried seeding again now with BackpackUser class

now I got different problem, BackpackUser don't have any roles, as roles needs to be in "backpack" guard to be assigned to BackpackUser

 Spatie\Permission\Exceptions\RoleDoesNotExist  : There is no role named `super-admin`.

  at .\vendor\spatie\laravel-permission\src\Exceptions\RoleDoesNotExist.php:11
     7| class RoleDoesNotExist extends InvalidArgumentException
     8| {
     9|     public static function named(string $roleName)
    10|     {
  > 11|         return new static("There is no role named `{$roleName}`.");
    12|     }
    13|
    14|     public static function withId(int $roleId)
    15|     {

  Exception trace:

  1   Spatie\Permission\Exceptions\RoleDoesNotExist::named("super-admin")
      .\vendor\spatie\laravel-permission\src\Models\Role.php:91

  2   Spatie\Permission\Models\Role::findByName("super-admin", "backpack")
      .\vendor\spatie\laravel-permission\src\Traits\HasRoles.php:270

what is my options? Why I can't just seed normally with default configuration?

please don't say that I need to manually "seed" the dummy data using Users interface..

Upvotes: 0

Views: 5952

Answers (2)

Kevin Tanudjaja
Kevin Tanudjaja

Reputation: 886

This issue caused by Spatie/Laravel-permission also stores model type in the db and puts the permission on the BackpackUser or User model.

Well.. just recently got answer from here:

https://github.com/Laravel-Backpack/PermissionManager/issues/193]

the solution is just added on end of 2019 from tabacitu:

https://backpackforlaravel.com/docs/4.0/base-about#having-both-regular-users-and-admins

basically you need either to add middleware, either add is_admin column on User or use the middleware provided by tabacitu

Upvotes: 0

António Almeida
António Almeida

Reputation: 10117

When you are creating your Roles and Permissions, you should define backpack guard using backpack_guard_name():

Permission::create([
    'name' => 'view members',
    'guard_name' => backpack_guard_name()
]);

Do the same for Roles:

Role::create([
    'name' => 'super-admin',
    'guard_name' => backpack_guard_name()
]);

On your table, model_has_roles your model type can't be App\User, it should be Backpack\Base\app\Models\BackpackUser:

use \Backpack\Base\app\Models\BackpackUser;
...

public function run()
{
    factory(BackpackUser::class)->create([
        'name' => 'Arianna',
        'email' => '[email protected]'
    ])->assignRole('super-admin');
}

Note:

BackpackUser extends App\User, this may cause you problems because your User may have custom fields, and by using BackpackUser you don't have those when creating it on a factory.

To solve this, you'd need to do it in two steps. First, creating the App\User with all the required fields, then, get the User as a BackpackUser object to assign the roles or permissions.

Upvotes: 1

Related Questions