Blogger Information
Blog 47
fans 1
comment 0
visits 53275
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP - laravel 的 简单权限控制
晴天
Original
941 people have browsed it
  • 在中间件中执行权限控制
    ```php
    public function handle($request, Closure $next)
    {
    1. $admin = Auth::user(); // 取出该用户的session
    2. $gid = $admin->gid; // 取出 gid 权限id
    3. $rights = DB::table('admin_group')->where('gid',$gid)->item()['rights'];
    4. // 利用gid去数据库取出该角色的可用权限 [1,2,3,54,6,89,5,25].. 对应着菜单的mid
    5. if($rights){
    6. $rights = json_decode($rights); //转成数组
    7. }
  1. // 拿到当前用户访问菜单的控制器与方法
  2. $res = $request->route()->action['controller'];
  3. $res = explode('\\',$res);
  4. $route = array_pop($res);
  5. $route = explode('@',$route); // 返回数组 0=> 控制器 1=> 方法
  6. // 去数据库查到该方法的mid 然后判断mid 是否存在于可用权限$rights中
  7. $mid = DB::table('admin_menu')->where('controller',$route[0])->where('action',$route[1])->item()['mid'];
  8. // 如果不存在 返回权限不足
  9. if (!in_array($mid,$rights)) {
  10. return response('权限不足',200);
  11. }
  12. // 存在 继续执行
  13. return $next($request);
  14. }

```

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post