简体   繁体   English

Spatie/Laravel-权限:虚拟数据错误

[英]Spatie/Laravel-Permissions: Dummy Data Error

I am using Laravel with spatie/laravel-permission package as described in this tutorial .我将 Laravel 与 spatie/laravel-permission 包一起使用,如本教程中所述 However, when I'm trying to create the dummy data, it is returning the following error Spatie\\Permission\\Exceptions\\RoleDoesNotExist : There is no role named Admin .但是,当我尝试创建虚拟数据时,它返回以下错误Spatie\\Permission\\Exceptions\\RoleDoesNotExist :没有名为Admin角色。

Below is the class to generate the dummy data.下面是生成虚拟数据的类。

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Ask for db migration refresh, default is no
        if ($this->command->confirm('Do you wish to refresh migration before seeding, it will clear all old data ?')) {
            // Call the php artisan migrate:refresh
            $this->command->call('migrate:refresh');
            $this->command->warn("Data cleared, starting from blank database.");
        }

        // Seed the default permissions
        Permission::firstOrCreate(['name' => 'Administer roles & permissions', 'guard_name' => 'isAdmin']);


        $this->command->info('Default Permissions added.');

        // Confirm roles needed
        if ($this->command->confirm('Create Roles for user, default is admin and user? [y|N]', true)) {

            // Ask for roles from input
            $input_roles = $this->command->ask('Enter roles in comma separate format.', 'Admin,User');

            // Explode roles
            $roles_array = explode(',', $input_roles);

            // add roles
            foreach($roles_array as $role) {
                $role = Role::firstOrCreate(['name' => trim($role), 'guard_name' => 'isAdmin']);

                if( $role->name == 'Admin' ) {
                    // assign all permissions
                    $role->syncPermissions(Permission::all());
                    $this->command->info('Admin granted all the permissions');
                } else {
                    // for others by default only read access
                    $role->syncPermissions(Permission::where('name', 'LIKE', 'view_%')->get());
                }

                // create one user for each role
                $this->createUser($role);
            }

            $this->command->info('Roles ' . $input_roles . ' added successfully');

        } else {
            Role::firstOrCreate(['name' => 'User']);
            $this->command->info('Added only default user role.');
        }

        // now lets seed some posts for demo
        $this->command->info('Some Posts data seeded.');
        $this->command->warn('All done :)');
    }

    /**
     * Create a user with given role
     *
     * @param $role
     */
    private function createUser($role)
    {
        $user = factory(User::class)->create();
        $user->assignRole($role->name);

        if( $role->name == 'Admin' ) {
            $this->command->info('Here is your admin details to login:');
            $this->command->warn($user->email);
            $this->command->warn('Password is "secret"');
        }
    }
}

Any idea to solve it?有什么想法可以解决吗?

You need to run你需要跑

php artisan cache:forget spatie.permission.cache

Before use Roles or Permissions使用角色或权限之前

I fixed this error by changing the order of the guards in the config/auth.php file:我通过更改 config/auth.php 文件中的守卫顺序修复了这个错误:

This is by default这是默认的

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'jwt',
            'provider' => 'users',
        ],
    ],

and this is what I have now这就是我现在所拥有的

    'guards' => [
        'api' => [
            'driver' => 'jwt',
            'provider' => 'users',
        ],
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    ],

I found out that I got this error when I changed the default guard to "api" (by default it's set to "web" ).我发现当我将默认防护更改为“api” (默认设置为“web” )时出现此错误。

When you seed roles without changing the order it correctly sets the guard_name to "api" but when it wants to apply these roles to a user it probably uses "web" guard.当您在不更改顺序的情况下播种角色时,它会将guard_name 正确设置为“api”,但是当它想将这些角色应用于用户时,它可能会使用“web” guard。

If you are using the default guard and you still get this error just try to set guard_name field in the roles seeder like this如果您使用的是默认防护,但仍然收到此错误,请尝试在角色播种器中设置 guard_name 字段,如下所示

Role::create(['name' => 'admin', 'guard_name' => 'web']);

This issue caused by Spatie/Laravel-permission also stores model type in the db and puts the permission on the BackpackUser or User model.由 Spatie/Laravel-permission 引起的此问题还将模型类型存储在 db 中,并将权限放在 BackpackUser 或 User 模型上。

please see this link for reference:请参阅此链接以供参考:

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

the solution is just added on end of 2019 from tabacitu:该解决方案是在 2019 年底从 tabacitu 添加的:

https://backpackforlaravel.com/docs/4.0/base-about#having-both-regular-users-and-admins https://backpackforlaravel.com/docs/4.0/base-about#have-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基本上你需要添加中间件,或者在 User 上添加 is_admin 列或者使用 tabacitu 提供的中间件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM