Mercurial > hg > isophonics-drupal-site
comparison core/modules/ban/src/BanMiddleware.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 } |