laravel - 一个或两个角色的组中间不起作用
问题描述
我有 3 个用户角色,超级管理员、管理员和用户或访客,3 组路由
1 = 将角色分配给用户 (SuperAdmin)。
2 = 管理员面板(超级管理员、管理员)。
3 = 索引页面(超级管理员、管理员、用户或访客)。
这三组路由是由具有给定权限的用户访问的。我尝试了很多东西,但都没有奏效
用户模型
public function role(){
return $this->belongsToMany('App\Role','role_assigns');
}
public function hasRole($role)
{
if ($this->role()->where('name','=',$role)->first()){
return true;
}return false;
}
public function isSuperAdmin() {
return $this->role()->where('name', '=','superadmin')->exists();
}
public function isAdmin() {
return $this->role()->where('name', '=','admin')->exists();
}
public function isUser() {
return $this->role()->where('name', '=','user')->exists();
}
好榜样
public function user(){
return $this->belongsToMany('App\Role','role_assigns');
}
路线
Route::group(['middleware' => 'roles:superadmin'], function (){
// User Assiginign Routes
Route::get('/users', 'UserRoleController@showAllUser');
Route::get('/assign', 'UserRoleController@showRoleAndUser');
Route::post('/user/{user_id} /role/{role_id}','UserRoleController@assignRoleToUser');
Route::post('assign_role/user/{user_id}/role/{role_id}',[
'uses'=>'UserRoleController@assignRoleToUser',
'as'=>'assign_role'
]);
Route::post('/ar', 'UserRoleController@showAllUser');
});
Route::group(['middleware' => 'roles:admin,superadmin'], function () {
// Product Dashboard for Admin
Route::get('/', 'ProductController@index');
Route::post('add_product','ProductController@store');
Route::get('/edit/{id}','ProductController@edit');
Route::get('/delete/{id}','ProductController@destroy');
Route::post('/update_product/{id}','ProductController@update');
Route::get('/add_product', function () {
return view('content.add_product');
});
});
Route::group(['middleware' => 'roles:admin,superadmin,user'], function () {
Route::get('/index', 'ProductController@index_page')
});
中间件.php
public function handle($request, Closure $next, ... $roles)
{
if (!Auth::check())
return redirect('signin');
$user = Auth::user();
if ( $user->isSuperAdmin()) {
return $next($request);
}elseif ($user->isAdmin() ){
return $next($request);
}
elseif ($user->isUser()||$request->user()->hasRole($roles)==null){
return redirect('index');
}
return response('Not Auttroize insufficent end of page',401);
解决方案
您可以通过获取用户的所有角色并检查他是否已授予权限来简单地做到这一点:
Route::group(['middleware' => 'roles:admin,superadmin,user'], function () {
Route::get('/index', 'ProductController@index_page')
});
然后在你的中间件中你应该做
public function handle($request, Closure $next, string $roles)
{
$user_roles = Auth::user()->role;
$roles = explode(',' $roles);
if ($user_roles->whereIn('role', $roles)->count()) {
return $next($request);
}
throw new Exeception('Unauthorized', 401)
}
推荐阅读
- javascript - 具有存储功能的克隆对象
- android - 将 id 移动到样式文件时如何使用数据绑定?
- python - 如何使用 Python SQL“UPDATE”语句更新需要从下拉菜单中选择的 MS Access MySQL 表字段?
- security - 访问令牌有多安全?
- sql-server - 如果记录存在,则在插入存储过程中调用更新存储过程
- python - 用先前计算的值替换操作数据集中的值
- dm-script - 如何在程序流程继续之前强制更新 UI?
- java - 关联实体管理器已关闭!休眠环境
- sql - EXTENDED STATISTICS 对 PostgreSQL 中的 INSERT/UPDATE/DELETE 有影响吗?
- java - 从数组列表中打印随机价格标签