2c153fc0cac2c90e1cda98e5fd10e974ed70f65e
[yaffs-website] / web / core / modules / ban / src / BanMiddleware.php
1 <?php
2
3 namespace Drupal\ban;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Symfony\Component\HttpFoundation\Request;
7 use Symfony\Component\HttpFoundation\Response;
8 use Symfony\Component\HttpKernel\HttpKernelInterface;
9
10 /**
11  * Provides a HTTP middleware to implement IP based banning.
12  */
13 class BanMiddleware implements HttpKernelInterface {
14
15   /**
16    * The decorated kernel.
17    *
18    * @var \Symfony\Component\HttpKernel\HttpKernelInterface
19    */
20   protected $httpKernel;
21
22   /**
23    * The ban IP manager.
24    *
25    * @var \Drupal\ban\BanIpManagerInterface
26    */
27   protected $banIpManager;
28
29   /**
30    * Constructs a BanMiddleware object.
31    *
32    * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
33    *   The decorated kernel.
34    * @param \Drupal\ban\BanIpManagerInterface $manager
35    *   The ban IP manager.
36    */
37   public function __construct(HttpKernelInterface $http_kernel, BanIpManagerInterface $manager) {
38     $this->httpKernel = $http_kernel;
39     $this->banIpManager = $manager;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
46     $ip = $request->getClientIp();
47     if ($this->banIpManager->isBanned($ip)) {
48       return new Response(SafeMarkup::format('@ip has been banned', ['@ip' => $ip]), 403);
49     }
50     return $this->httpKernel->handle($request, $type, $catch);
51   }
52
53 }