|
1
|
|
|
<?php namespace Neomerx\Limoncello\Http\Middleware; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015 [email protected] (www.neomerx.com) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use \Closure; |
|
20
|
|
|
use \Symfony\Component\HttpFoundation\Request; |
|
21
|
|
|
use \Symfony\Component\HttpFoundation\Response; |
|
22
|
|
|
use \Neomerx\Limoncello\Contracts\IntegrationInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @package Neomerx\Limoncello |
|
26
|
|
|
*/ |
|
27
|
|
|
class RequireAjaxMiddleware |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var IntegrationInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $integration; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param IntegrationInterface $integration |
|
38
|
|
|
*/ |
|
39
|
2 |
|
public function __construct(IntegrationInterface $integration) |
|
40
|
|
|
{ |
|
41
|
2 |
|
$this->integration = $integration; |
|
42
|
2 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Handle an incoming request. |
|
46
|
|
|
* |
|
47
|
|
|
* @param Request $request |
|
48
|
|
|
* @param Closure $next |
|
49
|
|
|
* |
|
50
|
|
|
* @return Response |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function handle(Request $request, Closure $next) |
|
53
|
|
|
{ |
|
54
|
|
|
/** @var Response $response */ |
|
55
|
2 |
|
$response = $request->isXmlHttpRequest() === true ? $next($request) : $this->createForbiddenResponse(); |
|
56
|
|
|
|
|
57
|
2 |
|
return $response; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return Response |
|
62
|
|
|
*/ |
|
63
|
1 |
|
protected function createForbiddenResponse() |
|
64
|
|
|
{ |
|
65
|
1 |
|
$response = $this->integration->createResponse(null, Response::HTTP_FORBIDDEN, []); |
|
66
|
|
|
|
|
67
|
1 |
|
return $response; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|