PermissionAuth.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Illuminate\Support\Facades\Auth;
  5. use Spatie\Permission\Models\Permission;
  6. use Illuminate\Support\Facades\Route;
  7. use App\Models\Admin\Admin;
  8. class PermissionAuth
  9. {
  10. /**
  11. * Handle an incoming request.
  12. *
  13. * @param \Illuminate\Http\Request $request
  14. * @param \Closure $next
  15. * @return mixed
  16. */
  17. public function handle($request, Closure $next)
  18. {
  19. $user = Auth::guard('admin')->user();
  20. // 超级管理员
  21. if ($user->hasRole('超级管理员')) {
  22. return $next($request);
  23. }
  24. $route = Route::currentRouteName();
  25. $permission = Permission::where('route', $route)->first();
  26. // 判断权限表中这条路由是否需要验证
  27. if ($permission) {
  28. if (! $user->hasPermissionTo($permission->id)) {
  29. // return response()->view('errors.403', ['status' => "权限不足,需要:{$permission->name}权限"]);
  30. abort(403, '没有权限');
  31. }
  32. }
  33. return $next($request);
  34. }
  35. }