CheckPermission.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace app\common\middleware;
  3. use thans\jwt\facade\JWTAuth;
  4. use app\admin\model\Role;
  5. use Exception;
  6. //验证权限
  7. class CheckPermission
  8. {
  9. public function handle($request, \Closure $next)
  10. {
  11. try {
  12. $noNeedRight = [
  13. 'admin/login',
  14. ];
  15. $route = $request->pathinfo();
  16. if(in_array($route, $noNeedRight)){
  17. return $next($request);
  18. } else {
  19. $jwtData = JWTAuth::auth();
  20. $adminInfo = [];
  21. if (!empty($jwtData['admin'])) {
  22. $adminInfo = $jwtData['admin']->getValue();
  23. // 超级管理员直接放行
  24. if ($adminInfo) {
  25. if ($adminInfo->id == 1) {
  26. return $next($request);
  27. }
  28. // 获取角色拥有的所有路由权限(需在用户模型中实现)
  29. $roleRoutes = Role::getRoleMenu($adminInfo->role_id);
  30. // 校验权限:当前路由是否在用户允许的路由列表中
  31. if (in_array($route, $roleRoutes)) {
  32. return $next($request);
  33. }
  34. }
  35. }
  36. return shutdown(lang('您暂无权限'), -1);
  37. }
  38. } catch (Exception $exception) {
  39. return shutdown($errorMsgArr[$exception->getMessage()] ?? $exception->getMessage(), -1);
  40. }
  41. return $next($request);
  42. }
  43. }