BanIpCheck.php 888 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Models\BanIp;
  4. use Closure;
  5. use Illuminate\Support\Facades\Auth;
  6. use Illuminate\Support\Facades\Cache;
  7. class BanIpCheck
  8. {
  9. /**
  10. * Handle an incoming request.
  11. *
  12. * @param \Illuminate\Http\Request $request
  13. * @param \Closure $next
  14. * @return mixed
  15. */
  16. public function handle($request, Closure $next)
  17. {
  18. $ip = $request->getClientIp();
  19. $ips = Cache::rememberForever('ip_blacklist', function () {
  20. if (!file_exists(storage_path('installed'))) {
  21. return [];
  22. }
  23. return BanIp::all()->pluck('ip')->toArray();
  24. });
  25. if (Auth::check() && !Auth::user()->isSuperAdmin()) {
  26. if (in_array($ip, $ips)) {
  27. abort('403');
  28. return false;
  29. }
  30. }
  31. return $next($request);
  32. }
  33. }