PermissionMiddleware.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Servers\MenuServer;
  4. use App\Servers\PermissionServer;
  5. use Closure;
  6. class PermissionMiddleware
  7. {
  8. /**
  9. * Handle an incoming request.
  10. *
  11. * @param \Illuminate\Http\Request $request
  12. * @param \Closure $next
  13. * @return mixed
  14. */
  15. public function handle($request, Closure $next)
  16. {
  17. $user = app('auth')->user();
  18. $clientRoute = request()->route()->getName();
  19. if (!empty($user)) MenuServer::creatServer()->sendMenu($user->{'roles_id'});
  20. if (in_array($clientRoute, ['login','logout', 'admin.index.ajax', 'admin.not.permission', 'admin.index', 'admin.ajaxIndex', 'admin.switch', 'admin.u_sort'])) {
  21. return $next($request);
  22. }
  23. $ret = PermissionServer::verifyPermission($user->{'roles_id'}, $clientRoute);
  24. if (empty($ret) && $user->{'id'} != 1) {
  25. $is_ajax = request()->ajax();
  26. if ($is_ajax) {
  27. return response()->json([
  28. 'status' => 'error',
  29. 'message' => '暂无权限',
  30. 'redirect' => '',
  31. 'callback' => '',
  32. 'data' => []
  33. ]);
  34. } else {
  35. // 当前会员没有权限
  36. return redirect()->route('admin.not.permission');
  37. }
  38. }
  39. return $next($request);
  40. }
  41. }