1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCache package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\HttpCache\SymfonyCache; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\RequestMatcher; |
17
|
|
|
use Symfony\Component\HttpFoundation\RequestMatcherInterface; |
18
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Base class for handlers for the symfony built-in HttpCache that need access |
22
|
|
|
* control on requests. |
23
|
|
|
* |
24
|
|
|
* @author David Buchmann <[email protected]> |
25
|
|
|
* |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
abstract class AccessControlledSubscriber implements EventSubscriberInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var RequestMatcher |
32
|
|
|
*/ |
33
|
|
|
private $clientMatcher; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* When creating this subscriber, you can configure a number of options. |
37
|
|
|
* |
38
|
|
|
* - client_matcher: RequestMatcherInterface to identify clients that are allowed to send request. |
39
|
|
|
* - client_ips: IP or array of IPs of clients that are allowed to send requests. |
40
|
|
|
* |
41
|
|
|
* Only one of request matcher or IPs may be a non-null value. If you use a |
42
|
|
|
* RequestMatcher, configure your IPs into it. |
43
|
|
|
* |
44
|
|
|
* If neither parameter is set, the filter is IP 127.0.0.1 |
45
|
|
|
* |
46
|
|
|
* @param array $options Options to overwrite the default options |
47
|
|
|
* |
48
|
|
|
* @throws \InvalidArgumentException if both client_matcher and client_ips are set or unknown keys are found in $options |
49
|
|
|
*/ |
50
|
14 |
|
public function __construct(array $options = []) |
51
|
|
|
{ |
52
|
14 |
|
$options = $this->getOptionsResolver()->resolve($options); |
53
|
|
|
|
54
|
12 |
|
$clientMatcher = $options['client_matcher']; |
55
|
12 |
|
if ($clientMatcher && $options['client_ips']) { |
56
|
1 |
|
throw new \InvalidArgumentException('You may not set both a request matcher and an IP.'); |
57
|
|
|
} |
58
|
11 |
|
if (!$clientMatcher) { |
59
|
8 |
|
$clientMatcher = new RequestMatcher(null, null, null, $options['client_ips'] ?: '127.0.0.1'); |
60
|
8 |
|
} |
61
|
|
|
|
62
|
11 |
|
$this->clientMatcher = $clientMatcher; |
63
|
11 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the options resolver for the constructor arguments. |
67
|
|
|
* |
68
|
|
|
* @return OptionsResolver |
69
|
|
|
*/ |
70
|
14 |
|
protected function getOptionsResolver() |
71
|
|
|
{ |
72
|
14 |
|
$resolver = new OptionsResolver(); |
73
|
14 |
|
$resolver->setDefaults([ |
74
|
14 |
|
'client_matcher' => null, |
75
|
14 |
|
'client_ips' => null, |
76
|
14 |
|
]); |
77
|
|
|
$resolver->setAllowedTypes('client_matcher', [RequestMatcherInterface::class, 'null']); |
78
|
|
|
$resolver->setAllowedTypes('client_ips', ['string', 'array', 'null']); |
79
|
|
|
|
80
|
|
|
return $resolver; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Check whether the request is allowed. |
85
|
|
|
* |
86
|
|
|
* @param Request $request The request to check |
87
|
|
|
* |
88
|
|
|
* @return bool Whether access is granted |
89
|
|
|
*/ |
90
|
|
|
protected function isRequestAllowed(Request $request) |
91
|
|
|
{ |
92
|
|
|
return $this->clientMatcher->matches($request); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|