You are here

class AdvbanMiddleware in Advanced ban 8

Provides a HTTP middleware to implement IP based banning.

Hierarchy

  • class \Drupal\advban\AdvbanMiddleware implements \Symfony\Component\HttpKernel\HttpKernelInterface

Expanded class hierarchy of AdvbanMiddleware

1 file declares its use of AdvbanMiddleware
AdvbanMiddlewareTest.php in tests/src/Unit/AdvbanMiddlewareTest.php
1 string reference to 'AdvbanMiddleware'
advban.services.yml in ./advban.services.yml
advban.services.yml
1 service uses AdvbanMiddleware
advban.middleware in ./advban.services.yml
Drupal\advban\AdvbanMiddleware

File

src/AdvbanMiddleware.php, line 12

Namespace

Drupal\advban
View source
class AdvbanMiddleware implements HttpKernelInterface {

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

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

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

  /**
   * {@inheritdoc}
   */
  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
    $ip = $request
      ->getClientIp();
    $ban_result = $this->banIpManager
      ->isBanned($ip, [
      'expiry_check' => TRUE,
      'info_output' => TRUE,
    ]);
    if ($ban_result['is_banned']) {
      $ban_text = $this->banIpManager
        ->banText([
        'ip' => $ip,
        'expiry_date' => $ban_result['expiry_date'],
      ]);
      return new Response($ban_text, 403);
    }
    return $this->httpKernel
      ->handle($request, $type, $catch);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdvbanMiddleware::$banIpManager protected property The ban IP manager.
AdvbanMiddleware::$httpKernel protected property The decorated kernel.
AdvbanMiddleware::handle public function Handles a Request to convert it to a Response.
AdvbanMiddleware::__construct public function Constructs a AdvbanMiddleware object.