Laravel Spatie 403用户尚未登录

时间:2020-05-06 15:53:05

标签: laravel spatie

本教程之后,我正在使用多重身份验证:https://github.com/sujalpatel2209/LaravelMultiUserAuthentication 并且我正在尝试按照本教程实施Spatie:https://www.itsolutionstuff.com/post/laravel-58-user-roles-and-permissions-tutorialexample.html 但是问题是,我无法访问“角色管理”页面。说403 用户尚未登录。 我也尝试遵循此说明,但仍然无法解决 https://docs.spatie.be/laravel-permission/v3/basic-usage/multiple-guards/

请问您能解决这个问题吗?

管理员

class Admin extends Authenticatable {
    use Notifiable, HasRoles;
    use SoftDeletes;

    protected $guard_name = 'admin';
}

Web.php

Route::group(['middleware' => 'checkAdminLogin'], function() {
 //Manage Roles
    Route::resource('roles','RoleController');
});

CreateAdminUserSeeder.php

class CreateAdminUserSeeder extends Seeder
{
    /**
    * Run the database seeds.
    *
    * @return void
    */
    public function run()
    {
        $user = Admin::create([
            'id' => '7',
            'name' => 'Hardik Savani', 
            'email' => 'admin@gmail.com',
            'password' => bcrypt('123456'),
            'created_by' => '0',
            'created_at' => '2020-04-29 12:43:27',
            'updated_at' => '2020-04-29 12:43:27',
            'username' => 'hardik',
            'roles' => '["ADMIN"]',
            'avatar' => '',
            'status' => 'ACTIVE'
        ]);

        $role = Role::create(['guard_name' => 'admin', 'name' => 'Admin']);
        $permissions = Permission::pluck('id','id')->all();
        $role->syncPermissions($permissions);   
        $user->assignRole([$role->id]);
    }
}

Auth.php

'guards' => [
        'admin' => [
            'driver' => 'session',
            'provider' => 'admin'
        ],

        'student' => [
            'driver' => 'session',
            'provider' => 'student'
        ],

        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],
'providers' => [
        'admin' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class
        ],

        'student' => [
            'driver' => 'eloquent',
            'model' => App\Student::class
        ],

        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

RoleController.php

class RoleController extends Controller
{
    /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
    function __construct() {
        $this->middleware('permission:role-list|role-create|role-edit|role-delete', ['only' => ['index','store']]);
        $this->middleware('permission:role-create', ['only' => ['create','store']]);
        $this->middleware('permission:role-edit', ['only' => ['edit','update']]);
        $this->middleware('permission:role-delete', ['only' => ['destroy']]);
    }

    /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
    public function index(Request $request) {
        $roles = Role::orderBy('id','DESC')->paginate(5);
        return view('roles.index',compact('roles'))->with('i', ($request->input('page', 1) - 1) * 5);
    }
}

1 个答案:

答案 0 :(得分:2)

你也可以传递你的守卫名字

$this->middleware('permission:role-list|role-delete,**YOURGUARDNAME**', ['only' => ['index','store']]);