$admin = Auth::user(); // 取出该用户的session$gid = $admin->gid; // 取出 gid 权限id$rights = DB::table('admin_group')->where('gid',$gid)->item()['rights'];// 利用gid去数据库取出该角色的可用权限 [1,2,3,54,6,89,5,25].. 对应着菜单的midif($rights){$rights = json_decode($rights); //转成数组}
// 拿到当前用户访问菜单的控制器与方法$res = $request->route()->action['controller'];$res = explode('\\',$res);$route = array_pop($res);$route = explode('@',$route); // 返回数组 0=> 控制器 1=> 方法// 去数据库查到该方法的mid 然后判断mid 是否存在于可用权限$rights中$mid = DB::table('admin_menu')->where('controller',$route[0])->where('action',$route[1])->item()['mid'];// 如果不存在 返回权限不足if (!in_array($mid,$rights)) {return response('权限不足',200);}// 存在 继续执行return $next($request);}
```
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号