Laravel 权限:绕过身份验证提供程序的超级管理员不起作用

问题描述

我正在使用 spatie 的 laravel-permission 包,并在我的应用程序上创建了角色和权限,并在 AuthServiceProvider 中为他们的文档中指定的超级管理员角色编写了绕过。它像魅力一样工作,突然它无缘无故停止工作,返回 403 错误。我已经重新启动了服务器并清除了路由缓存。我找不到停止工作的原因...

这是我的 AuthServiceProvider 和路由

认证服务提供者:

public function boot()
{
    $this->registerPolicies();

    // allows super-admin everywhere in the app
    Gate::before(function (User $user) {
        return $user->hasRole('super-admin') ? true : null;
    });
}

路线:

// admin only management
Route::group(['prefix' => 'admin','middleware' => ['role:writer']],function () {

    //admin dashboard
    Route::get('/dashboard',[AdminController::class,'index'])->name('admin.dashboard');

    // blog management
    Route::get('articles',[AdminArticles::class,'index'])->name('admin.articles.index');
    Route::get('articles/create','create'])->name('admin.articles.create');
    Route::get('articles/my-articles','myArticles'])->name('admin.articles.myArticles');
    Route::get('articles/{article}/edit','edit'])->name('admin.articles.edit');
    Route::get('articles/{article}/delete','destroy'])->name('admin.articles.destroy');
    Route::put('articles/{article}','update'])->name('admin.articles.update');
    Route::post('articles','store'])->name('admin.articles.store');
});

Route::group(['prefix' => 'admin','middleware' => ['role:super-admin']],function() {
    //pages
    Route::get('/homepage',[HomepageController::class,'create'])->name('admin.home.create');
    Route::get('/homepage/{homepage}/edit','edit'])->name('admin.home.edit');
    Route::put('/homepage/{homepage}','update'])->name('admin.home.update');
    Route::post('/homepage','store'])->name('admin.home.store');

    //users
    Route::get('users',[UsersController::class,'index'])->name('users.index');
    Route::get('users/json','usersJson'])->name('users.json');
    Route::get('users/create','create'])->name('users.create');
    Route::get('users/{user}/delete','destroy'])->name('users.destroy');
    Route::get('users/{user}/edit','edit'])->name('users.edit');
    Route::put('users/{user}','update'])->name('users.update');
    Route::post('users','store'])->name('users.store');
});

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)