[英]Checking the user's role in Laravel
I am beginner in Laravel. 我是Laravel的初学者。 I have project in Laravel 5.8.
我在Laravel 5.8中有项目。
I have this code: 我有以下代码:
Middleweare: 中间人:
class CheckRole
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $role)
{
if (! $request->user()->hasRole([$role])) {
if( $role == 'admin' || $role== 'receptionist' || $role == 'adminCompany' || $role == 'telemarketer')
{
return redirect('/cms');
}
else
{
return redirect()->route('index');
}
}
return $next($request);
}
}
Seeder: 播种机:
public function run()
{
DB::table('roles')->insert([
'name' => 'admin'
]);
DB::table('roles')->insert([
'name' => 'adminCompany'
]);
DB::table('roles')->insert([
'name' => 'telemarketer'
]);
DB::table('roles')->insert([
'name' => 'receptionist'
]);
DB::table('roles')->insert([
'name' => 'user'
]);
DB::table('roles')->insert([
'name' => 'userPremium'
]);
DB::table('roles')->insert([
'name' => 'userCompany'
]);
DB::table('roles')->insert([
'name' => 'userSponsor'
]);
DB::table('roles')->insert([
'name' => 'userGuest'
]);
}
Schema: 架构:
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->engine = "InnoDB";
});
and my web.php: 和我的web.php:
Route::group(['prefix' => 'admin'], function () {
Route::get('/', 'BackendController@index')->name('adminHome')->middleware('CheckRole:user,CheckRole:userPremium,CheckRole:userCompany,CheckRole:userSponsor,CheckRole:userGuest');
});
User.php User.php
public function hasRole(array $roles)
{
foreach ($roles as $role) {
if (isset(self::$roles[$role])) {
if (self::$roles[$role]) return true;
} else {
self::$roles[$role] = $this->roles()->where('name', $role)->exists();
if (self::$roles[$role]) return true;
}
}
return false;
}
I would like the CheckRole middleware to check if the user has the following roles: user, userPremium, userCompany, userSponsor, userGuest for route adminHome. 我希望CheckRole中间件检查用户是否具有以下角色:adminadmin的用户,userPremium,userCompany,userSponsor,userGuest。 At the moment, Laravel only checks 1 role - not all.
目前,Laravel仅检查1个角色-并非全部。
How repair it? 怎么修呢?
You should separate your parameters of your middleware by , instead of duplicate requests to the same middleware 您应该使用来分隔中间件的参数,而不是重复请求相同的中间件
Same issue here See: How to pass multiple parameters to middleware with OR condition in Laravel 5.2 此处存在相同问题,请参见: 如何在Laravel 5.2中使用OR条件将多个参数传递给中间件
You might consider https://github.com/spatie/laravel-permission a permission package. 您可能会认为https://github.com/spatie/laravel-permission是权限包。
Route::group(['prefix' => 'admin'], function () {
Route::get('/', 'BackendController@index')->name('adminHome')->middleware('CheckRole:user,userPremium,userCompany,userSponsor,UserGuest');
});
Like in the article i posted above you need to handle the middleware parameters, 就像我上面发布的文章一样,您需要处理中间件参数,
As by example from the other answer, something like this 作为另一个答案的例子,像这样
/**
* Handle an incoming request.
*
* @param $request
* @param Closure $next
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function handle($request, Closure $next) {
$roles = array_slice(func_get_args(), 2); // [default, admin, manager]
foreach ($roles as $role) {
try {
Role::whereName($role)->firstOrFail(); // make sure we got a "real" role
if (Auth::user()->hasRole($role)) {
return $next($request);
}
} catch (ModelNotFoundException $exception) {
dd('Could not find role ' . $role);
}
}
Flash::warning('Access Denied', 'You are not authorized to view that content.'); // custom flash class
return redirect('/');
}
You probaly can access your $role, in laravel 5.8 just as an array or you should check if it's an array or string, and then loop trough them 您可能可以像数组一样访问laravel 5.8中的$ role,或者应该检查它是数组还是字符串,然后循环遍历它们
Looks a bit cleaner then using func_get_args() 看起来更干净一点,然后使用func_get_args()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.