Группа Middle на одну или две роли не работает - PullRequest
0 голосов
/ 07 июня 2019

у меня 3 роли пользователя, SuperAdmin, Admin и User или guest, 3 группы маршрутов

1 = Назначение ролей пользователям (SuperAdmin).

2 = Панель администратора (SuperAdmin, Admin).

3 = страница указателя (SuperAdmin, Администратор, Пользователь или Гость).

эти три группы маршрутов - это доступ пользователя с данным разрешением. Я пробовал много вещей, но ничего из этого не работает

Модель пользователя

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')
});

Middleware.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);

1 Ответ

2 голосов
/ 07 июня 2019

Вы можете сделать это, просто получив все роли пользователя и проверив, дал ли он разрешение:

    Route::group(['middleware' => 'roles:admin,superadmin,user'], function () {
        Route::get('/index', 'ProductController@index_page')
    });

Тогда в вашем Middleware вы должны сделать

    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)
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...