class BanMiddleware

Same name in other branches
  1. 9 core/modules/ban/src/BanMiddleware.php \Drupal\ban\BanMiddleware
  2. 8.9.x core/modules/ban/src/BanMiddleware.php \Drupal\ban\BanMiddleware
  3. 11.x core/modules/ban/src/BanMiddleware.php \Drupal\ban\BanMiddleware

Provides a HTTP middleware to implement IP based banning.

Hierarchy

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

Title Sort descending Modifiers Object type Summary
BanMiddleware::$banIpManager protected property The ban IP manager.
BanMiddleware::$httpKernel protected property The decorated kernel.
BanMiddleware::handle public function
BanMiddleware::__construct public function Constructs a BanMiddleware object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.