1 | <?php |
||
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() |
|
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) |
||
94 | } |
||
95 |