You are here

class BanMiddleware in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/ban/src/BanMiddleware.php \Drupal\ban\BanMiddleware
  2. 9 core/modules/ban/src/BanMiddleware.php \Drupal\ban\BanMiddleware

Provides a HTTP middleware to implement IP based banning.

Hierarchy

  • class \Drupal\ban\BanMiddleware implements \Symfony\Component\HttpKernel\HttpKernelInterface

Expanded class hierarchy of BanMiddleware

1 file declares its use of BanMiddleware
BanMiddlewareTest.php in core/modules/ban/tests/src/Unit/BanMiddlewareTest.php
1 string reference to 'BanMiddleware'
ban.services.yml in core/modules/ban/ban.services.yml
core/modules/ban/ban.services.yml
1 service uses BanMiddleware
ban.middleware in core/modules/ban/ban.services.yml
Drupal\ban\BanMiddleware

File

core/modules/ban/src/BanMiddleware.php, line 13

Namespace

Drupal\ban
View source
class BanMiddleware implements HttpKernelInterface {

  /**
   * The decorated kernel.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
   */
  protected $httpKernel;

  /**
   * The ban IP manager.
   *
   * @var \Drupal\ban\BanIpManagerInterface
   */
  protected $banIpManager;

  /**
   * Constructs a BanMiddleware object.
   *
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
   *   The decorated kernel.
   * @param \Drupal\ban\BanIpManagerInterface $manager
   *   The ban IP manager.
   */
  public function __construct(HttpKernelInterface $http_kernel, BanIpManagerInterface $manager) {
    $this->httpKernel = $http_kernel;
    $this->banIpManager = $manager;
  }

  /**
   * {@inheritdoc}
   */
  public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE) : Response {
    $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);
  }

}

Members