bans->for($request->ip()); if ($ban === null) { return $next($request); } $this->bans->knocked($ban); return self::answer($request, $ban); } /** The page (or JSON) a banned address gets, also for a ban on the whole site. */ public static function answer(Request $request, IpBan $ban): Response { $where = $ban->whole_site ? __('this site') : __('the manager'); $message = $ban->expires_at === null ? __('This address is blocked from :where.', ['where' => $where]) : __('This address is blocked from :where until :when.', ['where' => $where, 'when' => $ban->expires_at->format('j F Y, H:i')]); // A ban that followed too many attempts is "429 Too Many Requests" and says when to come // back; one that was given by hand is a plain "403 Forbidden". $status = $ban->automatic ? 429 : 403; $headers = ['Cache-Control' => 'no-store']; if ($ban->automatic && $ban->expires_at !== null) { $headers['Retry-After'] = (string) max(1, (int) now()->diffInSeconds($ban->expires_at, false)); } return $request->expectsJson() ? response()->json(['message' => $message], $status, $headers) : response()->view('manager::banned', ['message' => $message, 'status' => $status], $status, $headers); } }