Admin.php 1021 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Laravel\Horizon\Horizon;
  5. class Admin
  6. {
  7. /**
  8. * Handle an incoming request.
  9. *
  10. * @param \Illuminate\Http\Request $request
  11. * @param \Closure $next
  12. * @return mixed
  13. */
  14. public function handle($request, Closure $next)
  15. {
  16. $authorization = $request->input('auth_data') ?? $request->header('authorization');
  17. if (!$authorization) abort(403, '未登录或登陆已过期');
  18. $authData = explode(':', base64_decode($authorization));
  19. if (!isset($authData[1]) || !isset($authData[0])) abort(403, '鉴权失败,请重新登入');
  20. $user = \App\Models\User::where('password', $authData[1])
  21. ->where('email', $authData[0])
  22. ->first();
  23. if (!$user) abort(403, '鉴权失败,请重新登入');
  24. if (!$user->is_admin) abort(403, '未登录或登陆已过期');
  25. $request->merge([
  26. 'user' => $user
  27. ]);
  28. return $next($request);
  29. }
  30. }