BanMiddleware.php in Drupal 9
File
core/modules/ban/src/BanMiddleware.php
View source
<?php
namespace Drupal\ban;
use Drupal\Component\Render\FormattableMarkup;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class BanMiddleware implements HttpKernelInterface {
protected $httpKernel;
protected $banIpManager;
public function __construct(HttpKernelInterface $http_kernel, BanIpManagerInterface $manager) {
$this->httpKernel = $http_kernel;
$this->banIpManager = $manager;
}
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
$ip = $request
->getClientIp();
if ($this->banIpManager
->isBanned($ip)) {
return new Response(new FormattableMarkup('@ip has been banned', [
'@ip' => $ip,
]), 403);
}
return $this->httpKernel
->handle($request, $type, $catch);
}
}
Classes
Name |
Description |
BanMiddleware |
Provides a HTTP middleware to implement IP based banning. |