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