Audit module: audit log and address bans
This commit is contained in:
1 parent
b75403e5ea
commit
d2ea1bacac
41 files changed
+2137
-5
No files matched your search
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Audit;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use IriksIT\Manager\Models\IpBan;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Keeps a banned address out of the whole manager, the login page included. It runs ahead of the
|
||||
* session and the login, so a banned visitor is not sent round in circles. The answer is written
|
||||
* here and not thrown. Alias `manager.not-banned`: put it on the login routes of the host app too.
|
||||
*/
|
||||
class RefuseBannedAddress
|
||||
{
|
||||
public function __construct(private readonly IpBans $bans) {}
|
||||
|
||||
/**
|
||||
* @param Closure(Request): Response $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
$ban = $this->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}."
|
||||
: "This address is blocked from {$where} until ".$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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user