我做了一个 Laravel 8 application (链接到 GitHub 存储库)需要用户注册和登录。
我目前正在努力添加用户角色和权限。我有 3 个角色(用户类型):管理员、作者和成员。每种类型的用户都应该有权访问仪表板的一部分。
用户表:
角色表:
在routes\web.php
中我有:
Route::get('/', [HomepageController::class, 'index'])->name('homepage');
Auth::routes();
Route::group(['middleware' => ['auth']], function() {
Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
Route::get('/dashboard/profile', [UserProfileController::class, 'index'])->name('profile');
Route::match(['get', 'post'],'/dashboard/profile/update', [UserProfileController::class, 'update'])->name('profile.update');
Route::post('/dashboard/profile/deleteavatar/{id}/{fileName}', [UserProfileController::class, 'deleteavatar'])->name('profile.deleteavatar');
//User roles
Route::get('/dashboard/author', [AuthorController::class, 'index']);
});
在User模型(app\Models\User.php
)中,我有:
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'role_id',
'username',
'first_name',
'last_name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function roles() {
return $this->belongsToMany(Role::class);
}
public function users()
{
return $this
->belongsToMany('App\User');
}
public function authorizeRoles($roles)
{
if ($this->hasAnyRole($roles)) {
return true;
}
abort(401, 'This action is unauthorized.');
}
public function hasAnyRole($roles)
{
if (is_array($roles)) {
foreach ($roles as $role) {
if ($this->hasRole($role)) {
return true;
}
}
} else {
if ($this->hasRole($roles)) {
return true;
}
}
return false;
}
public function hasRole($role)
{
if ($this->roles()->where('name', $role)->first()) {
return true;
}
return false;
}
}
在AuthorController (Controllers\Dashboard\AuthorController.php)
class AuthorController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('role:ROLE_Author');
}
public function index()
{
return view('dasboard.author');
}
}
如 CheckRole 中间件所示,如果用户未获得授权,则消息应为“此操作未经授权”:
class CheckRole
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next, $role)
{
if (!$request->user()->hasRole($role)) {
abort(401, 'This action is unauthorized.');
}
return $next($request);
}
}
问题
由于我无法查明的原因,尝试将作者重定向到管理面板的相应部分会导致403错误:
User does not have any of the necessary access rights.
问题
我做错了什么?
最佳答案
检查您的代码后。我发现了一些错误
1.在AuthorController中,您已将角色传递为ROLE_Author
而不是Author
$this->middleware('role:ROLE_Author');
但是在数据库中,您已将角色名称命名为Author
,所以它应该是
$this->middleware('role:Author');
2.在用户模型中,您有hasRole($role)
方法,但它正在访问具有belongsToMany
关系的角色关系
public function roles() {
return $this->belongsToMany(Role::class);
}
因此,如果您检查数据库 role_user
有空记录。因此,请在 role_user
表中添加相关数据,但现在您要在 users
表中添加角色.
假设您正在寻找在用户表
中分配角色
,然后更改用户表
中的关系角色
public function roles() {
return $this->belongsTo(Role::class,'role_id','id');
}
如果用户没有访问权限,则会抛出以下错误,否则会转到仪表板。
401 UNAUTHORIZED
此外,在 MemberController
中,您必须更改中间件
$this->middleware('role:ROLE_Member');
至
$this->middleware('role:Member');
也在 AuthorController
中。 Blade 文件名有错误。 view('dasboard.author')
但如果您看到 viewfolder
那么您已将文件夹命名为 dashboard
但在 View 中您提到了 dasboard.author
所以从这里开始改变
public function index()
{
return view('dasboard.author');
}
至
public function index()
{
return view('dashboard.author');
}
注意:经过检查,我没有发现提到的错误消息“用户没有任何必要的访问权限。”在 git repo 中。而且它不会为未经授权的用户抛出 403 错误。所以尝试清除 View 缓存、浏览器缓存。
关于php - 是什么原因导致此 Laravel 8 应用程序中的用户访问错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68045942/